Java DateTime API

1 Date/SimpleDateFormat

Date now = new Date();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String nowStr = df.parse(now);

Date then = df.format("2001-12-31 11:33:44");

2 Calender

Calender c = Calender.getInstance(); // GregorianCalendar
Calender c = new GregorianCalendar();

Date d = c.getTime();

3 JodaTime

<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.1</version>
</dependency>

4 LocalTime/LocalDate/LocalDateTime

// 获取今天时间
LocalDateTime now = LocalDateTime.now();
LocalDateTime then = LocalDateTime.of(2018, 10, 12, 22, 1, 1);

// 格式化时间
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-mm-dd hh:mm:ss");
LocalDateTime.now().format(dtf);

Author: unname

Created: 2019-03-22 周五 01:32

Go ahead, never stop.