深入了解Java 8日期和时间API

在这篇文章中,我们将更深入地了解通过Java 8获得的新的Date / Time API(
JSR 310 )。 请注意,本文主要由显示新API功能的代码示例驱动。 我认为这些示例是不言自明的,因此我没有花太多时间在它们周围写文本:-)

让我们开始吧!

处理日期和时间对象

Java 8 Date / Time API的所有类都位于java.time包内。 我们要看的第一类是java.time.LocalDate。 LocalDate表示没有时间的年月日日期。 我们从创建新的LocalDate实例开始:

// the current date
LocalDate currentDate = LocalDate.now();

// 2014-02-10
LocalDate tenthFeb2014 = LocalDate.of(2014, Month.FEBRUARY, 10);

// months values start at 1 (2014-08-01)
LocalDate firstAug2014 = LocalDate.of(2014, 8, 1);

// the 65th day of 2010 (2010-03-06)
LocalDate sixtyFifthDayOf2010 = LocalDate.ofYearDay(2010, 65);

LocalTimeLocalDateTime是我们要研究的下一个类。 两者的工作方式都与LocalDate类似。 LocalTime可用于时间(无日期),而LocalDateTime可将日期和时间组合在一个类中:

LocalTime currentTime = LocalTime.now(); // current time
LocalTime midday = LocalTime.of(12, 0); // 12:00
LocalTime afterMidday = LocalTime.of(13, 30, 15); // 13:30:15

// 12345th second of day (03:25:45)
LocalTime fromSecondsOfDay = LocalTime.ofSecondOfDay(12345);

// dates with times, e.g. 2014-02-18 19:08:37.950
LocalDateTime currentDateTime = LocalDateTime.now();

// 2014-10-02 12:30
LocalDateTime secondAug2014 = LocalDateTime.of(2014, 10, 2, 12, 30);

// 2014-12-24 12:00
LocalDateTime christmas2014 = LocalDateTime.of(2014, Month.DECEMBER, 24, 12, 0);

默认情况下,LocalDate / Time类将使用默认时区中的系统时钟。 我们可以通过提供时区或其他Clock实现来更改此设置:

// current (local) time in Los Angeles
LocalTime currentTimeInLosAngeles = LocalTime.now(ZoneId.of("America/Los_Angeles"));

// current time in UTC time zone
LocalTime nowInUtc = LocalTime.now(Clock.systemUTC());

从LocalDate / Time对象,我们可以获得我们可能需要的各种有用信息。 一些例子:

LocalDate date = LocalDate.of(2014, 2, 15); // 2014-02-15

boolean isBefore = LocalDate.now().isBefore(date); // false

// information about the month
Month february = date.getMonth(); // FEBRUARY
int februaryIntValue = february.getValue(); // 2
int minLength = february.minLength(); // 28
int maxLength = february.maxLength(); // 29
Month firstMonthOfQuarter = february.firstMonthOfQuarter(); // JANUARY

// information about the year
int year = date.getYear(); // 2014
int dayOfYear = date.getDayOfYear(); // 46
int lengthOfYear = date.lengthOfYear(); // 365
boolean isLeapYear = date.isLeapYear(); // false

DayOfWeek dayOfWeek = date.getDayOfWeek();
int dayOfWeekIntValue = dayOfWeek.getValue(); // 6
String dayOfWeekName = dayOfWeek.name(); // SATURDAY

int dayOfMonth = date.getDayOfMonth(); // 15
LocalDateTime startOfDay = date.atStartOfDay(); // 2014-02-15 00:00

// time information
LocalTime time = LocalTime.of(15, 30); // 15:30:00
int hour = time.getHour(); // 15
int second = time.getSecond(); // 0
int minute = time.getMinute(); // 30
int secondOfDay = time.toSecondOfDay(); // 55800

无需提供特定日期即可获得某些信息。 例如,如果我们需要有关特定年份的信息,则可以使用Year类:

Year currentYear = Year.now();
Year twoThousand = Year.of(2000);
boolean isLeap = currentYear.isLeap(); // false
int length = currentYear.length(); // 365

// sixtyFourth day of 2014 (2014-03-05)
LocalDate date = Year.of(2014).atDay(64);

我们可以使用加号和减号方法来添加或减去特定的时间量。 请注意,这些方法总是返回一个新实例(Java 8日期/时间类是不可变的)。

