抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Java 8 new date time API

编号 类的名称 描述
1 Instant 时间戳
2 Duration 持续时间,时间差
3 LocalDate 只包含日期,比如 2018-02-05
4 LocalTime 只包含时间,比如 23:12:10
5 LocalDateTime 包含日期和时间,比如 2018-02-05 23:14:21
6 Period 时间段
7 ZoneId 时区
8 ZoneOffset 时区偏移量,比如 +8:00
9 ZonedDateTime 带时区的时间
10 Clock 时钟,比如获取目前美国纽约的时间
11 java.time.format.DateTimeFormatter 时间格式化

Instant 时间戳

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.smobob.datetime;

import java.time.Instant;

/**
* Instant类在Java日期与时间功能中,表示了时间线上一个确切的点,
* 定义为距离初始时间的时间差(初始时间为GMT 1970年1月1日00:00)经测量一天有86400秒,
* 从初始时间开始不断向前移动。
*/
public class InstantExample {

public static void main(String[] args) {
// 创建一个Instant实例
Instant now = Instant.now();

// 访问Instant的时间
//距离初始时间的秒钟数
long seconds = now.getEpochSecond();
System.out.println("seconds: " + seconds);
//在当前一秒内的第几纳秒
int nanos = now.getNano();
System.out.println("nanos: " + nanos);

//当前时间
System.out.println("current: " + now.toString());
// 3秒后
Instant later = now.plusSeconds(3);
System.out.println("later: " + later);
// 3秒前
Instant earlier = now.minusSeconds(3);
System.out.println("earlier: " + earlier);
}

}

Duration 持续时间,时间差

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.smobob.datetime;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;

public class DurationExample {

public static void main(String[] args) throws InterruptedException {
// 创建Duration实例
Instant first = Instant.now();
Thread.sleep(3000);
Instant second = Instant.now();
Duration duration = Duration.between(first, second);
// 访问Duration的时间
long seconds = duration.getSeconds();
System.out.println("相差: " + seconds + " 秒");

LocalDateTime from = LocalDateTime.now();
LocalDateTime to = from.plusDays(5);
Duration duration2 = Duration.between(from, to);
System.out.println("从 from 到 to 相差: " + duration2.toDays() + " 天");
}

}

LocalDate 只包含日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.smobob.datetime;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;

public class LocalDateExample {

public static void main(String[] args) {
// 创建一个LocalDate实例
LocalDate localDate = LocalDate.now();

int year = localDate.getYear();
Month month = localDate.getMonth();
int dayOfMonth = localDate.getDayOfMonth();
int dayOfYear = localDate.getDayOfYear();
DayOfWeek dayOfWeek = localDate.getDayOfWeek();

System.out.println("year: " + year);
System.out.println("month: " + month.getValue());
System.out.println("dayOfMonth: " + dayOfMonth);
System.out.println("dayOfYear: " + dayOfYear);
System.out.println("dayOfWeek: " + dayOfWeek.getValue());

// 使用年月日信息构造出LocalDate对象
LocalDate localDate2 = LocalDate.of(2018, 3, 3);
// 3年后
LocalDate d1 = localDate2.plusYears(3);
System.out.println("plusYears: " + d1);
// 3年前
LocalDate d2 = localDate2.minusYears(3);
System.out.println("minusYears: " + d2);
}

}

LocalTime 只包含时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.smobob.datetime;

import java.time.LocalTime;

public class LocalTimeExample {

public static void main(String[] args) {
// 创建一个LocalTime实例
LocalTime localTime = LocalTime.now();

// 使用指定的时分秒和纳秒来新建对象
LocalTime localTime2 = LocalTime.of(21, 30, 59, 11001);

// 3小时后
LocalTime localTimeLater = localTime.plusHours(3);
// 3小时前
LocalTime localTimeEarlier = localTime.minusHours(3);

System.out.println("localTime: " + localTime);
System.out.println("localTime2: " + localTime2);
System.out.println("localTimeLater: " + localTimeLater);
System.out.println("localTimeEarlier: " + localTimeEarlier);
}

}

LocalDateTime 包含日期和时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.smobob.datetime;

import java.time.LocalDateTime;

public class LocalDateTimeExample {

public static void main(String[] args) {
// 创建一个LocalDateTime实例
LocalDateTime localDateTime = LocalDateTime.now();

// 使用指定的年月日、时分秒、纳秒来新建对象
LocalDateTime localDateTime2 = LocalDateTime.of(2018, 11, 26, 13, 55, 36, 123);

// 3年后的现在
LocalDateTime dt1 = localDateTime.plusYears(3);
// 3年前的现在
LocalDateTime dt2 = localDateTime.minusYears(3);

System.out.println("localDateTime: " + localDateTime);
System.out.println("localDateTime2: " + localDateTime2);
System.out.println("dt1: " + dt1);
System.out.println("dt2: " + dt2);
}

}

Period 时间段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.smobob.datetime;

import java.time.LocalDate;
import java.time.Period;

