java8-14-时间API

原来的时间类 
1.默认值 我们使用起来不方便
2.在不同包 不规范   在java.util和java.sql的包中都有日期类,此外用于格式化和解析的类在java.text包中定义
3.可变  线程不安全
4.时区处理麻烦
 
java8新的时间 
1.都在 java.time 包下  很有规律 
2.用final修饰 不可变 线程安全
3.设置Zoned时区类
 
 
LocalDate、LocalTime、LocalDateTime 类的实
例是不可变的对象,分别表示使用 ISO-8601日
历系统的日期、时间、日期和时间。它们提供
了简单的日期或时间,并不包含当前的时间信
息。也不包含与时区相关的信息。
Instant  时间戳  用于“时间戳”的运算。它是以Unix元年(传统
的设定为UTC时区1970年1月1日午夜时分)开始
所经历的描述进行运算
Duration:用于计算两个“时间”间隔
Period:用于计算两个“日期”间隔
TemporalAdjuster : 时间校正器。有时我们可能需要获
取例如:将日期调整到“下个周日”等操作。
TemporalAdjusters : 该类通过静态方法提供了大量的常
用 TemporalAdjuster 的实现。
java.time.format.DateTimeFormatter 类:该类提供 格式化方法
Java8 中加入了对时区的支持,带时区的时间为分别为:
ZonedDate、ZonedTime、ZonedDateTime
其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式
例如 :Asia/Shanghai 等
ZoneId:该类中包含了所有的时区信息
getAvailableZoneIds() : 可以获取所有时区时区信息
of(id) : 用指定的时区信息获取 ZoneId 对象
 
 
  1 package com.wf.zhang.java8.time;
  2 
  3 import org.junit.Test;
  4 
  5 import java.time.*;
  6 import java.time.format.DateTimeFormatter;
  7 import java.time.temporal.TemporalAdjusters;
  8 import java.util.Set;
  9 
 10 public class TestLocalDateTime {
 11 
 12     /***
 13      *  LocalDate、LocalTime、LocalDateTime 类
 14      *  日期、     时间、      日期和时间
 15      */
 16     @Test
 17     public void test01() {
 18         //系统时间
 19         LocalDateTime ldt = LocalDateTime.now();
 20         System.out.println(ldt);
 21 
 22         //指定时间
 23         LocalDateTime ldt2 = LocalDateTime.of(2015, 5, 1, 16, 16, 16);
 24         System.out.println(ldt2);
 25 
 26         //加上100天
 27         LocalDateTime ldt3 = ldt.plusDays(100);
 28         System.out.println(ldt3);
 29 
 30         //减去2周
 31         LocalDateTime ldt4 = ldt.minusMonths(2);
 32         System.out.println(ldt4);
 33 
 34 
 35         //一年中第几天
 36         System.out.println(ldt.getDayOfYear());
 37         //月中的第二天的日期
 38         LocalDateTime localDateTime = ldt.withDayOfMonth(2);
 39         System.out.println(localDateTime);
 40 
 41 
 42     }
 43 
 44     /**
 45      * Instant 时间戳  从1970-1-1 00:00:00 到指定时间的毫秒值
 46      */
 47 
 48     @Test
 49     public void test02() {
 50 
 51         //-8小时的时间 默认UTC时间
 52         Instant ins = Instant.now();
 53         System.out.println(ins);
 54 
 55         //+8小时 的时间
 56         OffsetDateTime offsetDateTime = ins.atOffset(ZoneOffset.ofHours(8));
 57         System.out.println(offsetDateTime);
 58 
 59         //时间戳
 60         System.out.println(ins.toEpochMilli());
 61 
 62         //加1秒   1970-01-01T00:00:01Z
 63         Instant ofEpochMilli = Instant.ofEpochMilli(1000);
 64 
 65         System.out.println(ofEpochMilli);
 66 
 67     }
 68 
 69 
 70     /**
 71      * Duration:用于计算两个 “时间” 间隔
 72      *
 73      */
 74     @Test
 75     public void test03() throws InterruptedException {
 76 
 77         Instant ins = Instant.now();
 78         try {
 79             Thread.sleep(1000);
 80         } catch (InterruptedException e) {
 81         }
 82 
 83         Instant ins2 = Instant.now();
 84 
 85         //秒的差值
 86         Duration duration = Duration.between(ins, ins2);
 87         System.out.println(duration.toMillis());
 88 
 89 
 90 
 91 
 92         LocalTime lt = LocalTime.now();
 93 
 94         try {
 95             Thread.sleep(1000);
 96         } catch (InterruptedException e) {
 97         }
 98 
 99         LocalTime lt2 = LocalTime.now();
100 
101         //秒的差值
102         Duration duration1 = Duration.between(lt, lt2);
103         System.out.println(duration1.toMillis());
104     }
105 
106     /**
107      *
108      * Period:用于计算两个  “日期” 间隔
109      */
110     @Test
111     public void  test04(){
112         LocalDate ld = LocalDate.of(2008, 6, 6);
113         LocalDate ld2 = LocalDate.now();
114         Period period = Period.between(ld, ld2);
115         //天数上的差值
116         System.out.println(period.getDays());
117         //月份上的差值
118         System.out.println(period.getMonths());
119     }
120 
121     /**
122      *
123      * TemporalAdjuster : 时间校正器。有时我们可能需要获
124      * 取例如:将日期调整到“下个周日”等操作。
125      *
126      * TemporalAdjusters : 该类通过静态方法提供了大量的常
127      * 用 TemporalAdjuster 的实现
128      */
129     @Test
130     public void test05(){
131         LocalDateTime now = LocalDateTime.now();
132         //下一个周日的日期
133         LocalDateTime time = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
134         System.out.println(time);
135     }
136 
137     /**
138      *java.time.format.DateTimeFormatter 类:格式化时间/日期
139      *
140      */
141     @Test
142     public  void test06(){
143 
144         //预定格式化  得到年月日
145         DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
146         LocalDateTime now = LocalDateTime.now();
147         String format = now.format(formatter);
148         System.out.println(format);
149 
150         //自定义格式化    yyyy年MM月dd日 HH:mm:ss
151         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
152         String s = dateTimeFormatter.format(now);
153         System.out.println(s);
154 
155         //指定格式化
156         LocalDateTime parse = now.parse(s, dateTimeFormatter);
157         System.out.println(parse);
158     }
159 
160 
161     /**
162      *  Java8 中加入了对时区的支持,带时区的时间为分别为:
163      * ZonedDate、ZonedTime、ZonedDateTime
164      *
165      * 其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式
166      * 例如 :Asia/Shanghai 等
167      *
168      * ZoneId:该类中包含了所有的时区信息
169      * getAvailableZoneIds() : 可以获取所有时区时区信息
170      * of(id) : 用指定的时区信息获取 ZoneId 对象
171      */
172     @Test
173     public void test07(){
174         //所有时区
175         Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
176         availableZoneIds.forEach(System.out::println);
177 
178         //伦敦时间
179         LocalDateTime time = LocalDateTime.now(ZoneId.of("Europe/London"));
180         System.out.println(time);
181 
182         //带时区的日期
183         LocalDateTime ldt = LocalDateTime.now();
184         ZonedDateTime zone = ldt.atZone(ZoneId.of("Asia/Macao"));
185         System.out.println(zone);
186 
187 
188     }
189 }
View Code

猜你喜欢

转载自www.cnblogs.com/wf-zhang/p/11832169.html