我是靠谱客的博主 粗暴书本,最近开发中收集的这篇文章主要介绍新date+java8_Java8_新日期工具类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 传统的日期格式器方法(线程不安全)

@Test

public void test() throws Exception{

//传统时间格式器(线程不安全)

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

//任务

Callable task = () -> sdf.parse("20201001");

//线程池

ExecutorService pool = Executors.newFixedThreadPool(10);

//模拟多线程

List> futureList = new ArrayList<>();

for (int i = 0; i < 10; i++) {

futureList.add(pool.submit(task));

}

//输出结果

for (Future dateFuture : futureList) {

System.out.println(dateFuture.get());

}

}

//抛出了java.util.concurrent.ExecutionException: java.lang.NumberFormatException: multiple points

2. 传统的日期格式器解决线程安全方法(ThreadLocal)

//1.创建工具类,内部方法由绑定ThreadLocal的SimpleDateFormat提供。

public class SimpleDateFormatThreadLocal {

//创建一个ThreadLocal对象,并初始值为SimpleDateFormat对象

public static final ThreadLocal sdf = new ThreadLocal(){

protected SimpleDateFormat initialValue() {

return new SimpleDateFormat("yyyyMMdd");

}

};

//提供一个dateStr转换Date方法

public static Date convert(String dateStr) throws ParseException {

return sdf.get().parse(dateStr);

}

}

//2.测试(线程安全问题解读了)

@Test

public void test1() throws Exception{

//任务(使用绑定在ThreadLocal上的SimpleDateFormat对象转换)

Callable task = () -> SimpleDateFormatThreadLocal.convert("20201020");

//线程池

ExecutorService pool = Executors.newFixedThreadPool(10);

//模拟多线程

List> futureList = new ArrayList<>();

for (int i = 0; i < 10; i++) {

futureList.add(pool.submit(task));

}

//输出结果

for (Future dateFuture : futureList) {

System.out.println(dateFuture.get());

}

//关闭线程池

pool.shutdown();

}

3. LocalDate新日期类(线程安全)

@Test

public void test2() throws Exception{

//新日期格式器(线程安全)

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");

//任务(LocalDate.parse(dateStr,dateTimeFormatter))

Callable task = () -> LocalDate.parse("20201020",dtf);

//线程池

ExecutorService pool = Executors.newFixedThreadPool(10);

//模拟多线程

List> futureList = new ArrayList<>();

for (int i = 0; i < 10; i++) {

futureList.add(pool.submit(task));

}

//输出结果

for (Future dateFuture : futureList) {

System.out.println(dateFuture.get());

}

//关闭线程池

pool.shutdown();

}

4. 新日期类、时间戳、时间间隔对象,日期间隔对象

//1.LocalDate LocalTime LocalDateTime 新日期类,类实例是不可变对象

@Test

public void test3(){

//获取当前日期对象

LocalDateTime ldt1 = LocalDateTime.now();

System.out.println(ldt1);

//获取指定日期对象

LocalDateTime ldt2 = LocalDateTime.of(2018, 10, 10, 11, 11, 11);

System.out.println(ldt2);

//日期对象运算

LocalDateTime ldt3 = ldt2.plusYears(1);

System.out.println(ldt3);

LocalDateTime ldt4 = ldt3.minusMonths(5);

System.out.println(ldt4);

//获取日期属性...

System.out.println("ldt4.getMonthValue() = " + ldt4.getMonthValue());

System.out.println("ldt4.getHour() = " + ldt4.getHour());

}

//2. Instant : 时间戳(使用 Unix 元年 1970年1月1日 00:00:00 所经历的毫秒值)

@Test

public void test4(){

//获取当前时间戳对象

Instant instant = Instant.now(); //默认UTC时区

System.out.println(instant);

//偏移量时间对象

OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8)); //+8H 北京时间

System.out.println(offsetDateTime);

//获取指定时间戳对象

Instant instant1 = Instant.ofEpochSecond(60); //从Unix元年算起的时间戳