LocalDate tomorrow = LocalDate.now().plusDays(1);

// before 5 houres and 30 minutes
LocalDateTime dateTime = LocalDateTime.now().minusHours(5).minusMinutes(30);

TemporalAdjusters是日期处理的另一种不错的方法。 TemporalAdjuster是一个单一方法接口,用于将调整过程与实际日期/时间对象分开。 可以使用TemporalAdjusters类的静态方法访问一组常见的TemporalAdjusters

LocalDate date = LocalDate.of(2014, Month.FEBRUARY, 25); // 2014-02-25

// first day of february 2014 (2014-02-01)
LocalDate firstDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth());

// last day of february 2014 (2014-02-28)
LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());

静态导入使它更通俗易懂:

import static java.time.temporal.TemporalAdjusters.*;

...

// last day of 2014 (2014-12-31)
LocalDate lastDayOfYear = date.with(lastDayOfYear());

// first day of next month (2014-03-01)
LocalDate firstDayOfNextMonth = date.with(firstDayOfNextMonth());

// next sunday (2014-03-02)
LocalDate nextSunday = date.with(next(DayOfWeek.SUNDAY));

时区

使用时区是新API简化的另一个重要主题。 到目前为止,我们已经看到的LocalDate / Time类不包含有关时区的信息。 如果我们要使用某个时区中的日期/时间,可以使用ZonedDateTimeOffsetDateTime

ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
ZoneId berlin = ZoneId.of("Europe/Berlin");

// 2014-02-20 12:00
LocalDateTime dateTime = LocalDateTime.of(2014, 02, 20, 12, 0);

// 2014-02-20 12:00, Europe/Berlin (+01:00)
ZonedDateTime berlinDateTime = ZonedDateTime.of(dateTime, berlin);

// 2014-02-20 03:00, America/Los_Angeles (-08:00)
ZonedDateTime losAngelesDateTime = berlinDateTime.withZoneSameInstant(losAngeles);

int offsetInSeconds = losAngelesDateTime.getOffset().getTotalSeconds(); // -28800

// a collection of all available zones
Set<String> allZoneIds = ZoneId.getAvailableZoneIds();

// using offsets
LocalDateTime date = LocalDateTime.of(2013, Month.JULY, 20, 3, 30);
ZoneOffset offset = ZoneOffset.of("+05:00");

// 2013-07-20 03:30 +05:00
OffsetDateTime plusFive = OffsetDateTime.of(date, offset);

// 2013-07-19 20:30 -02:00
OffsetDateTime minusTwo = plusFive.withOffsetSameInstant(ZoneOffset.ofHours(-2));

时间戳记

诸如LocalDate和ZonedDateTime之类的类提供了对时间的直观了解。 但是,通常我们需要从机器角度来看待时间。 为此,我们可以使用表示时间戳的Instant类。 即时计数是从1970年1月1日(1970-01-01 00:00:00)的第一秒开始的时间,也称为EPOCH 。 如果即时值出现在纪元之前,则它们可以为负。 它们遵循ISO 8601表示日期和时间的标准。

// current time
Instant now = Instant.now();

// from unix timestamp, 2010-01-01 12:00:00
Instant fromUnixTimestamp = Instant.ofEpochSecond(1262347200);

// same time in millis
Instant fromEpochMilli = Instant.ofEpochMilli(1262347200000l);

// parsing from ISO 8601
Instant fromIso8601 = Instant.parse("2010-01-01T12:00:00Z");

// toString() returns ISO 8601 format, e.g. 2014-02-15T01:02:03Z
String toIso8601 = now.toString();

// as unix timestamp
long toUnixTimestamp = now.getEpochSecond();

// in millis
long toEpochMillis = now.toEpochMilli();

// plus/minus methods are available too
Instant nowPlusTenSeconds = now.plusSeconds(10);

期限和期限

期间持续时间是另外两个重要的类别。 就像名称所暗示的那样,它们代表时间或数量。 期间使用基于日期的值(年,月,日),而期间使用秒或纳秒来定义时间量。 持续时间最适合使用Instants和机器时间。 如果终点发生在起点之前,则“期间”和“持续时间”可以包含负值。

// periods

