Skip to content

Instantly share code, notes, and snippets.

@ikhoon
Created August 31, 2016 06:25
Show Gist options
  • Save ikhoon/7c023bbc65078e06598c769978b76b2b to your computer and use it in GitHub Desktop.
Save ikhoon/7c023bbc65078e06598c769978b76b2b to your computer and use it in GitHub Desktop.
Java8 Time API 기능 비교
import org.junit.Test;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
public class Java8TimeTest {
private String instantFormat = "2007-12-03T10:14:30.000Z";
private String offsetTimeZoneFormat = "2007-12-03T10:15:30+01:00";
private String zoneTimezoneFormat = "2007-12-03T10:15:30+01:00[Europe/Paris]";
@Test(expected = DateTimeParseException.class)
public void testInstant() throws Exception {
System.out.println(Instant.parse(instantFormat));
// java.time.format.DateTimeParseException: Text '2007-12-03T10:15:30+01:00' could not be parsed at index 19
System.out.println(Instant.parse(offsetTimeZoneFormat));
// java.time.format.DateTimeParseException: Text '2007-12-03T10:15:30+01:00[Europe/Paris]' could not be parsed at index 19
System.out.println(Instant.parse(zoneTimezoneFormat));
}
@Test(expected = DateTimeParseException.class)
public void testOffsetTime() throws Exception {
System.out.println(OffsetDateTime.parse(instantFormat));
System.out.println(OffsetDateTime.parse(offsetTimeZoneFormat));
// java.time.format.DateTimeParseException: Text '2007-12-03T10:15:30+01:00[Europe/Paris]' could not be parsed, unparsed text found at index 2
System.out.println(OffsetDateTime.parse(zoneTimezoneFormat));
}
@Test
public void testZoneTime() throws Exception {
System.out.println(ZonedDateTime.parse(instantFormat));
System.out.println(ZonedDateTime.parse(offsetTimeZoneFormat));
System.out.println(ZonedDateTime.parse(zoneTimezoneFormat));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment