95 java8新特性_6 _新时间 API 文章目录 95 java8新特性_6 _新时间 API 新时间 API 线程安全问题 本地化日期时间 Instant与ZoneId Date、LocalDateTim
95 java8新特性_6 _新时间 API
文章目录
- 95 java8新特性_6 _新时间 API
- 新时间 API
- 线程安全问题
- 本地化日期时间
- Instant与ZoneId
- Date、LocalDateTime、Instant的转换
- DateTimeFormatter:格式化类
- java8总结:
新时间 API
- 之前时间API存在问题:线程安全问题,设计问题
线程安全问题
package com.wlw.chapter13_java8.demo03_newDate;import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
public class Demo01 {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
ExecutorService es = Executors.newFixedThreadPool(10);
Callable<Date> callable = new Callable<Date>() {
@Override
public Date call() throws Exception {
return sdf.parse("20200425");
}
};
List<Future<Date>> list = new ArrayList<>();
for (int i =0; i < 10; i++){
Future<Date> submit = es.submit(callable);
list.add(submit);
}
for (Future<Date> dateFuture : list) {
System.out.println(dateFuture.get());
}
es.shutdown();
}
}
新时间:
package com.wlw.chapter13_java8.demo03_newDate;import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
public class Demo01 {
public static void main(String[] args) throws Exception {
//SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
ExecutorService es = Executors.newFixedThreadPool(10);
Callable<LocalDate> callable = new Callable<LocalDate>() {
@Override
public LocalDate call() throws Exception {
return LocalDate.parse("20000425",dtf);
}
};
List<Future<LocalDate>> list = new ArrayList<>();
for (int i =0; i < 10; i++){
Future<LocalDate> submit = es.submit(callable);
list.add(submit);
}
for (Future<LocalDate> dateFuture : list) {
System.out.println(dateFuture.get());
}
es.shutdown();
}
}
本地化日期时间
- LocalDate
- LocalTime
- LocalDateTime
import java.time.LocalDateTime;
//LocalDateTime 的使用
public class Demo02 {
public static void main(String[] args) {
//1.创建本地时间
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
//自己定义时间
LocalDateTime of = LocalDateTime.of(2000,5,6,12,3,5);
System.out.println(of);
System.out.println(localDateTime.getYear()); //获取年
System.out.println(localDateTime.getMonthValue()); //获取月
System.out.println(localDateTime.getHour()); //获取小时
//加2天
LocalDateTime plusDays = localDateTime.plusDays(2);
System.out.println(plusDays);
//减2月
LocalDateTime minusMonths = localDateTime.minusMonths(2);
System.out.println(minusMonths);
}
}
Instant与ZoneId
- instant:时间戳(瞬间的时间)
- ZoneId:时区
import com.sun.deploy.util.SyncAccess;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Set;
/**
* - instant:时间戳(瞬间的时间)
* - ZoneId:时区
*/
public class Demo03 {
public static void main(String[] args) {
//创建instant:时间戳
Instant instant = Instant.now();//会错8小时 的时区差
System.out.println(instant.toString());
System.out.println(instant.toEpochMilli());//毫秒数
System.out.println(System.currentTimeMillis());
//添加与减少
Instant instant1 = instant.plusSeconds(10);//加10秒
System.out.println(Duration.between(instant,instant1).toMillis());
//ZoneId
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();//获取电脑支持的时区
for (String availableZoneId : availableZoneIds) {
System.out.println(availableZoneId);
}
System.out.println(ZoneId.systemDefault().toString());//获取本地时区
}
}
Date、LocalDateTime、Instant的转换
package com.wlw.chapter13_java8.demo03_newDate;import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class Demo03 {
public static void main(String[] args) {
//Date --》Instant---》 LocalDateTime
System.out.println("Date --》Instant---》 LocalDateTime-----------------------------------");
Date date = new Date();
Instant instant2 = date.toInstant();
System.out.println(instant2);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault());
System.out.println(localDateTime);
// LocalDateTime---》 Instant--》Date
System.out.println("LocalDateTime---》 Instant--》Date-------------------------------------");
Instant instant3 = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
System.out.println(instant3);
Date from = Date.from(instant3);
System.out.println(from);
}
}
/*
Date --》Instant---》 LocalDateTime-----------------------------------
2020-07-26T10:49:50.835Z
2020-07-26T18:49:50.835
LocalDateTime---》 Instant--》Date-------------------------------------
2020-07-26T10:49:50.835Z
Sun Jul 26 18:49:50 CST 2020
*/
DateTimeFormatter:格式化类
SimpleDateFormat:格式化类(线程不安全)
DateTimeFormatter:格式化类(线程安全)
package com.wlw.chapter13_java8.demo03_newDate;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
// DateTimeFormatter:格式化类
public class Demo04 {
public static void main(String[] args) {
//创建DateTimeFormatter:格式化类
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//把时间格式化字符串
String format = dtf.format(LocalDateTime.now());
System.out.println(format);
//把字符串 解析成时间
LocalDateTime localDateTime = LocalDateTime.parse("2020-06-06 12:12:12", dtf);
System.out.println(localDateTime);
}
}
/*
2020-07-26 19:08:14
2020-06-06T12:12:12
*/
java8总结:
- Lambda表达式:
- 允许把函数作为-一个方法的参数传递。
- 函数式接口:
- Consumer
- Supplier
- Function
- Predicate
- Stream API:
- 把对数据的操作封装成一个流
- 步骤:创建、中间操作、终止操作
- 新时间API