java中线程安全的时间类,【Java】JAVA日期安全格式化之SimpleDateFormat和jodaTime,DateTimeFormatter...

SimpleDateFormat线程不安全的日期格式化库

package com.rumenz.task;

import java.text.SimpleDateFormat;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

//线程不安全

public class DataFormat {

private static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");

private static Integer clientTotal=5000;

private static Integer threadTotal=200;

public static void main(String[] args) throws Exception{

ExecutorService executorService = Executors.newCachedThreadPool();

final Semaphore semaphore=new Semaphore(threadTotal);

final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);

for (int i = 0; i

executorService.execute(()->{

try{

semaphore.acquire();

update();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

countDownLatch.countDown();

});

}

countDownLatch.await();

executorService.shutdown();

}

private static void update() {

try{

simpleDateFormat.parse("20200115");

}catch (Exception e){

e.printStackTrace();

}

}

}

java.lang.NumberFormatException: For input string: "E152"

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:2089)

at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1867)

at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)

at java.text.DateFormat.parse(DateFormat.java:364)

at com.rumenz.task.DataFormat.update(DataFormat.java:47)

at com.rumenz.task.DataFormat.lambda$main$0(DataFormat.java:30)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

jodaTime 线程安全的格式化库

引入依赖

joda-time

joda-time

2.9.9

package com.keytech.task;

import org.joda.time.DateTime;

import java.time.format.DateTimeFormatter;

import java.time.temporal.TemporalAccessor;

import java.util.Date;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

//线程安全

public class DataFormat1 {

public static Integer clientTotal=5000;

public static Integer threadTotal=200;

private static DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyyMMdd");

public static void main(String[] args) throws Exception{

ExecutorService executorService = Executors.newCachedThreadPool();

final Semaphore semaphore=new Semaphore(threadTotal);

final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);

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

executorService.execute(()->{

try{

semaphore.acquire();

update();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

countDownLatch.countDown();

});

}

countDownLatch.await();

executorService.shutdown();

}

private static void update() {

DateTime dateTime = DateTime.parse("20200115").toDateTime();

System.out.println(dateTime);

}

}

DateTimeFormatter JAVA8中线程安全的日期转换类

package com.keytech.task;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

//线程安全

public class DateFormat2 {

public static Integer clientTotal=5000;

public static Integer threadTotal=200;

private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

public static void main(String[] args) throws Exception{

ExecutorService executorService = Executors.newCachedThreadPool();

final Semaphore semaphore=new Semaphore(threadTotal);

final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);

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

try{

semaphore.acquire();

update();

semaphore.release();

}catch (Exception e){

e.printStackTrace();

}

countDownLatch.countDown();

}

countDownLatch.await();

executorService.shutdown();

}

private static void update() {

LocalDate localDate = LocalDate.parse("20200115", formatter);

System.out.println(localDate);

}

}

关注微信公众号:【入门小站】,解锁更多知识点

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们不使用`java.util.Date`和`java.text.SimpleDateFormat`类时,可以使用`java.time`包中的类来处理日期时间。 1. `java.time.LocalDate`类表示一个日期,它只包含年、月、日的信息,没有时间和时区的概念。可以使用`LocalDate.now()`方法获取当前日期,也可以使用`LocalDate.of(year, month, day)`方法创建指定日期的对象。 2. `java.time.LocalTime`类表示一个时间,它只包含时、分、秒和毫秒的信息,没有日期和时区的概念。可以使用`LocalTime.now()`方法获取当前时间,也可以使用`LocalTime.of(hour, minute, second, nanoOfSecond)`方法创建指定时间的对象。 3. `java.time.LocalDateTime`类表示一个日期时间,它包含年、月、日、时、分、秒和毫秒的信息,没有时区的概念。可以使用`LocalDateTime.now()`方法获取当前日期时间,也可以使用`LocalDateTime.of(year, month, day, hour, minute, second, nanoOfSecond)`方法创建指定日期时间的对象。 4. `java.time.format.DateTimeFormatter`类用于格式化和解析日期时间对象。可以使用`DateTimeFormatter.ofPattern(pattern)`方法创建一个格式化模式,然后使用`format()`方法将日期时间对象格式化为字符串,或者使用`parse()`方法将字符串解析为日期时间对象。 5. `java.time.Duration`类用于表示两个时间点之间的持续时间。可以使用`Duration.between(start, end)`方法计算两个时间点之间的持续时间,返回一个`Duration`对象,然后可以使用`toXXX()`方法获取持续时间的各个部分(如小时、分钟、秒等)。 6. `java.time.Period`类用于表示两个日期之间的间隔。可以使用`Period.between(start, end)`方法计算两个日期之间的间隔,返回一个`Period`对象,然后可以使用`getYears()`、`getMonths()`、`getDays()`等方法获取间隔的年、月、日等部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值