LocalDateTime、Date、String和时间戳Long互相转换

时间:2025-04-02 06:59:06
import ;
import ;
import ;
import ;
import ;
import ;

public class DateUtils {


    public static void main(String[] args) {
        //LocalDateTime转时间戳
        // 获取秒数
        long l = ().toEpochSecond(("+8"));
        (l);
        // 获取毫秒数
        long l1 = ().toInstant((+8)).toEpochMilli();
        (l1);

        // 时间戳转LocalDateTime
        //1640878737155
        LocalDateTime localDateTime = ((1640878737155L), ("+8"));
        (());

        //LocalDateTime 转String
        DateTimeFormatter dateTimeFormatter = (FORMAT_ONE);
        String now = ().format(dateTimeFormatter);
        (now);

        // String 转LocalDateTime
        LocalDateTime string2LocalDateTime = ("2020-12-12 12:12:12", dateTimeFormatter);
        (string2LocalDateTime);

        // LocalDateTime转Date
        Date date = new Date(().toInstant((+8)).toEpochMilli());;
        (date);

        //Date 转LocalDateTime
        LocalDateTime localDateTime1 = new Date().toInstant().atOffset(("+8")).toLocalDateTime();
        LocalDateTime localDateTime2 = (new Date().toInstant(), ("+8"));
        (localDateTime1);
        (localDateTime2);


    }

}