优米格
分享有营养的

Java 8中使用LocalDate/LocalTime/LocalDateTime获取日期时间

BraUndress.png

1.获取年月日

示例代码:

// current date
LocalDate localDate = LocalDate.now();
System.out.println("Current date is:");
System.out.println(localDate.getYear());
System.out.println(localDate.getMonth());
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.get(ChronoField.MONTH_OF_YEAR));
// Date format
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(localDate.format(dateTimeFormatter));

// specific date
System.out.println("Specific date is:");
LocalDate specificDate = LocalDate.of(2018,10,23);
System.out.println(specificDate.format(dateTimeFormatter));

// TemporalAdjusters static method
LocalDate anothorLocalDate = localDate.with(firstDayOfYear());
System.out.println(anothorLocalDate.format(dateTimeFormatter));

打印结果如下:

Current date is:
2019
OCTOBER
23
10
2019-10-23
Specific date is:
2018-10-23
2019-01-01

2.获取时分秒

 // current time
LocalTime localTime = LocalTime.now();
System.out.println("Current time is:");
System.out.println(localTime.getHour());
System.out.println(localTime.getMinute());
System.out.println(localTime.get(ChronoField.MINUTE_OF_HOUR));
// Time format
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss");
System.out.println(localTime.format(dateTimeFormatter));

// specific time
System.out.println("Specific time is:");
LocalTime specificTime = LocalTime.of(9,35,23);
System.out.println(specificTime.format(dateTimeFormatter));

3.获取年月日时分秒

 // current time
LocalDateTime localDateTime = LocalDateTime.now();
// Time format
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
System.out.println(localDateTime.format(dateTimeFormatter));

4.日期加减

LocalDate localDate = LocalDate.now();
localDate = localDate.minusDays(7);//减7天

// 日期差(按月计算)
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
LocalDate startDate = LocalDate.parse(parameters.get("yearMonthS"), dateTimeFormatter);
LocalDate endDate = LocalDate.parse(parameters.get("yearMonthE"), dateTimeFormatter);
ChronoUnit.MONTHS.between(startDate, endDate)

5.日期格式化

## String转LocalDateTime
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05",df);

## String转LocalDate
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate ld = LocalDate.parse("2017-09-28",df);

## LocalDate转String
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now();
System.out.println(localDate.format(dateTimeFormatter));

## LocalDateTime转String
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
System.out.println(localDateTime.format(dateTimeFormatter));

## LocalDateTime转LocalDate
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();

## LocalDate转LocalDateTime(3种方式)
LocalDate localDate = LocalDate.parse("2019-01-04");
LocalDateTime localDateTime1 = localDate.atStartOfDay();
LocalDateTime localDateTime2 = localDate.atTime(LocalTime.now());
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = localDate.atTime(04, 30, 56);
System.out.println(localDateTime3);

References

  1. Class TemporalAdjusters
  2. 为什么不建议使用Date,而是使用Java8新的时间和日期API?
  3. Class DateTimeFormatter
  4. Java 8中处理日期和时间示例
赞(0)
未经允许禁止转载:优米格 » Java 8中使用LocalDate/LocalTime/LocalDateTime获取日期时间

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系