您的位置:首页 > 编程语言 > Java开发

JAVA8新特性[第六季]-新时间日期 API

2017-08-20 23:49 543 查看
一 使用 LocalDateLocalTimeLocalDateTime
1 案例

二 Instant 时间戳
1 案例

三Duration 和 Period
案例

四日期的操作
1 案例

五解析与格式化
1 案例

六 时区的处理
1 案例

一、 使用 LocalDate、LocalTime、LocalDateTime

LocalDate、LocalTime、LocalDateTime 类的 实例 是不可变的对象,分别表示使用 ISO-8601日 历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。

注:ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法

方法描述示例
now()静态方法,根据当前时间创建对象LocalDate localDate =LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
of()静态方法,根据指定日期/时间创建 对象LocalDate localDate = LocalDate.of(2016, 10, 26);
LocalTime localTime = LocalTime.of(02, 22, 56);
LocalDateTime localDateTime = LocalDateTime.of(2016, 10, 26, 12, 10, 55);
plusDays,
plusWeeks,
plusMonths,
plusYears
向当前 LocalDate 对象添加
几天、
几周、
几个月、
几年
minusDays,
minusWeeks,
minusMonths,
minusYears
从当前 LocalDate 对象减去
几天、
几周、
几个月、
几年
plus,
minus
添加或减少一个 Duration 或 Period
withDayOfMonth,
withDayOfYear,
withMonth,
withYear
将月份天数、年份天数、月份、年 份修改为指定的值并返回新的 LocalDate 对象
getDayOfMonth获得月份天数(1-31)
etDayOfYear获得年份天数(1-366)
getDayOfWeek获得星期几(返回一个 DayOfWeek 枚举值)
getMonth获得月份, 返回一个 Month 枚举值
getMonthValue获得月份(1-12)
getYear获得年份
until获得两个日期之间的 Period 对象, 或者指定 ChronoUnits 的数字
isBefore,
isAfter
比较两个 LocalDate
isLeapYear判断是否是闰年

1.1 案例:

@Test
public void test1() {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);

System.out.println("-----------------");

LocalDateTime localDateTime1 = LocalDateTime.of(2016, 8, 20, 03, 12, 25);
System.out.println(localDateTime1);
System.out.println("年:" + localDateTime1.getYear());
System.out.println("月:" + localDateTime1.getMonth().getValue());
System.out.println("月日:" + localDateTime1.getDayOfMonth());
System.out.println("周日:" + localDateTime1.getDayOfWeek().getValue());
System.out.println("年日:" + localDateTime1.getDayOfYear());
System.out.println("小时:" + localDateTime1.getHour());
System.out.println("分钟:" + localDateTime1.getMinute());
System.out.println("秒:" + localDateTime1.getSecond());

// 加5天
LocalDateTime localDateTime2 = localDateTime1.plusDays(5);
System.out.println(localDateTime2);

//前一年
LocalDateTime localDateTime3 = localDateTime1.minusYears(1);
System.out.println(localDateTime3);

//修改为几月
LocalDateTime localDateTime4 = localDateTime1.withMonth(9);
System.out.println(localDateTime4);

//比较两个日期
boolean isBefore = localDateTime1.isBefore(localDateTime3);
System.out.println(isBefore);

//是否闰年
boolean isLeapYear = LocalDate.now().isLeapYear();
System.out.println(isLeapYear);
}


执行结果:

2017-08-22T23:07:32.690
-----------------
2016-08-20T03:12:25
年:2016
月:8
月日:20
周日:6
年日:233
小时:3
分钟:12
秒:25
2016-08-25T03:12:25
2015-08-20T03:12:25
2016-09-20T03:12:25
false
false


二、 Instant 时间戳

用于“时间戳”的运算。它是以Unix元年(传统 的设定为UTC时区1970年1月1日午夜时分)开始 所经历的描述进行运算

2.1 案例:

/**
* Instant : 时间戳(按 Unix 元年 1970年1月1日 00:00:00 所经历的毫秒进行计算)
*/
@Test
public void test2(){
Instant instant = Instant.now();// 默认获取按 UTC 的世界时间
System.out.println(instant);

System.out.println("-------------");

OffsetDateTime offsetDateTime = Instant.now().atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);

System.out.println("-------------");

Instant epochSecond = Instant.ofEpochSecond(5);
System.out.println(epochSecond);

System.out.println("-------------");

Instant ofEpochSecond = Instant.ofEpochSecond(1, 1000000000);
System.out.println(ofEpochSecond);

