java8 Date相关工具

自己整理的java8 常用的工具类
  1 /**
  2  * @Author taozhaoyang
  3  * @Description 结合 java8 线程安全
  4  * @Date 9:55 2018/7/30
  5  **/
  6 public class NewDateUtil {
  7 
  8     private static DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  9 
 10     /**
 11      * 时间戳 转 date(String)
 12      *
 13      * @param time 毫秒
 14      * @return
 15      */
 16     public static String formatTimestamp(Long time) {
 17         return sdf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
 18     }
 19 
 20     /**
 21      * @Author taozhaoyang
 22      * @Description  localDateTime 转 Date
 23      * @Date 20:07 2018/7/30
 24      * @Param [localDateTime]
 25      * @return java.util.Date
 26      **/
 27     public static Date  covertLocalDateTimeToDate (LocalDateTime localDateTime) {
 28         return  Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
 29     }
 30 
 31     /**
 32      * @Author taozhaoyang
 33      * @Description localDateTime 转 String
 34      * @Date 20:07 2018/7/30
 35      * @Param [localDateTime]
 36      * @return java.lang.String
 37      **/
 38     public static String  formatLocalDateTime (LocalDateTime localDateTime) {
 39         return formatDate(covertLocalDateTimeToDate(localDateTime));
 40     }
 41 
 42     /**
 43      * 时间 转 date(String)
 44      *
 45      * @param date
 46      * @return
 47      */
 48     public static String formatDate(Date date) {
 49         return sdf.format(covertDateToLocalDateTime(date));
 50     }
 51 
 52     public static LocalDateTime  covertDateToLocalDateTime (Date date) {
 53         return  LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
 54     }
 55     /**
 56      * string 转date
 57      *
 58      * @param date
 59      * @return
 60      */
 61     public static Date parseString(String date) {
 62         return Date.from(LocalDateTime.parse(date, sdf).atZone(ZoneId.systemDefault()).toInstant());
 63     }
 64 
 65     /**
 66      * @Author taozhaoyang
 67      * @Description 比较相差多少天,只比较天数
 68      * @Date 20:30 2018/7/30
 69      * @Param [startDate, endDate]
 70      * @return long
 71      **/
 72     public static long compareDates(Date startDate,Date endDate) {
 73         return covertDateToLocalDateTime(endDate).toLocalDate().until(covertDateToLocalDateTime(endDate).toLocalDate(), ChronoUnit.DAYS);
 74     }
 75 
 76     /**
 77      * @Author taozhaoyang
 78      * @Description 今天 还剩多少秒
 79      * @Date 20:35 2018/7/30
 80      * @Param [startDate, endDate]
 81      * @return long
 82      **/
 83     public static long remianSecondsOfToday() {
 84         return LocalDateTime.now().until(LocalDateTime.of(LocalDate.now(),LocalTime.MAX), ChronoUnit.SECONDS);
 85     }
 86 
 87     /**
 88      * @Author taozhaoyang
 89      * @Description 和当前相差多少天
 90      * @Date 20:31 2018/7/30
 91      * @Param [endDate]
 92      * @return long
 93      **/
 94     public static long compareToDay(Date endDate) {
 95         return LocalDate.now().until(covertDateToLocalDateTime(endDate), ChronoUnit.DAYS);
 96     }
 97 
 98     public static void main(String[] args) {
 99         System.out.println("----------------------");
100         long time = 1532353895000L;
101         System.out.println(formatTimestamp(time));
102 
103         Date date = new Date();
104         System.out.println(formatDate(date));
105 
106         String str = "2018-07-23 21:51:35";
107         System.out.println(parseString(str));
108         System.out.println("----------------------");
109 
110         System.out.println(formatLocalDateTime(LocalDateTime.now()));
111 
112         System.out.println(compareToDay(parseString(str)));
113         System.out.println(remianSecondsOfToday());
114     }
115 }
116  
View Code

 

 
posted @ 2018-07-31 15:22  谢幕ゾ华丽  阅读(524)  评论(0编辑  收藏  举报