700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java获取当天开始 结束时间

java获取当天开始 结束时间

时间:2019-02-07 17:52:39

相关推荐

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()); // MondayLocalDate 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); // SundayLocalDate 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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。