LocalDate firstDate = LocalDate.of(2010, 5, 17); // 2010-05-17
LocalDate secondDate = LocalDate.of(2015, 3, 7); // 2015-03-07
Period period = Period.between(firstDate, secondDate);

int days = period.getDays(); // 18
int months = period.getMonths(); // 9
int years = period.getYears(); // 4
boolean isNegative = period.isNegative(); // false

Period twoMonthsAndFiveDays = Period.ofMonths(2).plusDays(5);
LocalDate sixthOfJanuary = LocalDate.of(2014, 1, 6);

// add two months and five days to 2014-01-06, result is 2014-03-11
LocalDate eleventhOfMarch = sixthOfJanuary.plus(twoMonthsAndFiveDays);

// durations

Instant firstInstant= Instant.ofEpochSecond( 1294881180 ); // 2011-01-13 01:13
Instant secondInstant = Instant.ofEpochSecond(1294708260); // 2011-01-11 01:11

Duration between = Duration.between(firstInstant, secondInstant);

// negative because firstInstant is after secondInstant (-172920)
long seconds = between.getSeconds();

// get absolute result in minutes (2882)
long absoluteResult = between.abs().toMinutes();

// two hours in seconds (7200)
long twoHoursInSeconds = Duration.ofHours(2).getSeconds();

格式化和解析

格式化和解析是处理日期和时间时的另一个大话题。 在Java 8中,这可以通过使用format()和parse()方法来实现:

// 2014-04-01 10:45
LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);

// format as basic ISO date format (20140220)
String asBasicIsoDate = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);

// format as ISO week date (2014-W08-4)
String asIsoWeekDate = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE);

// format ISO date time (2014-02-20T20:04:05.867)
String asIsoDateTime = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);

// using a custom pattern (01/04/2014)
String asCustomPattern = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));

// french date formatting (1. avril 2014)
String frenchDate = dateTime.format(DateTimeFormatter.ofPattern("d. MMMM yyyy", new Locale("fr")));

// using short german date/time formatting (01.04.14 10:45)
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
    .withLocale(new Locale("de"));
String germanDateTime = dateTime.format(formatter);

// parsing date strings
LocalDate fromIsoDate = LocalDate.parse("2014-01-20");
LocalDate fromIsoWeekDate = LocalDate.parse("2014-W14-2", DateTimeFormatter.ISO_WEEK_DATE);
LocalDate fromCustomPattern = LocalDate.parse("20.01.2014", DateTimeFormatter.ofPattern("dd.MM.yyyy"));

转换次数

当然,我们并不总是拥有所需类型的对象。 因此,我们需要一个选项来在彼此之间转换不同的日期/时间相关对象。 以下示例显示了一些可能的转换选项:

// LocalDate/LocalTime <-> LocalDateTime
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTimeFromDateAndTime = LocalDateTime.of(date, time);
LocalDate dateFromDateTime = LocalDateTime.now().toLocalDate();
LocalTime timeFromDateTime = LocalDateTime.now().toLocalTime();

// Instant <-> LocalDateTime
Instant instant = Instant.now();
LocalDateTime dateTimeFromInstant = LocalDateTime.ofInstant(instant, ZoneId.of("America/Los_Angeles"));
Instant instantFromDateTime = LocalDateTime.now().toInstant(ZoneOffset.ofHours(-2));

// convert old date/calendar/timezone classes
Instant instantFromDate = new Date().toInstant();
Instant instantFromCalendar = Calendar.getInstance().toInstant();
ZoneId zoneId = TimeZone.getDefault().toZoneId();
ZonedDateTime zonedDateTimeFromGregorianCalendar = new GregorianCalendar().toZonedDateTime();

// convert to old classes
Date dateFromInstant = Date.from(Instant.now());
TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of("America/Los_Angeles"));
GregorianCalendar gregorianCalendar = GregorianCalendar.from(ZonedDateTime.now());

结论

使用Java 8,我们可以在java.time包中获得一个非常丰富的API,用于处理日期和时间。 该API可以用更新,更灵活的类完全替换旧的类,如java.util.Date或java.util.Calendar。 由于大多数类都是不可变的,因此新的API有助于构建线程安全的系统。


翻译自: https://www.javacodegeeks.com/2014/03/a-deeper-look-into-the-java-8-date-and-time-api.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值