package com.dance.java8.day01.date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
public class TestSimpleDateFormat {
public static void main(String[] args) throws ExecutionException, InterruptedException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
ExecutorService executorService = Executors.newFixedThreadPool(10);
Callable<Date> callable = () -> simpleDateFormat.parse("20161218");
List<Future<Date>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(executorService.submit(callable));
}
for (Future<Date> dateFuture : list) {
System.out.println(dateFuture.get());
}
}
}
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.NumberFormatException: multiple points
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at com.dance.java8.day01.date.TestSimpleDateFormat.main(TestSimpleDateFormat.java:26)
Caused by: 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:1867)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.dance.java8.day01.date.TestSimpleDateFormat.lambda$main$0(TestSimpleDateFormat.java:17)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
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)
package com.dance.java8.day01.date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatThreadLocal {
private static final ThreadLocal<DateFormat> dateformat = new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyyMMdd");
}
};
public static Date convert(String source) throws ParseException {
return dateformat.get().parse(source);
}
}
package com.dance.java8.day01.date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
public class TestSimpleDateFormat {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
ExecutorService executorService = Executors.newFixedThreadPool(10);
Callable<Date> callable = () -> DateFormatThreadLocal.convert("20161218");
List<Future<Date>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(executorService.submit(callable));
}
for (Future<Date> dateFuture : list) {
System.out.println(dateFuture.get());
}
}
}
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
Sun Dec 18 00:00:00 CST 2016
package com.dance.java8.day01.date;
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 TestSimpleDateFormatNewApi {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
// 格式化类
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// localDate
Callable<LocalDate> callable = () -> LocalDate.parse("20161218",dateTimeFormatter);
List<Future<LocalDate>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(executorService.submit(callable));
}
for (Future<LocalDate> dateFuture : list) {
System.out.println(dateFuture.get());
}
executorService.shutdown();
}
}
2016-12-18
2016-12-18
2016-12-18
2016-12-18
2016-12-18
2016-12-18
2016-12-18
2016-12-18
2016-12-18
2016-12-18
@Test
public void test1(){
// 获取当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 打印
System.out.println("当前时间为:"+localDateTime);
// 指定日期
LocalDateTime of = LocalDateTime.of(2021, 12, 4, 17, 49, 12);
System.out.println("指定日期和时间:"+of);
// 日期时间计算
// + 1 day
System.out.println("添加一天:" + localDateTime.plusDays(1));
// -1 day
System.out.println("减去一天:" + localDateTime.plusDays(-1));
// -3 year
System.out.println("减去三年:" + localDateTime.minusYears(3));
// 获取
System.out.println(localDateTime.getYear());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getHour());
System.out.println(localDateTime.getMinute());
System.out.println(localDateTime.getSecond());
}
当前时间为:2021-12-04T17:56:57.172
指定日期和时间:2021-12-04T17:49:12
添加一天:2021-12-05T17:56:57.172
减去一天:2021-12-03T17:56:57.172
减去三年:2018-12-04T17:56:57.172
2021
12
4
17
56
57
参考LocalDateTime
Instant : 时间戳(以Unix 元年: 1970年1月1日 00:00:00 到某个时间之间的毫秒值)
@Test
public void test2(){
Instant now = Instant.now();
System.out.println("时间为:"+now); // 默认获取UTC 时区 格林尼治时间
// 小时偏移量计算
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println("偏移后的时间:"+offsetDateTime);
// 转换为毫秒
System.out.println("毫秒:"+now.toEpochMilli());
System.out.println("系统:"+System.currentTimeMillis());
// 偏移开始时间
Instant instant = Instant.ofEpochSecond(60);
System.out.println("元年偏移时间:"+instant);
}
时间为:2021-12-04T10:10:50.324Z
偏移后的时间:2021-12-04T18:10:50.324+08:00
毫秒:1638612650324
系统:1638612650428
元年偏移时间:1970-01-01T00:01:00Z
@Test
public void test3() throws InterruptedException {
// 时间间隔
Instant start = Instant.now();
Thread.sleep(1000);
Instant end = Instant.now();
Duration between = Duration.between(start, end);
System.out.println("时间间隔为:"+between.getSeconds());
LocalTime startTime = LocalTime.now();
Thread.sleep(1000);
LocalTime endTime = LocalTime.now();
long seconds = Duration.between(startTime, endTime).getSeconds();
System.out.println("时间间隔为:" + seconds);
}
时间间隔为:1
时间间隔为:1
@Test
public void test4(){
LocalDate now = LocalDate.now();
LocalDate localDate = LocalDate.of(2022,3,4);
Period between = Period.between(now, localDate);
System.out.println("日期间隔:"+between);
}
日期间隔:P3M
@Test
public void test5(){
LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间为:"+now);
// 指定日期为几号
LocalDateTime localDateTime = now.withDayOfMonth(10);
System.out.println("with date after:"+localDateTime);
// 时间矫正器
// 调整到下周日
LocalDateTime with = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println("下周日为:"+with);
// 自定义下一个工作日
LocalDateTime workDay = now.with(l -> {
LocalDateTime work = (LocalDateTime) l;
DayOfWeek dayOfWeek = work.getDayOfWeek();
switch (dayOfWeek) {
case FRIDAY:
return work.plusDays(3);
case SATURDAY:
return work.plusDays(2);
default:
return work.plusDays(1);
}
});
System.out.println("下一个工作日:"+workDay);
}
当前时间为:2021-12-04T21:45:33.042
with date after:2021-12-10T21:45:33.042
下周日为:2021-12-05T21:45:33.042
下一个工作日:2021-12-06T21:45:33.042
@Test
public void test6(){
// 使用提供的格式化类
DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE;
LocalDateTime now = LocalDateTime.now();
String format = now.format(isoDate);
System.out.println(format);
// 自定义格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = now.format(dateTimeFormatter);
System.out.println(format1);
}
2021-12-04
2021-12-04 22:19:10
@Test
public void test7(){
// 打印所有时区
// ZoneId.getAvailableZoneIds().forEach(System.out::println);
// 指定时区获取时间
LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println(now);
// 获取携带时区
ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(zonedDateTime);
}
2021-12-04T22:39:25.881
2021-12-04T22:39:25.881+08:00[Asia/Shanghai]