localdate 时间获取与转换,获取两个日期中间的所有天数
1 localDate 格式为年月日 yyyy-MM-dd
1.1 获取当前时间
//获取当前时间 LocalDate now = LocalDate.now();
1.2 string 转 LocalDate
//string 转 localDate LocalDate time = LocalDate.parse("2021-10-03");
1.3 日期所属的月份的第一天
LocalDate time = LocalDate.parse("2021-10-03"); //时间所属月份的第一天 LocalDate firstDayOfMonth = time.with(TemporalAdjusters.firstDayOfMonth());
1.4 日期所属月份最后一天
//时间所属月份的最后一天 LocalDate lastDayOfMonth = time.with(TemporalAdjusters.lastDayOfMonth());
1.5 时间下个月的第一天
//时间下个月的第一天 LocalDate firstDayOfNextMonth = time.with(TemporalAdjusters.firstDayOfNextMonth());
1.6 时间下一年的第一天
//时间 下一年的第一天 LocalDate firstDayOfNextYear = time.with(TemporalAdjusters.firstDayOfNextYear());
1.7 时间所属的年份的第一天
//时间所属年份的第一天 LocalDate firstDayOfYear = time.with(TemporalAdjusters.firstDayOfYear());
1.8 时间所属年份的最后一天
//时间所属年份的第一天 LocalDate lastDayOfYear = time.with(TemporalAdjusters.lastDayOfYear());
1.9 时间所属月份的周 的日期
//时间的第一个周末 LocalDate dayOfWeekInMonth = time.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY)); //时间的第二个周三 LocalDate dayOfWeekInMonth1 = time.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.WEDNESDAY)); //时间 上个月的最后一周 LocalDate with = time.with(TemporalAdjusters.dayOfWeekInMonth(0, DayOfWeek.SUNDAY));
1.10 时间所属月份的第一个周几
//时间所属月份第一个 周几 示例:第一个周一日期 LocalDate firstInMonth = time.with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY));
1.11 时间所属月份的最后一个周几的日期
//时间所属月份最后一个周几 日期 示例:最后一个周天日期 LocalDate lastInMonth = time.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
1.12 时间的下一个周几
1.13 时间的上一个周几
//时间 上一个周几 日期 示例:该时间的 上一个周日 日期 LocalDate previousSunday = time.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY));
1.14 获取时间的 月 周 日
// 获取时间的具体的 月 周 日 Month month = time.getMonth(); DayOfWeek dayOfWeek = time.getDayOfWeek(); int dayOfMonth = time.getDayOfMonth();
1.15 时间的 年月日 加减
/** * 时间 年月日的 加减 */ //减 LocalDate minusDays = time.minusDays(3); LocalDate localDate1 = time.minusWeeks(1); LocalDate minusMonths = time.minusMonths(2); LocalDate minusYears = time.minusYears(1); //加 LocalDate localDate = time.plusDays(7); LocalDate plusWeeks = time.plusWeeks(1); LocalDate months = time.plusMonths(1); LocalDate plusYears = time.plusYears(5);
1.16 时间的比较 之前-之后-相等-天数差
/** * 时间 的 比较 */ LocalDate three = LocalDate.parse("2021-10-03"); LocalDate four = LocalDate.parse("2021-10-23"); //天数差 int i = three.compareTo(four); //是否之后 boolean after = three.isAfter(four); //是否之前 boolean before = three.isBefore(four); //是否相等 boolean equals = three.equals(four); //时间的月份 比较 boolean equalsMonth = three.getMonth().equals(four.getMonth());
1.17 时间转换格式
/** * 时间转换格式 */ // LocalDate 转 string String s = three.toString(); // LocalDate 转 date Date date = Date.from(three.atStartOfDay(ZoneOffset.ofHours(8)).toInstant()); //LocalDate 转 localDateTime //获取时分秒 LocalTime localTime = LocalTime.now(); //LocalDateTime= 年月日+时分秒 LocalDateTime localDateTime = LocalDateTime.of(three,localTime);
测试用例:
@Test public void timeTest() { //获取当前时间 LocalDate now = LocalDate.now(); //string 转 localDate LocalDate time = LocalDate.parse("2021-10-03"); //时间所属月份的第一天 LocalDate firstDayOfMonth = time.with(TemporalAdjusters.firstDayOfMonth()); //时间所属月份的最后一天 LocalDate lastDayOfMonth = time.with(TemporalAdjusters.lastDayOfMonth()); //时间下个月的第一天 LocalDate firstDayOfNextMonth = time.with(TemporalAdjusters.firstDayOfNextMonth()); //时间 下一年的第一天 LocalDate firstDayOfNextYear = time.with(TemporalAdjusters.firstDayOfNextYear()); //时间所属年份的第一天 LocalDate firstDayOfYear = time.with(TemporalAdjusters.firstDayOfYear()); //时间所属年份的第一天 LocalDate lastDayOfYear = time.with(TemporalAdjusters.lastDayOfYear()); //时间的第一个周末 LocalDate dayOfWeekInMonth = time.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY)); //时间的第二个周三 LocalDate dayOfWeekInMonth1 = time.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.WEDNESDAY)); //时间 上个月的最后一周 LocalDate with = time.with(TemporalAdjusters.dayOfWeekInMonth(0, DayOfWeek.SUNDAY)); //时间所属月份第一个 周几 示例:第一个周一日期 LocalDate firstInMonth = time.with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY)); //时间所属月份最后一个周几 日期 示例:最后一个周天日期 LocalDate lastInMonth = time.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY)); //时间下一个周几 日期 示例:该时间的下一个周日 日期 LocalDate nextSunday = time.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)); //时间 上一个周几 日期 示例:该时间的 上一个周日 日期 LocalDate previousSunday = time.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)); // 获取时间的具体的 月 周 日 Month month = time.getMonth(); DayOfWeek dayOfWeek = time.getDayOfWeek(); int dayOfMonth = time.getDayOfMonth(); /** * 时间 年月日的 加减 */ //减 LocalDate minusDays = time.minusDays(3); LocalDate localDate1 = time.minusWeeks(1); LocalDate minusMonths = time.minusMonths(2); LocalDate minusYears = time.minusYears(1); //加 LocalDate localDate = time.plusDays(7); LocalDate plusWeeks = time.plusWeeks(1); LocalDate months = time.plusMonths(1); LocalDate plusYears = time.plusYears(5); /** * 时间 的 比较 */ LocalDate three = LocalDate.parse("2021-10-03"); LocalDate four = LocalDate.parse("2021-10-23"); //天数差 int i = three.compareTo(four); //是否之后 boolean after = three.isAfter(four); //是否之前 boolean before = three.isBefore(four); //是否相等 boolean equals = three.equals(four); //时间的月份 比较 boolean equalsMonth = three.getMonth().equals(four.getMonth()); /** * 时间转换格式 */ // LocalDate 转 string String s = three.toString(); // LocalDate 转 date Date date = Date.from(three.atStartOfDay(ZoneOffset.ofHours(8)).toInstant()); //LocalDate 转 localDateTime //获取时分秒 LocalTime localTime = LocalTime.now(); //LocalDateTime= 年月日+时分秒 LocalDateTime localDateTime = LocalDateTime.of(three,localTime); }
更新:2022-06-22
获取两个日期中间的所有天数,所有月份
/** * 获取一段时间的每一天日期或者月份 * 开始时间要小于结束时间 时间格式 日 2022-06-12 月 2022-06 * @param start * @param end * @return * @throws Exception */ public static List<String> getBetweenDate(String start, String end, Integer type) { List<String> list = new ArrayList<>(); if(start.length()<8){ start+="-01"; end+="-01"; } LocalDate startDate = LocalDate.parse(start); LocalDate endDate = LocalDate.parse(end); long distance = 0L; if (type == 1) { distance = ChronoUnit.DAYS.between(startDate, endDate); Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString())); } else if(type==2) { distance = ChronoUnit.MONTHS.between(startDate, endDate); Stream.iterate(startDate, d -> d.plusMonths(1)).limit(distance + 1).forEach(f -> list.add(f.toString().substring(0,7))); } return list; }
@Test
void test(){
LocalDate localDate = LocalDate.now();
LocalDate l = LocalDate.parse("2022-01-01");
List<String> day = getBetweenDate(l.toString(), localDate.toString(),1);
System.out.println("day = " + day);
List<String> month = getBetweenDate(l.toString(), localDate.toString(),2);
System.out.println("month = " + month);
}