1、用DateTimeFormatter转换一个LocalDateTime类型
import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class TestDate1 { public static void main(String[] args) { //Get current date time LocalDateTime now = LocalDateTime.now(); System.out.println("Before : " + now); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formatDateTime = now.format(formatter); System.out.println("After : " + formatDateTime); }}
Before : 2016-11-09T11:44:44.797After : 2016-11-09 11:44:44
2、String转LocalDateTime
import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class TestDate2 { public static void main(String[] args) { String now = "2016-11-09 10:30"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); LocalDateTime formatDateTime = LocalDateTime.parse(now, formatter); System.out.println("Before : " + now); System.out.println("After : " + formatDateTime); System.out.println("After : " + formatDateTime.format(formatter)); }}
Before : 2016-11-09 10:30After : 2016-11-09T10:30After : 2016-11-09 10:30
3、timestamp转localDateTime
Timestamp time = new Timestamp(LocalDateTime.now());LocalDateTime startTime = time.toLocalDateTime();
4、long类型转timestamp类型
Timestamp time = new Timestamp((long)1525607567619);