java 1.8 LocalDateTime

2019-06-27  本文已影响0人  張先生_9efb

LocalDateTime now = LocalDateTime.now();

// 2019-06-27T10:10:42.135

System.out.println(now);

// 2019-06-27 10:10:42

System.out.println(now.format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));

// 2019年6月27日 星期四

System.out.println(now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)));

// 2019年6月27日

System.out.println(now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)));

// 2019-6-27

System.out.println(now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)));

// 19-6-27

System.out.println(now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)));

// 2019-6-27 10:16:32

System.out.println(now.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));

// 2019年6月27日 上午10时16分32秒

System.out.println(now.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)));

// 19-6-27 上午10:16

System.out.println(now.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)));

// 2019-06-27T10:00

System.out.println(now.truncatedTo(ChronoUnit.HOURS));

// 2019-06-27T00:00

System.out.println(now.truncatedTo(ChronoUnit.DAYS));

// 减两天

System.out.println(now.minusDays(2));

LocalDate today = LocalDate.now();

//获取当前日期2019-06-27

System.out.println(today);

// 今天的零点2019-06-27T00:00

System.out.println(today.atStartOfDay());

//1 - 7

System.out.println(today.range(ChronoField.DAY_OF_WEEK));

//1 - 365

System.out.println(today.range(ChronoField.DAY_OF_YEAR));

//1 - 30

System.out.println(today.range(ChronoField.DAY_OF_MONTH));

// 加两周

System.out.println(today.plusWeeks(2L));

// 当前时代 IsoEra 值:CE

System.out.println(today.getEra());

// 当前月的天数30

System.out.println(today.lengthOfMonth());

// 28

System.out.println(today.plusMonths(-4L).lengthOfMonth());

// 计算周期

System.out.println(today.until(ChronoLocalDate.from(LocalDate.now().plusMonths(2L))));

// 这个月的第一天

System.out.println(today.with(TemporalAdjusters.firstDayOfMonth()));

// 这个月的第二天

System.out.println(today.withDayOfMonth(2));

// 这个月的最后一天

System.out.println(today.with(TemporalAdjusters.lastDayOfMonth()));

// 这个月的第一个周一

System.out.println(today.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));

LocalTime time = LocalTime.now();

// 09:33:33.200

System.out.println(time);

// 09:33:33

System.out.println(time.withNano(0));

// 加一个小时10:33:33.200

System.out.println(time.plusHours(1));

// 减一个小时

System.out.println(time.plusHours(-1));

// 加一个小时

System.out.println(time.plus(1L, ChronoUnit.HOURS));

// 加一个小时

System.out.println(time.plus(Duration.ofHours(1L)));

//9

System.out.println(time.get(ChronoField.HOUR_OF_DAY));

// 0 - 23

System.out.println(time.range(ChronoField.HOUR_OF_DAY));

// 2019-06-27T09:49:22.241

System.out.println(time.atDate(LocalDate.now()));

// 调整时间

System.out.println(time.adjustInto(LocalTime.now()));

上一篇下一篇

猜你喜欢

热点阅读