我是靠谱客的博主 拼搏皮皮虾,最近开发中收集的这篇文章主要介绍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

最后

以上就是拼搏皮皮虾为你收集整理的Date/Time API的全部内容,希望文章能够帮你解决Date/Time API所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部