JDK8 自定义日期格式转换为时间戳

import com.sun.org.apache.bcel.internal.generic.LLOAD;

import javax.tools.JavaCompiler;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;

/**
 * @version v1.0
 * @ProjectName: cshjavatest
 * @ClassName: TestLocalDateTime
 * @Description: TODO(一句话描述该类的功能)
 * @Author: CSH
 * @Date: 2020/5/1 16:39
 */
public class TestLocalDateTime {
    public static void main(String[] args) {

        /***
         * 自定义日期格式转换为时间戳
         */
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2018-05-29 13:52:50", dateTimeFormatter);
        long l = LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
//        long l2 = Instant.from(parse).toEpochMilli();
        System.out.println(l);
        java.sql.Date sDate=new java.sql.Date(l);
        System.out.println(sDate);
        java.sql.Timestamp jsTimestamp=new java.sql.Timestamp(System.currentTimeMillis());
        System.out.println(jsTimestamp);
    }
}

 

你可能感兴趣的:(JDK8 自定义日期格式转换为时间戳)