import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class MainThread {
public static void main(String[] args) {
timestamp();
}
private static void timestamp(){
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
long timestamp = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 转换成时间戳
System.out.println(timestamp);
System.out.println(LocalDateTime.ofInstant(new Date(timestamp).toInstant(),ZoneId.of("Asia/Shanghai")));
}
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class MainThread {
public static void main(String[] args) {
timestamp();
}
private static void timestamp(){
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
long timestamp = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 转换成时间戳
System.out.println(timestamp);
System.out.println(LocalDateTime.ofInstant(new Date(timestamp).toInstant(),ZoneId.of("Asia/Shanghai")));
}
}
执行结果:
Connected to the target VM, address: '127.0.0.1:52743', transport: 'socket'
2018-06-26T15:06:07.804
1529996767804
2018-06-26T15:06:07.804
Disconnected from the target VM, address: '127.0.0.1:52743', transport: 'socket'