System.out.println(instant1); //1970-01-01T00:01:00Z

}

//Duration : 用于计算两个“时间”间隔

//Period : 用于计算两个“日期”间隔

@Test

public void test5() throws InterruptedException {

//时间间隔对象 Duration

Instant t1 = Instant.now();

Thread.sleep(1000);

Instant t2 = Instant.now();

Duration duration = Duration.between(t1, t2);

System.out.println(duration.toMillis());

//日期间隔对象 Period

LocalDate d1 = LocalDate.of(2020, 1, 1);

LocalDate d2 = LocalDate.now();

Period period = Period.between(d1, d2);

System.out.println(period.getYears()); //相差年数

System.out.println(period.getMonths()); //相差月数

System.out.println(period.getDays()); //相差天数

}

5.时间调整器 TemporaAdjuster

@Test

public void test6(){

//获取当前时间对象

LocalDateTime localDateTime = LocalDateTime.now();

System.out.println(localDateTime);

//指定月份为2

LocalDateTime localDateTime1 = localDateTime.withMonth(2);

System.out.println(localDateTime1);

//时间调整器工具类TemporalAdjusters.next指定下个周日

LocalDateTime localDateTime2 = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));

System.out.println("next Sunday: " + localDateTime2);

//计算下个工作日(自定义时间调整器)

LocalDateTime localDateTime4 = localDateTime.with(temporal -> {

//强转日期类

LocalDateTime localDateTime3 = (LocalDateTime) temporal;

//获取周几

DayOfWeek dayOfWeek = localDateTime3.getDayOfWeek();

//返回下个工作日

if(dayOfWeek.equals(DayOfWeek.FRIDAY)){

return localDateTime3.plusDays(3);

}else if(dayOfWeek.equals(DayOfWeek.SATURDAY)){

return localDateTime3.plusDays(2);

}else {

return localDateTime3.plusDays(1);

}

});

System.out.println("next workDay: " + localDateTime4);

}

6.时间格式器 DateTimeFormatter

@Test

public void test7(){

//日期格式器

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

//当前时间对象

LocalDateTime now = LocalDateTime.now();

//格式化当前日期 localDateTime => dateStr

String dateStr = dateTimeFormatter.format(now);

System.out.println(dateStr);

//解析日期字符串 dateStr => localDateTime

LocalDateTime lacalDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);

System.out.println(lacalDateTime);

}

7.ZonedDate、ZonedTime、ZonedDateTime : 带时区的时间或日期

@Test

public void test8(){

//获取所有时区字符串表现形式

Set set = ZoneId.getAvailableZoneIds();

//set.forEach(System.out::println);

//设置为亚洲/东京时区时间

LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));

System.out.println(localDateTime);

//为日期对象添加时区信息

ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Tokyo"));

System.out.println(zonedDateTime);

}

8.可重复注解

//1. Annotation注解容器

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})

@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnnotations {

MyAnnotation[] value();

}

//2. Annotation注解 -> @Repeatable(MyAnnotations.class) 指定注解容器

@Repeatable(MyAnnotations.class)

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})

@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnnotation {

String value() default "codeDuck";

}

//3. 使用和测试

public class TestAnno {

//重复注解

@MyAnnotation("hello")

@MyAnnotation("world")

public void demo(){}

@Test

public void test() throws NoSuchMethodException {

//字节码对象

Class testAnnoClass = TestAnno.class;

//获取demo方法对象

Method demo = testAnnoClass.getMethod("demo");

//获取MyAnnotation.class类型注解对象

MyAnnotation[] annotations = demo.getAnnotationsByType(MyAnnotation.class);

for (MyAnnotation annotation : annotations) {

System.out.println(annotation.value()); // hello n world

}

}

}

最后

以上就是粗暴书本为你收集整理的新date+java8_Java8_新日期工具类的全部内容,希望文章能够帮你解决新date+java8_Java8_新日期工具类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部