System.out.println("-------------");

int ins = instant.get(ChronoField.NANO_OF_SECOND);
System.out.println(ins);

System.out.println("-------------");

long epochMilli = instant.toEpochMilli();
System.out.println(epochMilli);
}


执行结果:

2017-08-22T15:11:55.070Z
-------------
2017-08-22T23:11:55.160+08:00
-------------
1970-01-01T00:00:05Z
-------------
1970-01-01T00:00:02Z
-------------
70000000
-------------
1503414715070


三、Duration 和 Period

Duration:用于计算两个“时间”间隔

Period:用于计算两个“日期”间隔

案例:

1.Duration

/*
* Duration : 计算两个“时间”的间隔
*/
@Test
public  void test3(){
LocalTime localTime = LocalTime.now();
LocalTime localTime1 = LocalTime.of(22, 22, 22);

Duration duration = Duration.between(localTime1, localTime);
System.out.println(duration);
System.out.println(duration.getSeconds());
System.out.println(duration.getNano());
System.out.println(duration.toHours());
System.out.println(duration.toMillis());
System.out.println(duration.toMinutes());
System.out.println(duration.toDays());
}


执行结果:

PT52M4.732S
3124
732000000
0
3124732
52
0


2.Period

/**
* Period : 计算两个“日期”的间隔
*/
@Test
public  void test4(){
LocalDate localDate = LocalDate.now();
LocalDate localDate1 = LocalDate.of(2017, 8, 29);

Period period = Period.between(localDate1, localDate);
System.out.println(period);

System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.getYears());
}


执行结果:

P-7D
-7
0
0


四、日期的操作

TemporalAdjuster : 时间校正器。有时我们可能需要获 取例如:将日期调整到“下个周日”等操作。

TemporalAdjusters : 该类通过静态方法提供了大量的常 用 TemporalAdjuster 的实现。

4.1 案例:

/**
* TemporalAdjusters: 时间校正器
*/
@Test
public  void  test5(){
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime dateTime = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));// 下个周日
System.out.println(dateTime);

System.out.println("---------------");

LocalDateTime localDateTime2 = localDateTime.with((t) -> {
LocalDateTime localDateTime1 = (LocalDateTime) t;
DayOfWeek dayOfWeek = localDateTime1.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return localDateTime1.plusDays(3);
} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
return localDateTime1.plusDays(2);
} else {
return localDateTime1.plusDays(1);
}
});
System.out.println(localDateTime2);
}


执行结果:

2017-08-27T23:19:57.904
---------------
2017-08-23T23:19:57.904


五、解析与格式化

java.time.format.DateTimeFormatter 类:该类提供了三种 格式化方法:

1. 预定义的标准格式

2. 语言环境相关的格式

3. 自定义的格式

5.1 案例:

/**
* DateTimeFormatter : 用于日期时间解析格式化
*/
@Test
public void test6(){
//DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");
LocalDateTime localDateTime = LocalDateTime.now();
String dateTime = localDateTime.format(dateTimeFormatter);//格式化
System.out.println(dateTime);

System.out.println("----------------");

LocalDateTime parse = LocalDateTime.parse(dateTime, dateTimeFormatter); // 解析
System.out.println(parse);
}


执行结果:

2017年08月22日 23:25:13 星期二
----------------
2017-08-22T23:25:13


六、 时区的处理

java8 中加入了对时区的支持,带时区的时间为分别为: ZonedDate、ZonedTime、ZonedDateTime,其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式 例如 :Asia/Shanghai 等。

ZoneId:该类中包含了所有的时区信息

1. getAvailableZoneIds() : 可以获取所有时区时区信息

2. of(id) : 用指定的时区信息获取 ZoneId 对象

6.1 案例:

1.获取所有时区id

@Test
public  void test7(){
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
availableZoneIds.forEach(System.out::println);
System.out.println(availableZoneIds.size());
}


执行结果:

Asia/Aden
America/Cuiaba
Etc/GMT+9
Etc/GMT+8
Africa/Nairobi
America/Marigot
....... 太多了.......


2.带时区的时间日期

@Test
public  void test8(){
LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Europe/Monaco"));
System.out.println(zonedDateTime);

System.out.println("------------");

LocalDateTime dateTime = LocalDateTime.now(ZoneId.of("Europe/Monaco"));
System.out.println(dateTime);
}


执行结果:

2017-08-22T23:32:11.143+02:00[Europe/Monaco]
------------
2017-08-22T17:32:11.154
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java