Java 8: LocalDate, LocalTime, LocalDateTime 처리 날짜 시간

4529 단어 Java
JDK 8 에는 처리 시간 을 위해 세 가지 종류 가 추가 됐다.
LocalDate 전문 처리 날짜, LocalTime 전문 처리 시간, LocalDateTime 은 날짜 와 시간 을 포함 하고 많은 복잡 한 문제 에 대해 이미 만들어 진 방법 을 제공 했다. 예 를 들 어 2017 년 12 월 의 첫 번 째 월요일 등 이다.
package test;
 
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
 
/**
 * Created by lightClouds917
 * Date 2017/11/6
 * Description:Java8          
 *
 SQL  -> Java
 --------------------------
 date -> LocalDate
 time -> LocalTime
 timestamp -> LocalDateTime
 */
public class DateTest2 {
    public static void main(String[] args){
        test1();
        test2();
 
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getTime());
    }
 
    /**
     *      LocalDate
     */
    public static void test1(){
        //         2017-11-06
        LocalDate today = LocalDate.now();
        System.out.println(today);
 
        //       2017-10-08
        LocalDate today2 = LocalDate.of(2017,10,8);
        System.out.println(today2);
 
        //       2017-02-22           yyyy-MM-dd
        LocalDate today3 = LocalDate.parse("2017-02-22");
        System.out.println(today3);
 
        //       2017-11-01
        LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(firstDayOfMonth);
        //       2017-11-02    n 
        LocalDate secondDayOfMonth = today.withDayOfMonth(2);
        System.out.println(secondDayOfMonth);
 
        //       2017-02-28                 
        LocalDate lastDayOfMonth = today3.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(lastDayOfMonth);
 
 
        //  2017 12          2017-12-04
        LocalDate firstDayOf201712 = LocalDate.parse("2017-12-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        System.out.println(firstDayOf201712);
 
    }
 
    /**
     *       LocalTime
     */
    public static void test2(){
        //               17:18:41.571
        LocalTime now = LocalTime.now();
        System.out.println(now);
 
        //                 17:45:41
        LocalTime now1 = LocalTime.now().withNano(0);
        System.out.println(now1);
        //00:46:46.651            0   
        LocalTime now2 = LocalTime.now().withHour(0);
        System.out.println(now2);
 
        //      00:20:55
        LocalTime time1 = LocalTime.of(0,20,55);
        System.out.println(time1);
        //      05:43:22
        LocalTime time2 = LocalTime.parse("05:43:22");
        System.out.println(time2);
 
 
        //     2017-11-06T17:53:15.930
        LocalDateTime lt = LocalDateTime.now();
        System.out.println(lt);
    }
}

그리고:
public class TimeTest {
 
    @Test
    public void testTime() {
        LocalDateTime time = LocalDateTime.now();
 
        System.out.println(time.toString()); //     
        System.out.println(time.toLocalTime()); //    (LocalTime)
        System.out.println(time.toLocalDate()); //    (LocalDate)
        System.out.println(time.getDayOfMonth()); //            
        System.out.println(time.getDayOfWeek());  //         
        System.out.println(time.getDayOfYear());  //              
        System.out.println(time.getHour());
        System.out.println(time.getMinute());
        System.out.println(time.getMonthValue());
        System.out.println(time.getMonth());
        System.out.println("-----------------------------------");
        //     
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
        System.out.println(time.format(formatter));
        //    
        LocalDateTime startTime = LocalDateTime.of(2018, 1, 1, 20, 31, 20);
        LocalDateTime endTime = LocalDateTime.of(2018, 1, 3, 20, 31, 20);
        //    
        System.out.println(time.isAfter(startTime));
        System.out.println(time.isBefore(endTime));
 
        //    ,    
        System.out.println(time.plusYears(2)); // 2 
        System.out.println(time.plusDays(2)); //   
        System.out.println(time.minusYears(2)); //   
        System.out.println(time.minusDays(2)); //   
 
        //     (  Instant)
        System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        //    (  Instant)
        System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());
    }
 
}

좋은 웹페이지 즐겨찾기