今天是高考的日子,是大四师兄师姐答辩毕业的日子。一代又来,一代又去。好久没写博客,借此特殊日子整理一下前不久学java8新特性时写的代码,留下痕迹。(本博客的代码根据 java8新特性教程 学习整理,加上个人的理解而成,关于某个新特性的介绍代码里的注释已经阐述清楚,故不再写文字介绍,直接看代码吧!)
本篇介绍java8的新特性之一:时间API。
import java.time.Clock;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
import org.junit.Test;
/**
* Date API
Java8在包java.time下面包括了一款新的date和time的API。
新的Date API和Joda-Time库是相兼容的,但是它们不是一样的。
*/
public class DateAPI {
/**
Clock
Clock提供了访问当前日期和时间的方法。
Clock是时区敏感的,并且它可以被用来替代System.currentTimeMillis进行获取当前毫秒数。
同时,时间轴上的时间点是可以用类Instant来表示的。
Instants可以被用来创建遗留的java.util.Date对象。
*/
@Test
public void Clock(){
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();//System.currentTimeMillis
Instant instant = clock.instant();
Date legacyDate = Date.from(instant);
System.err.println(legacyDate);
}
/**
* TimeZones被用来表示ZoneId。
* 它们可以通过静态工厂方法访问。
* TimeZones定义了时差,它在instants和本地日期时间转换上十分重要。
*/
@Test
public void TimeZones(){
System.err.println(ZoneId.getAvailableZoneIds());
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
System.err.println(zone1.getRules());
System.err.println(zone2.getRules());
//ZoneRules[currentStandardOffset=+01:00]
//ZoneRules[currentStandardOffset=-03:00]
}
/**
* LocalTime
本地时间代表了一个和时区无关的时间,e.g. 10pm or 17:30:15.
下面的示例创建了前部分展示的两个时区的本地时间。
然后,我们将比较这两个时间并计算出这两个时间在小时和分钟数上的差异。
*/
@Test
public void LocalTime(){
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
LocalTime local1 = LocalTime.now(zone1);
LocalTime local2 = LocalTime.now(zone2);
System.err.println(local1.isBefore(local2));//false
long hours = ChronoUnit.HOURS.between(local1, local2);
System.err.println(hours);//-4
long minutes = ChronoUnit.MINUTES.between(local1, local2);
System.err.println(minutes);//-299
// LocalTime包含了多个工厂方法用来简化创建过程,其中也包括通过字符串来创建时间:
LocalTime local = LocalTime.of(23, 59, 59);
System.err.println(local);
DateTimeFormatter GERMAN_formatter =
DateTimeFormatter
.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(Locale.GERMAN);
LocalTime now = LocalTime.parse("13:48",GERMAN_formatter);
System.err.println(now);
}
/**
* LocalDate
LocalDate代表了一个可区分日期,e.g. 2014-03-11。 它是不变的同时工作原理类似于LocalTime。
下面的例子描绘了通过加减年,月,日来计算出一个新的日期。需要注意的是这每个操作都返回一个新的实例。
*/
@Test
public void LocalDate(){
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
LocalDate yesterday = tomorrow.minusDays(2);
System.err.println(yesterday);//2014-04-06
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
System.out.println(dayOfWeek); // FRIDAY
//从字符串解析出LocalDate和解析LocalTime一样简单:
DateTimeFormatter germanFormatter =
DateTimeFormatter
.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.GERMAN);
LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter);
System.out.println(xmas); // 2014-12-24
}
/**
* LocalDateTime代表日期和时间。它将我们前部分看到的时间和日期组合进一个实例。
* LocalDateTime是不可变的并且它的工作原理和LocalTime和LocalDate十分相似。
*/
@Test
public void LocalDateTime(){
//-- 从date-time中获取某些字段值:
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
System.out.println(dayOfWeek); // WEDNESDAY
Month month = sylvester.getMonth();
System.out.println(month); // DECEMBER
long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
System.out.println(minuteOfDay); // 1439
// -- 在一些额外的时区信息帮助下,它可以被转换为instant。Instants可以被容易的转换为遗留的java.util.Date类型。
Instant instant = sylvester
.atZone(ZoneId.systemDefault())
.toInstant();
Date legacyDate = Date.from(instant);
System.out.println(legacyDate); // Wed Dec 31 23:59:59 CET 2014
// -- 格式date-time的过程和格式date和time基本上是一样的。在使用系统自带的定义格式时,我们也可以定义我们自己的格式:
// 和java.text.NumberFormat不一样的是DateTimeFormatter是不可变的并且是类型安全的。
DateTimeFormatter formatter =
DateTimeFormatter
.ofPattern("MMM dd, yyyy - HH:mm");
LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);
String string = formatter.format(parsed);
System.out.println(string); // Nov 03, 2014 - 07:13
}
}
详情请见这篇博客: java8新特性教程