日期Utils

/**
 * 获取某年第一天日期
 * @param year 年份
 * @return Date
 */
public static Date getYearFirst(String year){
   int thisYear =Integer.parseInt(year);
   Calendar calendar = Calendar.getInstance();
   calendar.clear();
   calendar.set(Calendar.YEAR, thisYear);
   Date currYearFirst = calendar.getTime();
   return currYearFirst;
}

/**
 * 获取某年最后一天日期
 * @param year 年份
 * @return Date
 */
public static Date getYearLast(String year){
   int thisYear =Integer.parseInt(year);
   Calendar calendar = Calendar.getInstance();
   calendar.clear();
   calendar.set(Calendar.YEAR, thisYear);
   calendar.roll(Calendar.DAY_OF_YEAR, -1);
   Date currYearLast = calendar.getTime();

   return currYearLast;
}

/**
 * 获取某年月的第一天 2019-12
 * @param yearAndMonth
 * @return
 */
public static Date getFisrtDayOfMonth(String yearAndMonth) {
   String[] yearAndMonths =yearAndMonth.split("-");
   int thisYear = Integer.parseInt(yearAndMonths[0]);
   int thisMonth =Integer.parseInt(yearAndMonths[1]);
   Calendar cal = Calendar.getInstance();
   //设置年份
   cal.set(Calendar.YEAR, thisYear);
   //设置月份
   cal.set(Calendar.MONTH, thisMonth - 1);
   //获取某月最小天数
   int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
   //设置日历中月份的最小天数
   cal.set(Calendar.DAY_OF_MONTH, firstDay);
   Date currYearMonthFirst = cal.getTime();
   return currYearMonthFirst;
}

/**
 *获取某年月的最后一天 2019-12
 * @param yearAndMonth
 * @return
 */
public static Date getLastDayOfMonth(String yearAndMonth) {
   String[] yearAndMonths =yearAndMonth.split("-");
   int thisYear = Integer.parseInt(yearAndMonths[0]);
   int thisMonth =Integer.parseInt(yearAndMonths[1]);
   Calendar cal = Calendar.getInstance();
   //设置年份
   cal.set(Calendar.YEAR,thisYear);
   //设置月份
   cal.set(Calendar.MONTH, thisMonth-1);
   //获取某月最大天数
   int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
   //设置日历中月份的最大天数
   cal.set(Calendar.DAY_OF_MONTH, lastDay);
   Date currYearMonthLast= cal.getTime();
   return currYearMonthLast;
}

/**
 * 时间戳转日期
 * @param timestamp
 * @param type
 * @return
 */
public String getStringTime(String timestamp,String type) {
   Date date = new Date(Long.valueOf(timestamp)*1000L);
   String str ="";
   if ("month".equals(type)){
      str = new SimpleDateFormat("yyyy-MM").format(date.getTime());  // 获取只有年月的时间
   }else if ("day".equals(type)){
      str = new SimpleDateFormat("yyyy-MM-dd").format(date.getTime());  // 获取只有年月日的时间
   }
   return str;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霤影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值