Java LocalDate LocalDateTime
在平常时开发过程中,总有很多奇奇怪怪的时间需要互相转换,比如 Date to String , Date to DateTime、String to Date、String to DateTime、Date to TimeStamp等等。

1、LocalDateTime to String(将LocalDateTime 转为自定义的时间格式字符串

  1. public static String localDateTimeToString(LocalDateTime localDateTime, String format) {
  2. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
  3. return localDateTime.format(formatter);
  4. }

2、timestamp to LocalDateTime (将long类型的timeStamp转为LocalDateTime )

  1. public static LocalDateTime timestampToLocalDateTime(long timestamp) {
  2. Instant instant = Instant.ofEpochMilli(timestamp);
  3. ZoneId zone = ZoneId.systemDefault();
  4. return LocalDateTime.ofInstant(instant, zone);
  5. }

3、LocalDateTime to timestamp ( 将LocalDateTime转为long类型的timeStamp )

  1. public static long localDateTimeToTimestamp(LocalDateTime localDateTime) {
  2. ZoneId zone = ZoneId.systemDefault();
  3. Instant instant = localDateTime.atZone(zone).toInstant();
  4. return instant.toEpochMilli();
  5. }

4、LocalDateTime to Date ( 将LocalDateTime to Date )

  1. public static Date localDateTimeToDate(LocalDateTime localDateTime) {
  2. ZoneId zone = ZoneId.systemDefault();
  3. Instant instant = localDateTime.atZone(zone).toInstant();
  4. return Date.from(instant);
  5. }

5、String to 自定义格式 ( Formatter ) 的LocalDateTime ( 将时间字符串转为自定义格式的LocalDateTime )

  1. public static LocalDateTime parseStringToDateTime(String time, String format) {
  2. DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
  3. return LocalDateTime.parse(time, df);
  4. }

6、Date to LocalDateTime ( 将Date转为LocalDateTime )

  1. public static LocalDateTime dateToLocalDateTime(Date date) {
  2. Instant instant = date.toInstant();
  3. ZoneId zone = ZoneId.systemDefault();
  4. return LocalDateTime.ofInstant(instant, zone);
  5. }

7、Date to LocalDate( 将Date 转为 LocalDate )

  1. public static LocalDate dateToLocalDate(Date date) {
  2. Instant instant = date.toInstant();
  3. ZoneId zone = ZoneId.systemDefault();
  4. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
  5. return localDateTime.toLocalDate();
  6. }

8、LocalDate to Date

  1. public static Date localDateToDate(LocalDate localDate) {
  2. ZoneId zone = ZoneId.systemDefault();
  3. Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
  4. return Date.from(instant);
  5. }

9、Date to LocalTime ( 将 Date 转为 LocalTime )

  1. public static LocalTime dateToLocalTime(Date date) {
  2. Instant instant = date.toInstant();
  3. ZoneId zone = ZoneId.systemDefault();
  4. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
  5. return localDateTime.toLocalTime();
  6. }

10、LocalTime to Date

  1. public static Date localTimeToDate(LocalTime localTime , LocalDate localDate) {
  2. LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
  3. ZoneId zone = ZoneId.systemDefault();
  4. Instant instant = localDateTime.atZone(zone).toInstant();
  5. return Date.from(instant);
  6. }

11、获取任意日期

  1. public static LocalDate anyDay(LocalDate localDate) {
  2. /**
  3. * 当月第一天-写法1
  4. */
  5. //第一天传入1,第二天2,第三天3,第四天4,第五天5,......
  6. LocalDate dayOfMonth = localDate.withDayOfMonth(2);
  7. /**
  8. * 当月第一天-写法2
  9. */
  10. LocalDate with = localDate.with(TemporalAdjusters.firstDayOfMonth());
  11. //当月最后一天
  12. LocalDate lastDate = localDate.with(TemporalAdjusters.lastDayOfMonth());
  13. //下一天
  14. LocalDate plusDays = dayOfMonth.plusDays(1);
  15. //前一天
  16. LocalDate minusDays = dayOfMonth.minusDays(1);
  17. // 取2014年1月第一个周一
  18. LocalDate firstMondayOf2014 = LocalDate.parse("2014-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
  19. return dayOfMonth;
  20. }