Last active
January 5, 2017 13:05
-
-
Save pietrom/b685c8c9020da3409426ed04794081d0 to your computer and use it in GitHub Desktop.
Spiking JDK 8 date and time API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.Instant; | |
| import java.time.LocalDate; | |
| import java.time.LocalDateTime; | |
| import java.time.LocalTime; | |
| import java.time.ZoneId; | |
| import java.time.ZonedDateTime; | |
| import java.time.format.DateTimeFormatter; | |
| public class DateAndTimeMeltingPot { | |
| public static void main(String[] args) { | |
| // Setting up LocalDate, LocalTime, and LocalDateTime instances | |
| LocalDateTime myBirthday = LocalDateTime.of(LocalDate.of(1978, 3, 19), LocalTime.of(1, 15)); | |
| System.out.println(myBirthday); | |
| // Getting 'now' as an Instant instance | |
| Instant now = Instant.now(); | |
| System.out.println(now); | |
| // Getting 'now' as ZonedDateTime instance | |
| ZonedDateTime nowAsDateAndTime = ZonedDateTime.now(); | |
| System.out.println(nowAsDateAndTime); | |
| // Converting ZoneDateTime from one ZoneId to another one (UTC) | |
| ZonedDateTime nowAsUtcDateAndTime = ZonedDateTime.ofInstant(nowAsDateAndTime.toInstant(), ZoneId.of("UTC")); | |
| System.out.println(nowAsUtcDateAndTime); | |
| LocalDateTime parsedLocal = LocalDateTime.parse("1978-03-17T01:15:00.000Z", DateTimeFormatter.ISO_DATE_TIME); | |
| System.out.println(parsedLocal); | |
| ZonedDateTime parsedZoned = ZonedDateTime.parse("1978-03-17T01:15:00.000Z", DateTimeFormatter.ISO_DATE_TIME); | |
| System.out.println(parsedZoned); | |
| ZonedDateTime parsedZoned2 = ZonedDateTime.parse("1978-03-17T01:15:00.000+01:00", DateTimeFormatter.ISO_ZONED_DATE_TIME); | |
| System.out.println(parsedZoned2); | |
| ZonedDateTime parsedZone2toUtc = ZonedDateTime.ofInstant(parsedZoned2.toInstant(), ZoneId.of("UTC")); | |
| System.out.println(parsedZone2toUtc); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment