java获取当天开始,结束时间

//获取当天结束时间

public static Date getEndTime(Date date) {
    Calendar dateEnd = Calendar.getInstance();
    dateEnd.setTime(date);
    dateEnd.set(Calendar.HOUR_OF_DAY, 23);
    dateEnd.set(Calendar.MINUTE, 59);
    dateEnd.set(Calendar.SECOND, 59);
    return dateEnd.getTime();
}
public static Date getEndTimeOfDay(Date date) {
   LocalDate toDay = toLocalData(date);
   LocalDateTime localDateTime = toDay.atTime(23, 59, 59, 999);
   ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
   return Date.from(zonedDateTime.toInstant());
}

 

//获取当天开始时间

public static Date getStartTime(Date date) {
    Calendar dateStart = Calendar.getInstance();
    dateStart.setTime(date);
    dateStart.set(Calendar.HOUR_OF_DAY, 0);
    dateStart.set(Calendar.MINUTE, 0);
    dateStart.set(Calendar.SECOND, 0);
    return dateStart.getTime();
}
public static Date getBeginTimeOfDay(Date date) {
   LocalDate toDay = toLocalData(date);
   LocalDateTime localDateTime = toDay.atTime(0, 0, 0);
   ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
   return Date.from(zonedDateTime.toInstant());
}

//每周开始时间

public static Date getBeginDayOfWeek(Date date) {
   Calendar c = new GregorianCalendar();
   c.setFirstDayOfWeek(Calendar.MONDAY);
   c.setTime(date);
   c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
   LocalDate toDay = toLocalData(c.getTime());
   LocalDateTime localDateTime = toDay.atTime(0, 0, 0);
   ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
   return Date.from(zonedDateTime.toInstant());
}

//每周的结束时间

public static Date getEndDayOfWeek(Date date) {
   Calendar c = new GregorianCalendar();
   c.setFirstDayOfWeek(Calendar.MONDAY);
   c.setTime(date);
   c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
   LocalDate toDay = toLocalData(c.getTime());
   LocalDateTime localDateTime = toDay.atTime(23, 59, 59, 999);
   ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
   return Date.from(zonedDateTime.toInstant());
}

//一个月的开始时间

public static Date getBeginTimeOfMonth(Date date) {
   LocalDate ld = toLocalData(date);
   YearMonth yearMonth = YearMonth.of(ld.getYear(), ld.getMonth());
   LocalDate localDate = yearMonth.atDay(1);
   LocalDateTime startOfDay = localDate.atStartOfDay();
   ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.systemDefault());
   return Date.from(zonedDateTime.toInstant());
}

//一个月的结束时间

public static Date getEndTimeOfMonth(Date date) {
   LocalDate ld = toLocalData(date);
   YearMonth yearMonth = YearMonth.of(ld.getYear(), ld.getMonth());
   LocalDate endOfMonth = yearMonth.atEndOfMonth();
   LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
   ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
   return Date.from(zonedDateTime.toInstant());
}

//前几个月,字符串形式

public static String[] getBeforeMonth(int num) {
   final String[] result = new String[num];
   LocalDate localDate = LocalDate.now();
   for (int i = 0; i < num; i++) {
      result[i] = (localDate.getMonthValue()) + "月";
      localDate = localDate.minusMonths(1);
   }
   return result;
}
//后几个月,字符串形式
public static String[] getAfterMonth(int num) {
   final String[] result = new String[num];
   LocalDate localDate = LocalDate.now();
   for (int i = 0; i < num; i++) {
      result[i] = (localDate.getMonthValue()) + "月";
      localDate = localDate.plusMonths(1);
   }
   return result;
}
//前几个月,integer类型
public static Integer[] getBeMonth(int num) {
   final Integer[] result = new Integer[num];
   LocalDate localDate = LocalDate.now();
   for (int i = 0; i < num; i++) {
      result[i] = (localDate.getMonthValue());
      localDate = localDate.minusMonths(1);
   }
   return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值