1. 程式人生 > >java日期互轉:LocalDateTime、String、TimeStamp、Long、Instant、Date

java日期互轉:LocalDateTime、String、TimeStamp、Long、Instant、Date

mat new t class 只有一個 一個 int 引擎 日期格 simple

由於java版本的叠代,一個使用java開發的項目中可能出現多種日期對象,例如LocalDateTime、LocalDate、Date,不像C#只有一個DateTime,因此在各種日期格式或者對象之間的轉換顯得有點復雜,總是記不住,在需要用到時總是需要依靠搜索引擎,有點浪費時間,所以特意把常用的轉換場景總結如下:

1、 LocalDateTime轉為String、TimeStamp、Long、Instant、 Date

        System.out.println("----------------LocalDateTime----------------");
        //LocalDateTime -> String
        String localDateTimeToString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("LocalDateTime -> String:  " + localDateTimeToString);
        //LocalDateTime -> TimeStamp
        Timestamp localDateTimeToTimeStamp = Timestamp.valueOf(LocalDateTime.now());
        System.out.println("LocalDateTime -> TimeStamp:  " + localDateTimeToTimeStamp);
        //LocalDateTime -> Long
        Long localDateTimeToLong = Timestamp.valueOf(LocalDateTime.now()).getTime();
        System.out.println("LocalDateTime -> Long:  " + localDateTimeToLong);
        //LocalDateTime -> Instant
        Instant localDateTimeToInstant = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();
        System.out.println("LocalDateTime -> Instant:  " + localDateTimeToInstant);
        //LocalDateTime -> Date
        Date LocalDateTimeToDate = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("LocalDateTime -> Date:  " + LocalDateTimeToDate);

2、String轉為LocalDateTime、 Date

        System.out.println("----------------String----------------");
        //String -> LocalDateTime
        LocalDateTime stringToLocalDateTime =
                LocalDateTime.parse("2018-03-11 15:30:11", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("String -> LocalDateTime:  " + stringToLocalDateTime);
        //String -> Date
        try {
            Date stringToDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2018-03-11 15:30:11");
            System.out.println("String -> Date:  " + stringToDate);
        } catch (ParseException ex) {

        }

3、Timestamp轉為LocalDateTime、 Date

        System.out.println("---------------Timestamp-----------------");
        //Timestamp -> LocalDateTime
        LocalDateTime timeStampToLocalDateTime =
                LocalDateTime.ofInstant(new Timestamp(1520754566856L).toInstant(), ZoneId.systemDefault());
        System.out.println("Timestamp -> LocalDateTime:  " + timeStampToLocalDateTime);
        //Timestamp -> Date
        Date timestampToDate = Date.from(new Timestamp(1520754566856L).toInstant());
        System.out.println("Timestamp -> Date:  " + timestampToDate);

4、Long轉為LocalDateTime、 Date

        System.out.println("---------------Long-----------------");
        //Long -> LocalDateTime
        LocalDateTime longToLocalDateTime =
                LocalDateTime.ofInstant(Instant.ofEpochMilli(1520754566856L), ZoneId.systemDefault());
        System.out.println("Long -> LocalDateTime:  " + longToLocalDateTime);
        //Long -> Date
        Date longToDate = new Date(1520754566856L);
        System.out.println("Long -> Date: " + longToDate);

5、Instant轉為LocalDateTime、 Date

        System.out.println("----------------Instant----------------");
        //Instant -> LocalDateTime
        LocalDateTime instantToLocalDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
        System.out.println("Instant -> LocalDateTime:  " + instantToLocalDateTime);
        //Instant -> Date
        Date instantToDate = Date.from(Instant.now());
        System.out.println("Instant -> Date:  " + instantToDate);

6、Date轉為LocalDateTime、String、TimeStamp、Long、Instant

        System.out.println("----------------Date----------------");
        //Date -> String
        String dateToString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("Date -> String:  " + dateToString);
        //Date -> LocalDateTime
        LocalDateTime dateToLocalDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
        System.out.println("Date -> LocalDateTime:  " + dateToLocalDateTime);
        //Date -> Timestamp
        Timestamp dateToTimestamp = new Timestamp(new Date().getTime());
        System.out.println("Date -> Timestamp:  " + dateToTimestamp);
        //Date -> Long
        Long dateToLong = new Date().getTime();
        System.out.println("Date -> Long: " + dateToLong);
        //Date -> Instant
        Instant dateToInstant = new Date().toInstant();
        System.out.println("Date -> Instant:  " + dateToInstant);
 

  

java日期互轉:LocalDateTime、String、TimeStamp、Long、Instant、Date