Java 8 Date常用工具类

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11983108.html

 

Demo

  1 package org.fool.util;
  2 
  3 import java.time.Instant;
  4 import java.time.LocalDate;
  5 import java.time.LocalDateTime;
  6 import java.time.LocalTime;
  7 import java.time.ZoneId;
  8 import java.time.format.DateTimeFormatter;
  9 import java.util.Date;
 10 
 11 public class DateUtils {
 12     public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
 13     public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
 14     public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
 15 
 16     /**
 17      * get the current date
 18      */
 19     public static Date getCurrentDate() {
 20         return new Date();
 21     }
 22 
 23     /**
 24      * get the beginning of current date
 25      */
 26     public static Date getStartCurrentDate() {
 27         LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
 28         return localDateTimeToDate(start);
 29     }
 30 
 31     /**
 32      * get the ending of current date
 33      */
 34     public static Date getEndCurrentDate() {
 35         LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
 36         return localDateTimeToDate(end);
 37     }
 38 
 39     /**
 40      * get the previous date
 41      */
 42     public static Date getPreviousDate(long days) {
 43         LocalDate localDate = LocalDate.now().minusDays(days);
 44         return localDateToDate(localDate);
 45     }
 46 
 47     /**
 48      * get the future date
 49      */
 50     public static Date getFutureDate(long days) {
 51         LocalDate localDate = LocalDate.now().plusDays(days);
 52         return localDateToDate(localDate);
 53     }
 54 
 55     /**
 56      * LocalDate => Date
 57      */
 58     public static Date localDateToDate(LocalDate localDate) {
 59         return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
 60     }
 61 
 62     /**
 63      * LocalDateTime => Date
 64      */
 65     public static Date localDateTimeToDate(LocalDateTime localDateTime) {
 66         return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
 67     }
 68 
 69     /**
 70      * Date => LocalDate
 71      */
 72     public static LocalDate dateToLocalDate(Date date) {
 73         return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
 74     }
 75 
 76     /**
 77      * Date => LocalDateTime
 78      */
 79     public static LocalDateTime dateToLocalDateTime(Date date) {
 80         return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
 81     }
 82 
 83     /**
 84      * Date => String
 85      */
 86     public static String dateToString(Date date, String format) {
 87         DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
 88         return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().format(formatter);
 89     }
 90 
 91     /**
 92      * dateString => Date
 93      */
 94     public static Date dateStringToDate(String dateStr, String format) {
 95         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
 96         LocalDate localDate = LocalDate.parse(dateStr, dateTimeFormatter);
 97         return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
 98     }
 99 
100     /**
101      * dateTimeString => Date
102      */
103     public static Date dateTimeStringToDate(String dateTimeStr, String format) {
104         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
105         LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, dateTimeFormatter);
106         return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
107     }
108 
109     /**
110      * dateString => LocalDate
111      */
112     public static LocalDate dateStringToLocalDate(String dateStr, String format) {
113         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
114         return LocalDate.parse(dateStr, dateTimeFormatter);
115     }
116 
117     /**
118      * dateTimeString => LocalDate
119      */
120     public static LocalDateTime dateTimeStringToLocalDateTime(String dateTimeStr, String format) {
121         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
122         return LocalDateTime.parse(dateTimeStr, dateTimeFormatter);
123     }
124 }

 

 

Reference

https://howtodoinjava.com/java/date-time/localdatetime-to-date/

 

posted @ 2019-12-04 15:23  李白与酒  阅读(1648)  评论(0编辑  收藏  举报