103-java 8新機能(3)-新日付API/繰返し注記
一.元の日付処理
1.SimpleDateFormateスレッドが安全でない
(1)安全でないコード
(2)java 1.8以前のソリューション
(3)java 1.8のソリューション
二.JAva 1.8新時間/日付API
時間操作の要件:任意の時刻を取得する時間 .現在時刻の関連時刻を取得します.例えば、来月10日、当日10時、来週5日など 計算時間の差 時間と文字列間の変換 1.取得日時 LocalDate:取得日 LocalTime:取得時間 LocalDateTime:取得日+時間
2.時間の増減 plusYears(10)#10年後 minusDays(3)#3日前
3.タイムスタンプ-->コンピュータの読み込み時間 instant
4.計算間隔 Duration:計算時間間の間隔 Period:2つの日付間の間隔を計算する
5.TemporalAdjusters時間矯正器
机能:*取得来月何日*取得来周何*取得当月...
6.時間フォーマットDateTimeFormatter
7.タイムゾーンZonedDate/ZoneTime/ZoneDateTime
三.JAva 1.8繰り返し注記
同じ場所で注釈を繰り返し使用することができる.
次の手順に従います.定義注記 注釈容器 を定義する.業務上注釈
詳細、補足
1.SimpleDateFormateスレッドが安全でない
(1)安全でないコード
public static void main(String[] args) throws Exception {
//1.8
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); //1.8 ,
Callable<Date> task = () -> sdf.parse("20200711"); //
ArrayList<Future> list = new ArrayList<>();
ExecutorService pool = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
list.add(pool.submit(task));
}
for (Future future : list) {
System.out.println(future.get());
}
pool.shutdown();
}
(2)java 1.8以前のソリューション
public class TestSimpleDateFormate {
//1.8 ThreadLocal
private static final ThreadLocal<DateFormat> LOCAL = new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyyMMdd");
}
};
public static void main(String[] args) throws Exception {
//1.8
Callable<Date> task = () -> LOCAL.get().parse("20200711"); // .ThreadLocal
ArrayList<Future> list = new ArrayList<>();
ExecutorService pool = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
list.add(pool.submit(task));
}
for (Future future : list) {
System.out.println(future.get());
}
pool.shutdown();
}
}
(3)java 1.8のソリューション
//1.8
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
Callable<LocalDate> task = () -> LocalDate.parse("20200711",dtf);
ArrayList<Future> list = new ArrayList<>();
ExecutorService pool = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
list.add(pool.submit(task));
}
for (Future future : list) {
System.out.println(future.get());
}
pool.shutdown();
二.JAva 1.8新時間/日付API
時間操作の要件:
/**
* LocalDate :
* LocalTime :
* LocalDateTime : +
* API
*
*/
@Test
public void test1(){
//
LocalDate now = LocalDate.now();
System.out.println(now);
//
LocalTime now1 = LocalTime.now();
System.out.println(now1);
// +
LocalDateTime now2 = LocalDateTime.now();
System.out.println(now2);
//
LocalDateTime of = LocalDateTime.of(2021, 1, 31, 22, 22, 22);
System.out.println("of :" + of);
// , , , , ,
System.out.println("year:" + now2.getYear());
System.out.println("month:" + now2.getMonth());
System.out.println("mothOfDate:" + now2.getDayOfMonth());
System.out.println("hour:" + now2.getHour());
}
2.時間の増減
/**
*
*/
@Test
public void test2(){
LocalDateTime now = LocalDateTime.now();
//
//10
LocalDateTime localDateTime = now.plusYears(10);
System.out.println("10 :" + localDateTime);
//3
LocalDateTime localDateTime1 = now.minusDays(3);
System.out.println("3 " + localDateTime1);
}
3.タイムスタンプ-->コンピュータの読み込み時間
/**
* Instant
* -->
*/
@Test
public void test3(){
// UTC
Instant instant = Instant.now();
System.out.println(instant);
//
OffsetDateTime odt = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(odt);
//
long l = instant.toEpochMilli();
System.out.println(l);
//
Instant instant1 = Instant.ofEpochSecond(60);
System.out.println(instant1);
}
4.計算間隔
/**
*
* Duration:
* Period:
*/
@Test
public void test4(){
//
Instant instant = Instant.now();
// 1000
Instant instant1 = instant.minusSeconds(1000);
//
System.out.println(Duration.between(instant,instant1).toMillis());
//
LocalDateTime localDateTime = LocalDateTime.now();
// 1
LocalDateTime localDateTime1 = localDateTime.plusYears(1);
// ,
System.out.println(Duration.between(localDateTime,localDateTime1).toDays());
// instant LocalDateTime
// DateTimeException: Unable to obtain Instant from TemporalAccesso
//System.out.println(Duration.between(instant,localDateTime1).toDays());
}
5.TemporalAdjusters時間矯正器
机能:*取得来月何日*取得来周何*取得当月...
/**
* TemporalAdjusters
* :
*
*
* ...
*/
@Test
public void test5(){
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
// 10
LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10);
System.out.println(localDateTime1);
// 12
LocalDateTime localDateTime2 = localDateTime.withHour(12);
System.out.println(localDateTime2);
//
LocalDateTime localDateTime3 = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(localDateTime3);
}
6.時間フォーマットDateTimeFormatter
/**
* DateTimeFormatter
*
* ,
*/
@Test
public void test6(){
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
//
LocalDateTime localDateTime = LocalDateTime.now();
String s = formatter.format(localDateTime);
System.out.println(s);
// Instant
/*Instant instant = Instant.now();
String s1 = formatter.format(instant);
System.out.println(s1);*/
//
/*TemporalAccessor parse = formatter.parse(s);
System.out.println(parse);*/
//
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy MM dd HH:mm:ss");
String s1 = formatter1.format(LocalDateTime.now());
System.out.println(s1);
LocalDateTime parse1 = LocalDateTime.parse(s1, formatter1);
System.out.println(parse1);
}
7.タイムゾーンZonedDate/ZoneTime/ZoneDateTime
/**
*
* ZonedDate/ZoneTime/ZoneDateTime
*/
@Test
public void test7(){
Set<String> set = ZoneId.getAvailableZoneIds();
set.stream()
.sorted()
.parallel()
.forEach(System.out::println);
LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Chongqing"));
System.out.println(" :" + localDateTime);
}
三.JAva 1.8繰り返し注記
同じ場所で注釈を繰り返し使用することができる.
@MyAnnotation("hello")
@MyAnnotation("world")
public void show(){
System.out.println(" ");
}
次の手順に従います.
@Repeatable(MyAnnotations.class) // ,
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE,ElementType.PARAMETER})
public @interface MyAnnotation {
String value() default " ";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE,ElementType.PARAMETER})
public @interface MyAnnotations {
//myAnnotation
MyAnnotation[] value();
}
public class TestRepeatAnnotation {
@Test
public void test() throws NoSuchMethodException {
Class<TestRepeatAnnotation> clazz = TestRepeatAnnotation.class;
// MyAnnotation
Method show = clazz.getMethod("show");
MyAnnotation[] annotations = show.getAnnotationsByType(MyAnnotation.class);
for (int i = 0; i < annotations.length; i++) {
System.out.println(annotations[i].value());
}
}
@MyAnnotation("hello")
@MyAnnotation("world")
public void show(){
System.out.println(" ");
}
}
詳細、補足