Java8에서 추가된 Date와 Time 객체를 이용한 테스트 예제를 사용해보았다.

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class Java8LocalDate {

@Test
public void localDateTest() {
System.out.println();
LocalDate today = LocalDate.now();
System.out.println(today);

int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
System.out.println(year + "." + month + "." + day);

LocalDate christMasDate = LocalDate.of(2015, 12, 25);
System.out.println(christMasDate);

LocalDate anotherDate = LocalDate.of(2015, 12, 25);
if(christMasDate.equals(anotherDate)) {
System.out.printf("same day : %s\n", anotherDate);
}

YearMonth yearMonthNow = YearMonth.now();
System.out.println("yearMonth : " + yearMonthNow);

MonthDay birthday = MonthDay.of(christMasDate.getMonth(), christMasDate.getDayOfMonth());
System.out.println("monthday : " + birthday);
MonthDay MonthDayNow = MonthDay.from(today);
if (MonthDayNow.equals(birthday)) {
System.out.println("Today is birthday.");
} else {
System.out.println("Today is not birthday");
}

LocalDate christMasEveDate = LocalDate.of(2015, 12, 24);
System.out.println(christMasEveDate.isBefore(christMasDate)); // true


System.out.println("1 week after : " + today.plus(1, ChronoUnit.WEEKS));
System.out.println("1 day after : " + today.plus(1, ChronoUnit.DAYS));
System.out.println("1 century after : " + today.plus(1, ChronoUnit.CENTURIES));
System.out.println("1 year after : " + today.plus(1, ChronoUnit.YEARS));

System.out.println("leap year : " + today.isLeapYear());
}

@Test
public void localTimeTest() {
System.out.println();
LocalTime time = LocalTime.now();
System.out.println("time : " + time);
System.out.println("2 hours after : " + time.plusHours(2));
System.out.println("3 hours before : " + time.minusHours(2));
}

@Test
public void clockTest() {
System.out.println();
Clock clock = Clock.systemUTC();
System.out.println("Clock : " + clock);
System.out.println("Clock (zone) : " + clock.getZone());

Clock laClock= Clock.system(ZoneId.of("America/Los_Angeles"));
System.out.println("LA Clock : " + laClock);

}

@Test
public void periodTest() {
System.out.println();
LocalDate startLocatDate = LocalDate.of(2015, Month.MARCH, 1);
LocalDate endLocatDate = LocalDate.of(2015, Month.OCTOBER, 1);

Period period = Period.between(startLocatDate, endLocatDate);
System.out.println(period.getMonths()); // 7달

LocalDate next = endLocatDate.plus(period);
System.out.println(next); // 7달 뒤

}

@Test
public void timeStampTest() {
System.out.println();
Instant timestamp = Instant.now();
System.out.println(timestamp);
}

@Test
public void formatterTest() {
System.out.println();
String day = "20150901";
LocalDate localDate = LocalDate.parse(day, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("day : " + localDate);

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM uuuu").withLocale(Locale.ENGLISH);
String anotherDate = "04 Aug 2015";
LocalDate parsedLocalData = LocalDate.parse(anotherDate, dateTimeFormatter);
System.out.println(anotherDate + " parses to " + parsedLocalData);
}

}


결과


day : 2015-09-01

04 Aug 2015 parses to 2015-08-04


Clock : SystemClock[Z]

Clock (zone) : Z

LA Clock : SystemClock[America/Los_Angeles]


2015-05-19T15:34:55.525Z


2015-05-20

2015.5.20

2015-12-25

same day : 2015-12-25

yearMonth : 2015-05

monthday : --12-25

Today is not birthday

true

1 week after : 2015-05-27

1 day after : 2015-05-21

1 century after : 2115-05-20

1 year after : 2016-05-20

leap year : false


7

2016-05-01


time : 00:34:55.530

2 hours after : 02:34:55.530

3 hours before : 22:34:55.530


Process finished with exit code 0




새로 추가된 Java8 Date와 Time에 좋은 자료는 아래에 있다. 아주 디테일하게 정리된 자료이다. 

http://www.ne.jp/asahi/hishidama/home/tech/java/datetime.html



Posted by '김용환'
,