Date/Time API

Date/Time API

使用LocalDate、LocalTime、LocalDateTime、Instant、Period和Duration创建和管理基于日期和基于时间的事件,包括将日期和时间组合到单个对象中

跨时区处理日期和时间,并管理由夏令时产生的更改,包括设置日期和时间值的格式

使用Instant、Period、Duration和TemporalUnit定义、创建和管理基于日期和时间的事件


The java.time package consists of four subpackages:
•     java.time.temporal—Accesses date/time fields and units
•     java.time.format—Formats the input and output of date/time objects
•     java.time.zone—Handles time zones
•     java.time.chrono—Supports calendar systems such as Japanese and Thai calendars

LocalDate today = LocalDate.now();
System.out.println("Today's date is: " + today);

LocalDate newYear2016 = LocalDate.of(2016, 1, 1);
System.out.println("New year 2016: " + newYear2016);


1、Instant类型指的是时间戳

Listing 8-1. UsingInstant.java
import java.time.Instant;
public class UsingInstant {
 public static void main(String args[]){
 // prints the current timestamp with UTC as time zone
 Instant currTimeStamp = Instant.now();
 System.out.println("Instant timestamp is: "+ currTimeStamp);
 
 // prints the number of seconds as Unix timestamp from epoch time
 System.out.println("Number of seconds elapsed: " + currTimeStamp.getEpochSecond());
 // prints the Unix timestamp in milliseconds
 System.out.println("Number of milliseconds elapsed: " + currTimeStamp.toEpochMilli());
 }
}
When executed, it prints the following:
Instant timestamp is: 2015-11-02T03:16:04.502Z
Number of seconds elapsed: 1446434164
Number of milliseconds elapsed: 1446434164502

LocalDateTime localDateTime = LocalDateTime.now();
Instant instant = Instant.now();
System.out.println("LocalDateTime is: " + localDateTime + " \nInstant is: " + instant);
When we executed this, it printed the following:
LocalDateTime is: 2015-11-02T17:21:11.402
Instant is: 2015-11-02T11:51:11.404Z


2、Period 
Period of(int years, int months, int days)
Period ofWeeks(int unit)
Period ofDays(int unit)
Period ofMonths(int unit)
Period ofYears (int unit)
Period parse(CharSequence string)

LocalDate manufacturingDate = LocalDate.of(2016, Month.JANUARY, 1);
LocalDate expiryDate = LocalDate.of(2018, Month.JULY, 18);
Period expiry = Period.between(manufacturingDate, expiryDate);
System.out.printf("Medicine will expire in: %d years, %d months, and %d days (%s)\n",
expiry.getYears(), expiry.getMonths(), expiry.getDays(), expiry);
This code segment prints the following:
Medicine will expire in: 2 years, 6 months, and 17 days (P2Y6M17D)


Java8日期和时间API区分了人类和计算机使用日期和时间相关API的方式信息例如,Instant类表示Unix时间戳,并在内部使用long和int变量。
即时值对于人类来说不是非常可读或可用,因为该类不支持与日、月、小时等相关的方法(相反,Period类支持此类方法)
3、Duration
Duration of(long number, 
TemporalUnit unit)
Duration ofDays(long unit)
Duration ofHours(long unit)
Duration ofMinutes(long unit)
Duration ofSeconds(long unit)
Duration ofMillis(long unit)
Duration ofNanos(long unit)
Duration parse(CharSequence string)
### formatting Date and Time

Listing 8-6. FlightTravel.java
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FlightTravel {
 public static void main(String[] args) {
 DateTimeFormatter dateTimeFormatter =
 DateTimeFormatter.ofPattern("dd MMM yyyy hh.mm a");
 // Leaving on 1st Jan 2016, 6:00am from "Singapore"
 ZonedDateTime departure = ZonedDateTime.of(
 LocalDateTime.of(2016, Month.JANUARY, 1, 6, 0),
 ZoneId.of("Asia/Singapore"));
 System.out.println("Departure: " + dateTimeFormatter.format(departure));
 // Arrival on the same day in 10 hours in "Auckland"
 ZonedDateTime arrival =
 departure.withZoneSameInstant(ZoneId.of("Pacific/Auckland"))
 .plusHours(10);
 System.out.println("Arrival: " + dateTimeFormatter.format(arrival));
 }
}
Here is the result:
Departure: 01 Jan 2016 06.00 AM
Arrival: 01 Jan 2016 09.00 PM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值