1 public class Test {
2 public static void main(String[] args) {
3 /**
4 *(一) Clock 时钟
5 * Clock类提供了访问当前日期和时间的方法,Clock是时区敏感的,可以用来取代 System.currentTimeMillis() 来获取当前的微秒数。
6 * 某一个特定的时间点也可以使用Instant类来表示,Instant类也可以用来创建老的java.util.Date对象。
7 */
8 Clock clock = Clock.systemDefaultZone();
9 long millis = clock.millis();
10 System.out.println(millis+":"+System.currentTimeMillis());
11 Instant instant = clock.instant();
12 Date legacyDate = Date.from(instant); // legacy java.util.Date
13 System.out.println(legacyDate);
14
15 /**
16 *(二)Timezones 时区
17 * 在新API中时区使用ZoneId来表示。时区可以很方便的使用静态方法of来获取到。
18 * 时区定义了到UTS时间的时间差,在Instant时间点对象到本地日期对象之间转换的时候是极其重要的。
19 */
20 System.out.println(ZoneId.getAvailableZoneIds());//输出所有的时区
21 ZoneId zone1 = ZoneId.of("Europe/Berlin");
22 ZoneId zone2 = ZoneId.of("Brazil/East");
23 System.out.println(zone1.getRules());//输出ZoneRules[currentStandardOffset=+01:00]
24 System.out.println(zone2.getRules());//输出ZoneRules[currentStandardOffset=-03:00]
25
26 /**
27 *(三)LocalTime 本地时间
28 * LocalTime 定义了一个没有时区信息的时间,例如 晚上10点,或者 17:30:15。
29 * 下面的例子使用前面代码创建的时区创建了两个本地时间。之后比较时间并以小时和分钟为单位计算两个时间的时间差:
30 */
31 LocalTime now1 = LocalTime.now(zone1);
32 LocalTime now2 = LocalTime.now(zone2);
33 System.out.println(now1.isBefore(now2)); // false
34 long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
35 long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
36 System.out.println(hoursBetween); // -2
37 System.out.println(minutesBetween); // -179
38
39 /**LocalTime 提供了多种工厂方法来简化对象的创建,包括解析时间字符串。
40 *
41 */
42 LocalTime late = LocalTime.of(23, 59, 59);
43 System.out.println(late); // 23:59:59
44 DateTimeFormatter germanFormatter =
45 DateTimeFormatter
46 .ofLocalizedTime(FormatStyle.SHORT)
47 .withLocale(Locale.GERMAN);
48 LocalTime leetTime = LocalTime.parse("13:37", germanFormatter);
49 System.out.println(leetTime); // 13:37
50
51 /**
52 * (四)LocalDate 本地日期
53 * LocalDate 表示了一个确切的日期,比如 2014-03-11。该对象值是不可变的,用起来和LocalTime基本一致。
54 * 下面的例子展示了如何给Date对象加减天/月/年。另外要注意的是这些对象是不可变的,操作返回的总是一个新实例。
55 */
56 LocalDate today = LocalDate.now();
57 LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
58 LocalDate yesterday = tomorrow.minusDays(2);
59 System.out.println(yesterday);
60 LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
61 DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
62 System.out.println(dayOfWeek); // FRIDAY
63
64 DateTimeFormatter germanFormatter1 =
65 DateTimeFormatter
66 .ofLocalizedDate(FormatStyle.MEDIUM)
67 .withLocale(Locale.GERMAN);
68 LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter1);
69 System.out.println(xmas); // 2014-12-24
70 //用这种方式比以前使用日期的要方便多了
71
72 /**
73 * (五)LocalDateTime 本地日期时间
74 * LocalDateTime 同时表示了时间和日期,相当于前两节内容合并到一个对象上了。
75 * LocalDateTime和LocalTime还有LocalDate一样,都是不可变的。LocalDateTime提供了一些能访问具体字段的方法。
76 */
77 LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
78 DayOfWeek dayOfWeek1 = sylvester.getDayOfWeek();
79 System.out.println(dayOfWeek1); // WEDNESDAY
80 Month month = sylvester.getMonth();
81 System.out.println(month); // DECEMBER
82 long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
83 System.out.println(minuteOfDay); // 1439
84 /**
85 * 只要附加上时区信息,就可以将其转换为一个时间点Instant对象,Instant时间点对象可以很容易的转换为老式的java.util.Date。
86 */
87 Instant instant1 = sylvester
88 .atZone(ZoneId.systemDefault())
89 .toInstant();
90 Date legacyDate1 = Date.from(instant1);
91 System.out.println(legacyDate1); // Wed Dec 31 23:59:59 CET 2014
92
93 /**
94 * 格式化LocalDateTime和格式化时间和日期一样的,除了使用预定义好的格式外,我们也可以自己定义格式:
95 */
96 DateTimeFormatter formatter =
97 DateTimeFormatter
98 .ofPattern("MMM dd, yyyy - HH:mm");
99 LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);
100 String string = formatter.format(parsed);
101 System.out.println(string); // Nov 03, 2014 - 07:13
102
103 //和java.text.NumberFormat不一样的是新版的DateTimeFormatter是不可变的,所以它是线程安全的。
104 //关于时间日期格式的详细信息:http://download.java.net/jdk8/docs/api/java/time/format/DateTimeFormatter.html
105 //java jdk1.8新提供的这个time在某些时候还是有好处的
106
107
108 }
109
110
111
112 }