Java各种日期类间的互转

LocalDateTime取代Date的原因

日期的大小比较及差值计算

一、LocalDateTime转换

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateConvert {

    public static void main(String[] args) {

        System.out.println("-----LocalDateTime转为String、TimeStamp、Long、Instant、Date-----");
        LocalDateTime now = LocalDateTime.now();
        System.out.println("LocalDateTime.now():" + now);//2008-08-08T08:08:08.008
        //LocalDateTime转String
        String ldtToStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("LocalDateTime -> String:  " + ldtToStr);
        //LocalDateTime转Timestamp
        Timestamp ldtToTimeStamp = Timestamp.valueOf(now);
        System.out.println("LocalDateTime -> TimeStamp:  " + ldtToTimeStamp);
        //LocalDateTime转Long(毫秒数)
        Long ldtToLong = Timestamp.valueOf(now).getTime();
        System.out.println("LocalDateTime -> Long:  " + ldtToLong);
        //LocalDateTime转Instant  2008-08-08T08:08:08.008Z
        Instant ldtToInstant = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();
        System.out.println("LocalDateTime -> Instant:  " + ldtToInstant);
        //LocalDateTime转Instant         
        Date ldtToDate = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("LocalDateTime -> Date:  " + ldtToDate);

        System.out.println("----------------String转为LocalDateTime、 Date----------------");
        LocalDateTime strToLDT = LocalDateTime.parse("2008-08-08 20:08:08", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("String -> LocalDateTime:  " + strToLDT);
        try {
            Date strToDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2008-08-08 20:08:08");
            System.out.println("String -> Date:  " + strToDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println("---------------Timestamp转为LocalDateTime、 Date-----------------");
        LocalDateTime timeStampToLDT = LocalDateTime.ofInstant(new Timestamp(1564198800772L).toInstant(), ZoneId.systemDefault());
        System.out.println("Timestamp -> LocalDateTime:  " + timeStampToLDT);
        Date timestampToDate = Date.from(new Timestamp(1564198800772L).toInstant());
        System.out.println("Timestamp -> Date:  " + timestampToDate);

        System.out.println("---------------Long转为LocalDateTime、 Date-----------------");
        LocalDateTime longToLDT = LocalDateTime.ofInstant(Instant.ofEpochMilli(1564198800772L), ZoneId.systemDefault());
        System.out.println("Long -> LocalDateTime:  " + longToLDT);
        Date longToDate = new Date(1564198800772L);
        System.out.println("Long -> Date: " + longToDate);

        System.out.println("----------------Instant转为LocalDateTime、 Date----------------");
        LocalDateTime instantToLDT = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
        System.out.println("Instant -> LocalDateTime:  " + instantToLDT);
        Date instantToDate = Date.from(Instant.now());
        System.out.println("Instant -> Date:  " + instantToDate);

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

二、Date与LocalDate的互转

import java.time.*;
import java.util.Date;

public class DateConvert {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("date2LocalDate:" + date2LocalDate(date));
        LocalDate localDate = LocalDate.now();
        System.out.println("localDate2Date:" + localDate2Date(localDate));
    }
    public static LocalDate date2LocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
    public static Date localDate2Date(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }
}

你可能感兴趣的:(Java各种日期类间的互转)