public class PeriodExample {

public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate java8Release = LocalDate.of(2018, 12, 14);
Period periodToNextJavaRelease = Period.between(today, java8Release);
System.out.println("Months left between today and Java 8 release : " + periodToNextJavaRelease.getMonths());
}

}

ZoneId 时区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.smobob.datetime;

import java.time.ZoneId;
import java.util.TimeZone;

public class ZoneIdExample {

public static void main(String[] args) {
// 获取系统默认时区
ZoneId defaultZoneId = ZoneId.systemDefault();
System.out.println(defaultZoneId);

//接收一个“区域/城市”的字符串作为参数获取时区
ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
System.out.println(shanghaiZoneId);

// TimeZone 转换为 ZoneId
ZoneId oldToNewZoneId = TimeZone.getDefault().toZoneId();
System.out.println(oldToNewZoneId);

//获取所有合法的“区域/城市”字符串
System.out.println(ZoneId.getAvailableZoneIds());
}

}

ZoneOffset 时区偏移量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.smobob.datetime;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class ZoneOffsetExample {

public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime: " + localDateTime);

OffsetDateTime odt = OffsetDateTime.now();
ZoneOffset zoneOffset1 = odt.getOffset();
System.out.println("zoneOffset1: " + zoneOffset1);
OffsetDateTime offsetDateTime1 = OffsetDateTime.of(localDateTime, zoneOffset1);
System.out.println("offsetDateTime1: " + offsetDateTime1);

ZoneOffset zoneOffset2 = ZoneOffset.of("+09:00");
System.out.println("zoneOffset2: " + zoneOffset2);
OffsetDateTime offsetDateTime2 = OffsetDateTime.of(localDateTime, zoneOffset2);
System.out.println("offsetDateTime2: " + offsetDateTime2);
}

}

ZonedDateTime 带时区的时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.smobob.datetime;

import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeExample {

public static void main(String[] args) {
// 创建一个ZonedDateTime实例
ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println("dateTime: " + dateTime);
System.out.println("zoneDateTime : " + dateTime);

// 使用指定的年月日、时分秒、纳秒以及时区ID来新建对象
ZoneId zoneId = ZoneId.of("UTC+8");
ZonedDateTime dateTime2 = ZonedDateTime.of(2018, 3, 8, 23, 45, 59, 1234, zoneId);

// 3天后
ZonedDateTime zoneDateTime = dateTime2.plus(Period.ofDays(3));
System.out.println("zoneDateTime : " + zoneDateTime);

ZoneId zoneId2 = ZoneId.of("Europe/Copenhagen");
ZoneId zoneId3 = ZoneId.of("Europe/Paris");

System.out.println("zoneId2: " + zoneId2);
System.out.println("zoneId3: " + zoneId3);
}

}

Clock 时钟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.smobob.datetime;

import java.time.Clock;
import java.time.Instant;
import java.util.Date;

public class ClockExample {

public static void main(String[] args) {
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
Instant instant = clock.instant();
Date legacyDate = Date.from(instant);
System.out.println(millis);
System.out.println(legacyDate);
}

}

DateTimeFormatter 时间格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.smobob.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class DateTimeFormatExample {

public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
String formattedDate = formatter.format(LocalDate.now());
String formattedZonedDate = formatter.format(ZonedDateTime.now());

System.out.println("LocalDate: " + formattedDate);
System.out.println("formattedZonedDate: " + formattedZonedDate);

LocalDateTime dateTime = LocalDateTime.now();
// 20180303
String strDate1 = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);
// 2013-03-03
String strDate2 = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
// 当前时间
String strDate3 = dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME);
// 2018-03-03
String strDate4 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// 今天是:2018年 三月 03日 星期六
String strDate5 = dateTime.format(DateTimeFormatter.ofPattern("今天是:YYYY年 MMMM dd日 E", Locale.CHINESE));

System.out.println(strDate1);
System.out.println(strDate2);
System.out.println(strDate3);
System.out.println(strDate4);
System.out.println(strDate5);

// 将一个字符串解析成一个日期对象
String strDate6 = "2018-03-03";
String strDate7 = "2017-03-03 15:30:05";
LocalDate date = LocalDate.parse(strDate6, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(date);
LocalDateTime dateTime1 = LocalDateTime.parse(strDate7, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(dateTime1);
}

}

日期转换示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.smobob.datetime;

import java.time.*;
import java.util.Date;

public class ConvertExample {

/**
* LocalDate -> Date
*
* @param localDate
* @return
*/
public static Date toDate(LocalDate localDate) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
return Date.from(instant);
}

/**
* LocalDateTime -> Date
*
* @param localDateTime
* @return
*/
public static Date toDate(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}

/**
* LocalTime -> Date
*
* @param localTime
* @return
*/
public static Date toDate(LocalTime localTime) {
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}

/**
* Date -> LocalDate
*
* @param date
* @return
*/
public static LocalDate toLocalDate(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime.toLocalDate();
}

/**
* Date -> LocalDateTime
*
* @param date
* @return
*/
public static LocalDateTime toLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}

/**
* Date -> LocalTime
*
* @param date
* @return
*/
public static LocalTime toLocalTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime.toLocalTime();
}

}