Skip to content

Instantly share code, notes, and snippets.

@csturtevant
Created July 17, 2016 17:45
Show Gist options
  • Save csturtevant/cd923946f8b2c2420765062fed15f7b6 to your computer and use it in GitHub Desktop.
Save csturtevant/cd923946f8b2c2420765062fed15f7b6 to your computer and use it in GitHub Desktop.
Java 8 Date Time Example with Time Zone and Printed List of Available Time Zones
public class Program {
public static void main(String args[]) {
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
System.out.println(dayOfWeek); // WEDNESDAY
Month month = sylvester.getMonth();
System.out.println(month); // DECEMBER
long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
System.out.println(minuteOfDay); // 1439
Instant instant = sylvester
.atZone(ZoneId.of("America/New_York"))
.toInstant();
System.out.println("This is the instant " + instant);
//This for each will print the list of available time zones to be used similarlly to the above "America/New_York"
for (String timeZoneID : TimeZone.getAvailableIDs()) {
System.out.println(timeZoneID);
}
Date legacyDate = Date.from(instant);
System.out.println(legacyDate); // Wed Dec 31 20:59:59 PST 2014
DateTimeFormatter formatter =
DateTimeFormatter
.ofPattern("MMM dd, yyyy - HH:mm");
LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);
String string = parsed.format(formatter);
System.out.println(string); // Nov 03, 2014 - 07:13
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment