Java8 four notes

13, the new date and time API

⑴, using LocalDate, LocalTime, LocalDateTime
example LocalDate, LocalTime, LocalDateTime class is immutable objects, respectively, using the ISO-8601日
calendar system (ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的notation) of the date, time, date, and time. They provide a simple date or time, does not include the current time information. It does not contain information related to the time zone.

⑵, Instant timestamps
for "stamping" operations. It is based on Unix describes the first year (the traditional time zone is set to UTC January 1, 1970 at midnight) began to be experienced operation.
⑶, Duration and Period
the Duration: two for calculating the "time" interval
Period: two for calculating the "Date" spacer
⑷, manipulating date
①TemporalAdjuster: time corrector. Sometimes we may need to obtain for example: to adjust the date "next Sunday" and so on.
②TemporalAdjusters: This class provides an implementation of a large number of common TemporalAdjuster by the static method.

Get next Sunday 
the LocalDate nextSunday = LocalDate.now.with ( 
      TemporalAdjusters.next (DayOfWeek.SUNDAY) 
);

⑸, parsing and formatting
java.time.format.DateTimeFormatter classes: class format provides three methods:
① predefined standard formats
related locale format ②
format ③ custom
processing ⑹, time zone
Java8 added support for time zones, time bands are respectively:
ZonedDate, ZonedTime, ZonedDateTime
wherein each time zone corresponds to the ID, area ID are "{areas} / {city}" format
for example: Asia / Shanghai like
ZoneId : This class contains all of the time zone information
getAvailableZoneIds (): you can get all the time zone zone information
of (id): Gets ZoneId object with the specified time zone information
⑺, date formatting the thread-safety issues
SimpleDateFormat is thread non-secure, and in java1.8 provided LocalDate classes and class DateTimeFormat

public  static  void main (String [] args) throws Exception { 
   the DateTimeFormatter DTF = DateTimeFormatter.ofPattern ( "yyyyMMdd" ); 
   
   a Callable <the LocalDate> Task = new new a Callable <the LocalDate> () { 

      @Override 
      public the LocalDate Call () throws Exception {
          / / no matter what changes, will generate a new instance of the security thread 
         the LocalDate LocalDate.parse LD = ( "20,190,501" , DTF);
          return LD; 
      } 
   }; 

   ExecutorService the pool = Executors.newFixedThreadPool (10 ); 
   
   List<Future<LocalDate>> results = new ArrayList<>();
   
   for (int i = 0; i < 10; i++) {
      results.add(pool.submit(task));
   }
   
   for (Future<LocalDate> future : results) {
      System.out.println(future.get());
   }
   
   pool.shutdown();
}

⑻, example

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

import org.junit.Test;

public class TestLocalDateTime {
   
   / ** ZonedDate, ZonedTime, ZonedDateTime: time or date with time zone * / 
   @Test 
   public  void test6 () {
       // Check how many time zones supported in Java8 
      the Set the SET = <String> ZoneId.getAvailableZoneIds (); 
      set.forEach ( :: println System.out); 
      // when specifying Shanghai area 
      the LocalDateTime LDT = LocalDateTime.now (ZoneId.of ( "Asia / on Shanghai" )); 
      System.out.println (LDT); 
      // date and time information with time zone 
      ZDT = ZonedDateTime.now ZonedDateTime (ZoneId.of ( "US / Pacific" )); 
      System.out.println (ZDT); 
   } 
   
   / ** the DateTimeFormatter: parsing and formatting date or time * / 
   @Test 
   public void test5 () {
       // the DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE; 
      
      the DateTimeFormatter DTF = DateTimeFormatter.ofPattern ( "yyyy Year MM month dd day HH: mm: SS E" ); 
      
      the LocalDateTime LDT = LocalDateTime.now (); 
      String strDate = DTF .format (LDT); 
      System.out.println (strDate); 

      // resolved back to the original format of 
      the LocalDateTime newLdt = ldt.parse (strDate, DTF); 
      System.out.println (newLdt); 
   } 
   
