/**
* 旧版时间API的问题:
* 1.设计很差:在java.util和java.sql的包中都有日期类,java.util同时包含日期和时间,而java.sql中仅包含日期
* 2.非线程安全:java.util.Date是非线程安全的,所有的日期类都是可变的,这是最严重的问题
* 3.时区处理麻烦:日期类并不提供国际化,没有时区支持
*/
//获取本地日期 年月日
LocalDate date1 = LocalDate.now();
System.out.println("LocalDate"+date1);
//创建制定日期
LocalDateTime of = LocalDateTime.of(2020, 4, 2, 4, 2, 4);
System.out.println("createTime:"+of);
//获取本地日期 时分秒
LocalTime now1 = LocalTime.now();
System.out.println("LocalTime:"+now1);
//获取本地日期 年月日时分秒
LocalDateTime now2 = LocalDateTime.now();
System.out.println("LocalDateTime:"+now2);
//获取当前时区时间
ZonedDateTime now = ZonedDateTime.now();
System.out.println("ZonedDateTime"+now);
//查看所有时区
ZoneId.getAvailableZoneIds().forEach(System.out::println);
//设置成别的时区时间
ZonedDateTime zonedDateTime = now.withZoneSameInstant(ZoneId.of("America/Vancouver"));
System.out.println("withZoneSameInstant:"+zonedDateTime);
//时间解析类
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
String format1 = now2.format(format);
System.out.println(format1);
//时间戳
Instant now3 = Instant.now();
System.out.println(now3);
// Duration:用于计算2个时间(LocalTime,时分秒)的距离
//Period:用于计算2个日期(LocalDate,年月日)的距离
打印结果:
LocalDate2020-07-09
createTime:2020-04-02T04:02:04
LocalTime:12:29:42.655
LocalDateTime:2020-07-09T12:29:42.655
ZonedDateTime2020-07-09T12:29:42.655+08:00[Asia/Shanghai]
测试一下旧版本的API并发不安全问题:
/**
* 旧版时间API测试
*/
public static void test1(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
for(int i = 0;i < 50;i++){
new Thread(()->{
try{
Date date = format.parse("2020-07-09");
System.out.println(date.toLocaleString());
}catch (ParseException e){
e.printStackTrace();
}
}).start();
}
}
结果:
Exception in thread "Thread-2" Exception in thread "Thread-9" Exception in thread "Thread-7" Exception in thread "Thread-3" Exception in thread "Thread-8" Exception in thread "Thread-0" Exception in thread "Thread-4" Exception in thread "Thread-6" java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.text.DigitList.getDouble(DigitList.java:169)
at java.text.DecimalFormat.parse(DecimalFormat.java:2056)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.example.multithreading.test.StreamDemo.lambda$test1$0(StreamDemo.java:64)
at com.example.multithreading.test.StreamDemo$$Lambda$1/1702297201.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: For input string: "E.0"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.text.DigitList.getDouble(DigitList.java:169)
at java.text.DecimalFormat.parse(DecimalFormat.java:2056)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.example.multithreading.test.StreamDemo.lambda$test1$0(StreamDemo.java:64)
at com.example.multithreading.test.StreamDemo$$Lambda$1/1702297201.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:601)
at java.lang.Long.parseLong(Long.java:631)
at java.text.DigitList.getLong(DigitList.java:195)
at java.text.DecimalFormat.parse(DecimalFormat.java:2051)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.example.multithreading.test.StreamDemo.lambda$test1$0(StreamDemo.java:64)
at com.example.multithreading.test.StreamDemo$$Lambda$1/1702297201.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.text.DigitList.getDouble(DigitList.java:169)
at java.text.DecimalFormat.parse(DecimalFormat.java:2056)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.example.multithreading.test.StreamDemo.lambda$test1$0(StreamDemo.java:64)
at com.example.multithreading.test.StreamDemo$$Lambda$1/1702297201.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.text.DigitList.getDouble(DigitList.java:169)
at java.text.DecimalFormat.parse(DecimalFormat.java:2056)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.example.multithreading.test.StreamDemo.lambda$test1$0(StreamDemo.java:64)
at com.example.multithreading.test.StreamDemo$$Lambda$1/1702297201.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
不仅报错,而且打印出来的东西也不对
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-6-30 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2019-12-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
2020-7-9 0:00:00
0001-7-9 0:00:00
0001-7-9 0:00:00
Process finished with exit code 0
测试一下新版API多线程问题是否会出现:
/**
* 新API线程测试
*/
public static void test2(){
LocalDate now = LocalDate.of(2020,7,9);
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
for(int i = 0;i < 50;i++){
new Thread(()->{
try{
String format1 = now.format(format);
System.out.println(format1);
}catch (Exception e){
e.printStackTrace();
}
}).start();
}
}
结果:
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
2020-07-09
Process finished with exit code 0
建议以后打架还是使用新版的时间API,都在java.time这个包下