java8的日期处理
优势:
java.util.Date 和 SimpleDateFormatter 都不是线程安全的,而 LocalDate 和 LocalTime 和最基本的 String 一样,是不变类型,不但线程安全,而且不能修改。
java.util.Date 月份从0开始,一月是0,十二月是11。java.time.LocalDate 月份和星期都改成了 enum ,就不可能再用错了。
java.util.Date是一个“万能接口”,它包含日期、时间,还有毫秒数。如果你只需要日期或时间那么有一些数据就没啥用。在新的Java 8中,日期和时间被明确划分为LocalDate和LocalTime,LocalDate无法包含时间,LocalTime无法包含日期。当然,LocalDateTime才能同时包含日期和时间。
java.util.Date 推算时间(比如往前推几天/ 往后推几天/ 计算某年是否闰年/ 推算某年某月的第一天、最后一天、第一个星期一等等)要结合Calendar要写好多代码,相当麻烦。
今天简单介绍一下常用的方法:
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
//下一周的星期天
LocalDate newdata = localDate.with(nextOrSame(DayOfWeek.SUNDAY));
System.out.println(newdata);
//当月的最后一天
LocalDate lastDate = localDate.with(lastDayOfMonth());
System.out.println(lastDate);
//表示当前月的第二周的星期天
LocalDate date1 = localDate.with(dayOfWeekInMonth(2,DayOfWeek.SUNDAY));
System.out.println(date1);
//当前月的第一天
LocalDate date2 = localDate.with(firstDayOfMonth());
System.out.println(date2);
//下个月的第一天
LocalDate date3 = localDate.with(firstDayOfNextMonth());
System.out.println(date3);
//明年的第一天
LocalDate date4 = localDate.with(firstDayOfNextYear());
System.out.println(date4);
//当年的第一天
LocalDate date5 = localDate.with(firstDayOfYear());
System.out.println(date5);
//本月第一个满足星期三的日期
LocalDate date6 =localDate.with(firstInMonth(DayOfWeek.WEDNESDAY));
System.out.println(date6);
//今年的最后一天
LocalDate date7 = localDate.with(lastDayOfYear());
System.out.println(date7);
//当月最后一个满足是星期四的日期
LocalDate date8 = localDate.with(lastInMonth(DayOfWeek.TUESDAY));
System.out.println(date8);
//下个星期天的日期
LocalDate date9 = localDate.with(next(DayOfWeek.SUNDAY));
LocalDate date13 = localDate.with(nextOrSame(DayOfWeek.SUNDAY));
System.out.println(date9);
System.out.println("localDate2==="+localDate);
//上个星期天的日期
LocalDate date10 = localDate.with(previous(DayOfWeek.SUNDAY));
System.out.println(date10);
LocalDate date11 = localDate.with(previousOrSame(DayOfWeek.SUNDAY));
System.out.println(date11);
//上个星期三
LocalDate localDate1 = LocalDate.now();
LocalDate date12 = localDate1.with(previousOrSame(DayOfWeek.WEDNESDAY));
System.out.println(date12);
// 获取当前时间日期
ZonedDateTime date1 = ZonedDateTime.parse("2015-12-03T10:15:30+05:30[Asia/Shanghai]");
System.out.println("date1: " + date1);
ZoneId id = ZoneId.of("Europe/Paris");
System.out.println("ZoneId: " + id);
ZoneId currentZone = ZoneId.systemDefault();
System.out.println("当期时区: " + currentZone);
LocalDateTime dt1 = LocalDateTime.of(2017, Month.APRIL,18,13,45,20);
System.out.println(dt1);
LocalDate date1 = dt1.toLocalDate(); //通过LocalDateTime获得LocalDate
LocalTime time1 = dt1.toLocalTime(); //通过LocalDateTime获得LocalTime
System.out.println("date1======="+date1+"time1===="+time1);
LocalDate date = LocalDate.of(2014,02,26);
LocalTime time = LocalTime.of(12,23,20);
LocalDateTime dt2 = LocalDateTime.of(date,time);
System.out.println(dt2);
//LocalDate结合LocalTime成一个LocalDateTime
LocalDateTime dt3 = date.atTime(13,45,20);
System.out.println(dt3); //2014-02-26T13:45:20
//LocalDate结合LocalTime成一个LocalDateTime
LocalDateTime dt4 = date.atTime(time);
System.out.println(dt4); //2014-02-26T12:23:20
//LocalTime结合LocalDate成LocalDateTime
LocalDateTime dt5 = time.atDate(date);
System.out.println(dt5); //2014-02-26T12:23:20
Date date = new Date();
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId); //转换
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = localDateTime.format(dateTimeFormatter);
System.out.println(format);
LocalDate localDate1 = instant.atZone(zoneId).toLocalDate(); //转换
LocalTime localTime = instant.atZone(zoneId).toLocalTime(); //转换
LocalDateTime localDateTime1 = instant.atZone(zoneId).toLocalDateTime();//转换
String format2 = localDateTime1.format(dateTimeFormatter);
System.out.println(format2);
//--------------------------------------------------------------------------
LocalDateTime l = LocalDateTime.now();
ZonedDateTime zonedDateTime = l.atZone(zoneId);
Date date1 = Date.from(zonedDateTime.toInstant());
LocalDate localDate = LocalDate.now();
ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
Date date2 = Date.from(zdt.toInstant());
LocalDate today = LocalDate.now();
// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2);
// 取本月最后一天,再也不用计算是28,29,30还是31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
// 取下一天:
LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(1);
// 取2015年1月第一个周一,这个计算用Calendar要死掉很多脑细胞:
LocalDate firstMondayOf2015 = LocalDate.parse("2019-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2015-01-05
// 判断闰年
boolean isLeapYear = today.isLeapYear();
//判断该月天数
int days = today.lengthOfMonth();
这里说一下with(TemporalAdjusters temporalAdjusters ):