   / ** TemporalAdjuster: time corrector * / 
   @Test 
   public  void Test4 () { 
       the LocalDateTime LDT =LocalDateTime.now (); 
      System.out.println (LDT); 
      // number of days of the month specified value modify 
      the LocalDateTime LDT2 = ldt.withDayOfMonth (10 ); 
      System.out.println (LDT2); 
      // get the next one week day 
      the LocalDateTime ldt3 = ldt.with (TemporalAdjusters.next (DayOfWeek.SUNDAY)); 
      System.out.println (ldt3); 
      
      // custom: the next working day 
      the LocalDateTime ldt5 ldt.with = ((m) -> { 
         the LocalDateTime ldt4 = (the LocalDateTime) m; 
         
         DayOfWeek Dow = ldt4.getDayOfWeek (); 
         
         IF (dow.equals (DayOfWeek.FRIDAY)) {
             return ldt4.plusDays (. 3 );
         }the else  IF (dow.equals (DayOfWeek.SATURDAY)) {
             return ldt4.plusDays (2 ); 
         } the else {
             return ldt4.plusDays (. 1 ); 
         } 
      }); 
      
      System.out.println (ldt5); 
      
   } 
   
   / ** 
    * Duration: two for calculating the "time" interval 
    * period: two for calculating the "date" spacer 
    * / 
   @Test 
   public  void Test3 () { 
      the Instant INS1 = Instant.now (); 
      
      System.out.println ( "- ------------------- " );
       the try { 
         the Thread.sleep ( 1000 );
      } catch (InterruptedException e) {
      }
      
      Instant ins2 = Instant.now();
      
      System.out.println("所耗费时间为:" + Duration.between(ins1, ins2));
      System.out.println("获取毫秒:" + Duration.between(ins1, ins2).toMillis());
      
      System.out.println("----------------------------------");
      
      LocalDate ld1 = LocalDate.now();
      LocalDate ld2 = LocalDate.of(2019, 1, 1);
      
      Period pe = Period.between(ld2, ld1);
      System.out.println(pe.getYears());
      System.out.println (pe.getMonths ()); 
      System.out.println(pe.getDays());
   } 
   
   / ** the Instant: timestamp. (Using Unix experienced milliseconds year January 1, 1970 00:00:00) * / 
   @Test 
   public  void test2 () {
       // default UTC time zone using 
      the Instant INS = Instant.now (); 
      System.out .println (INS); 
      // offset 8 hours 
      OffsetDateTime ODT = ins.atOffset (ZoneOffset.ofHours (. 8 )); 
      System.out.println (ODT); 
      // convert this moment is 1970-01-01T00: 00 : 00Z era of milliseconds. 
      System.out.println (ins.toEpochMilli ());
       // change seconds 
      the Instant INS2 Instant.ofEpochSecond = (. 5 ); 
      System.out.println (INS2);
   } 
   
   / **   the LocalDate, LocalTime, the LocalDateTime * / 
   @Test 
   public  void test1 () {
       // get the current system time 
      the LocalDateTime LDT = LocalDateTime.now (); 
      System.out.println (LDT); 
      // the specified date / time create objects 
      the LocalDateTime LD2 = LocalDateTime.of (2019, 05, 01, 10, 10, 10 ); 
      System.out.println (LD2); 
      // add 20 years 
      the LocalDateTime ldt3 = ld2.plusYears (20 ); 
      System.out. the println (ldt3); 
      // Save 2 months 
      the LocalDateTime ldt4 = ld2.minusMonths (2 ); 
      System.out.println (ldt4);
       
      System.out.println (ldt.getYear ()); 
      System.out.println (ldt.getMonthValue ());
      System.out.println(ldt.getDayOfMonth());
      System.out.println(ldt.getHour());
      System.out.println(ldt.getMinute());
      System.out.println(ldt.getSecond());
   }
}

 

Guess you like

Origin www.cnblogs.com/weilz/p/11334733.html