java

java jdk 1.8 부터 나온 date 사용방법

초이짬 2016. 12. 9. 18:57
728x90

기존 1.1 버전 부터 쓰던 calendar 를 대신하는 java date 사용이 1.8 부터 새 버전이 나왔다

전에 얼핏 보고 사용해봐야지 하다가 이번 프로젝트에서 처음 써보게 된다.

아래에 보면 과거와 다르게 포맷터를 사용안해도 자연스럽게 변환해주는것을 알수 있다


그리고 날짜 비교도 쉽고 여러모로 편한것을 알 수 있다


왜 이제야 나왓는지 의문이다..

LocalDate currentDate = LocalDate.now();
LocalDate specific = LocalDate.of(2016, Month.DECEMBER, 10);
LocalTime currentTime = LocalTime.now(); // current time
LocalTime midday = LocalTime.of(12, 0); // 12:00
LocalTime afterMidday = LocalTime.of(13, 30, 15); // 13:30:15

// 12345th second of day (03:25:45)
LocalTime fromSecondsOfDay = LocalTime.ofSecondOfDay(12345);

// dates with times, e.g. 2016-12-18 19:08:37.950
LocalDateTime currentDateTime = LocalDateTime.now();

// 2016-12-02 12:30
LocalDateTime secondAug2014 = LocalDateTime.of(2016, 10, 2, 12, 30);

// 2016-12-24 12:00
LocalDateTime christmas2014 = LocalDateTime.of(2016, Month.DECEMBER, 24, 12, 0);
System.out.println(currentDate);
System.out.println(specific);
System.out.println(currentTime);
System.out.println(currentDate);
System.out.println(midday);
System.out.println(afterMidday);
System.out.println(fromSecondsOfDay);
System.out.println(currentDateTime);
System.out.println(secondAug2014);
System.out.println(christmas2014);

System.out.println();
System.out.println();
System.out.println("초::::"+currentTime.getSecond());
System.out.println();
System.out.println();
 

//================지역 날짜 확인====================
// current (local) time in 한국 서울
  LocalTime currentTimeInAsiaSeoul = LocalTime.now(ZoneId.of("Asia/Seoul"));

// current time in UTC time zone
LocalTime nowInUtc = LocalTime.now(Clock.systemUTC());

System.out.println("currentTimeInLosAngeles :"+currentTimeInAsiaSeoul);
System.out.println("nowInUtc :"+nowInUtc );


//================날짜 비교====================
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("================날짜 비교====================");
System.out.println();
LocalDate date = LocalDate.of(2016, 10, 15); // 2016-10-15

boolean isBefore = LocalDate.now().isBefore(date); // false

// information about the month
Month february = date.getMonth(); // FEBRUARY
int februaryIntValue = february.getValue(); // 2
int minLength = february.minLength(); // 28
int maxLength = february.maxLength(); // 29
Month firstMonthOfQuarter = february.firstMonthOfQuarter(); // JANUARY

// information about the year
int year = date.getYear(); // 2016
int dayOfYear = date.getDayOfYear(); // 46
int lengthOfYear = date.lengthOfYear(); // 365
boolean isLeapYear = date.isLeapYear(); // false

DayOfWeek dayOfWeek = date.getDayOfWeek();
int dayOfWeekIntValue = dayOfWeek.getValue(); // 6
String dayOfWeekName = dayOfWeek.name(); // SATURDAY

int dayOfMonth = date.getDayOfMonth(); // 15
LocalDateTime startOfDay = date.atStartOfDay(); // 2016-12-15 00:00

// time information
LocalTime time = LocalTime.of(15, 30); // 15:30:00
int hour = time.getHour(); // 15
int second = time.getSecond(); // 0
int minute = time.getMinute(); // 30
int secondOfDay = time.toSecondOfDay(); // 55800
System.out.println(date);
System.out.println(isBefore);
System.out.println(februaryIntValue);
System.out.println(februaryIntValue);
System.out.println(minLength);
System.out.println(maxLength);
System.out.println(firstMonthOfQuarter);
System.out.println(year);
System.out.println(dayOfYear);
System.out.println(lengthOfYear);
System.out.println(isLeapYear);
System.out.println(dayOfWeek);
System.out.println(dayOfWeekIntValue);
System.out.println(dayOfWeekName);
System.out.println(dayOfMonth);
System.out.println(startOfDay);
System.out.println(time);
System.out.println(hour);
System.out.println(second);
System.out.println(minute);
System.out.println(secondOfDay);




728x90