java8 the new time and date

One: solve the traditional time format thread-safety issues

Traditional time formatted using the SimpleDateFormat class, but SimpleDateFormat is not a thread-safe classes in multiple threads will complain

    public static void main(String[] args) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Callable<Date> task = () -> sdf.parse("20190813");
        ExecutorService pool = Executors.newFixedThreadPool(10);
        
        List<Future<Date>> results = new ArrayList<>();
        for(int i = 0; i < 10; i++) {
            results.add(pool.submit(task));
        }
        
        for(Future<Date> future : results) {
            System.out.println(future.get());
        }
    }

This code will execute discovery packet java.lang.NumberFormatException: multiple points exception.

 

In java8 use DateTimeFormatter can solve this problem

    public static void main(String[] args) throws Exception{
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        Callable<LocalDate> task = () -> LocalDate.parse("20190813", df);
        ExecutorService 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());
        }
    }

 

Two: the new time and a new date type

1.localdat to, localtime, my localdateti

    public  static  void newDateTime () {
         // get the current time 
        the LocalDateTime LDT = LocalDateTime.now (); 
        System.out.println (LDT); 
        
        // acquires the specified date, day, hour time 
        LocalDateTime ldt2 = LocalDateTime.of (2019, . 9, 13 is, 18 is,. 8,. 4 ); 
        System.out.println (LDT2); 
        
        // increase / decrease year, increase / decrease month, day, week, hours, minutes, seconds Similarly 
        LocalDateTime ldt3 = ldt.plusYears ( 2 ); 
        System.out.println (ldt3); 
        the LocalDateTime ldt4 = ldt.minusYears (2 ); 
        System.out.println (ldt4); 
        
        // get year, month 
         System.out.println (ldt.getYear ());
        System.out.println (ldt.getMonthValue ()); 
        
    }

2.Instant: timestamp

    public  static  void newInstant () {
         // Instant.now () Gets the default zone UTC time, UTC time zone is GMT deviations have eight hours 
        the Instant INS1 = Instant.now (); 
        System.out.println (INS1); 
        
        // set with offset time zone UTC 
        OffsetDateTime ODT = ins1.atOffset (ZoneOffset.ofHours (. 8 )); 
        System.out.println (ODT); 
        
        // Get millisecond timestamp 
        System.out.println (ins1.toEpochMilli ()); 
        
    }

3.Duration: calculating time interval between the two; Period: calculating the spacing between two dates

    public  static  void calculateInterval () throws Exception { 
        the Instant INS1 = Instant.now (); 
        the Thread.sleep ( 1000 ); 
        the Instant INS2 = Instant.now ();
         // calculate the time interval between the two 
        the Duration DURATION = Duration.between (INS1, INS2); 
        ; System.out.println (duration.toMillis ()) 
        
        
        the LocalDate LD1 = LocalDate.of (, 2015,. 3, 22 is ); 
        the LocalDate LD2 = ; LocalDate.now ()
         // between two dates interval 
        period period = Period.between (LD1, LD2);
        System.out.println (period); 
        // Gets Date interval, month, day 
        System.out.println (period.getYears ()); 
        System.out.println (period.getMonths ()); 
        System.out. the println (period.getDays ()); 
        
    }

4. TemporalAdjuster: Time Correction

    public  static  void demoTemporalAdjuster () { 
        the LocalDateTime LDT1 = LocalDateTime.now (); 
        System.out.println (LDT1); 
        
        // tenth day of the month of the date of obtaining 
        the LocalDateTime LDT2 = ldt1.withDayOfMonth (10 ); 
        System.out.println (LDT2); 
        
        // Get the next weekend date 
        LocalDateTime ldt3 = ldt1.with (TemporalAdjusters.next (DayOfWeek.SUNDAY)); 
        System.out.println (ldt3); 
        
        // custom Get the next working day date 
        LocalDateTime = ldt1.with ldt5 ((L) -> { 
            the LocalDateTime ldt4 = (the LocalDateTime) L; 
            
            dayOfWeek dayOfWeek = ldt4.getDayOfWeek();
            
            if(dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return ldt4.plusDays(3);
            } else if(dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return ldt4.plusDays(2);
            } else {
                return ldt4.plusDays(1);
            }
        });
        System.out.println(ldt5);
    }

5. Format Time: DateTimeFormatter

    public  static  void demoDateTimeFormatter () { 
        the LocalDateTime now = LocalDateTime.now (); 
        
        // define the date conversion mode 
        DateTimeFormatter df = DateTimeFormatter.ofPattern ( "yyyy Year MM month dd day HH: mm: SS" ); 
        
        // Date Format 
        String = formatDateStr df.format (now); 
        System.out.println (formatDateStr); 
        
        // convert the date 
        the LocalDateTime newDate = LocalDateTime.parse (formatDateStr, DF); 
        System.out.println (newDate); 
    }

 

Guess you like

Origin www.cnblogs.com/programmlover/p/11517418.html