JavaTimeDemo.java
Download JavaTimeDemo.java
1: // A sampling of the Java 8 Date and Time APIs. This program demos various
2: // tasks one might do using the new APIs.
3: // Written 11/2014
4:
5: import java.util.Scanner;
6: import java.time.*;
7: import java.time.format.*;
8: import java.time.zone.*;
9: import static java.time.temporal.TemporalAdjusters.*;
10:
11: /** @author Wayne Pollock
12: */
13:
14: public class JavaTimeDemo {
15:
16: public static void main ( String[] args ) {
17: // How to time something:
18: System.out.println( "\nStarting timer...." );
19: Instant start = Instant.now();
20: Scanner in = new Scanner( System.in );
21: System.out.print( "\tWhat is \"2 + 3\"? " );
22: String answer = in.nextLine();
23: Instant end = Instant.now();
24: System.out.println("Starting Instant: " + start
25: + " (in ISO-8601 format)" );
26: long duration = Duration.between(start, end).getSeconds();
27: System.out.println( "You took " + duration
28: + " seconds to work that out!\n" );
29:
30: // How to format a LocalDate as a human-readable string:
31: DateTimeFormatter myFormat =
32: DateTimeFormatter.ofPattern( "MMM d yyyy hh:mm a" );
33: DateTimeFormatter dateOnly =
34: DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL );
35: LocalDateTime now = LocalDateTime.now();
36: System.out.println( "Default format of LocalDateTime: " + now );
37: System.out.println( "Date-only part (as FULL): "
38: + dateOnly.format(now) );
39: System.out.println( "Custom Format: " + myFormat.format(now) );
40: System.out.printf( "Using printf: %tD%n", now );
41:
42: // Parse a String to a LocalDateTime:
43: DateTimeFormatter inputFormat =
44: DateTimeFormatter.ofPattern( "M/d/yy h:m a" );
45: String someDate = "7/9/14 3:40 PM";
46: System.out.print( "\nEnter a date and time (e.g., \""
47: + someDate + "\"): " );
48: LocalDateTime dateTime;
49: for ( ; ; ) {
50: try {
51: someDate = in.nextLine();
52: dateTime = LocalDateTime.parse( someDate, inputFormat );
53: break;
54: }
55: catch ( DateTimeParseException pe )
56: { System.out.print( "Invalid date and time format, "
57: + "please try again: " );
58: }
59: }
60: System.out.println( "You entered: " + dateTime );
61:
62: // Extract parts of a LocalDateTime (two ways):
63: DayOfWeek day = dateTime.getDayOfWeek();
64: System.out.printf( "That is a %tA, or %s%n", dateTime, day );
65:
66: Period period =
67: Period.between(dateTime.toLocalDate(), now.toLocalDate() );
68: // Convert Period representation into English:
69: String pStr = period.toString().replaceFirst( "P-?", "" );
70: pStr = pStr.replaceAll("Y-?", " years, " );
71: pStr = pStr.replaceAll("M-?", " months, " );
72: pStr = pStr.replaceAll("D", " days " );
73: System.out.print( "That is " + pStr );
74: if ( dateTime.isAfter(now) )
75: System.out.println( "from now.\n" );
76: else
77: System.out.println( "ago.\n" );
78:
79: // Performing date arithmetic:
80: LocalDate ldom = now.with( lastDayOfMonth() ).toLocalDate();
81: System.out.println( "Last day of this month: " + ldom );
82: System.out.println( "90 days from today: " + now.plusDays(90) );
83:
84: // Using ZonedDateTime:
85: System.out.println( "\nThe default Time zone on this computer is: "
86: + ZoneId.systemDefault() );
87: ZonedDateTime myTime = ZonedDateTime.of( now, ZoneId.systemDefault());
88:
89: System.out.println( "Some available time zones:" );
90: int count = 0;
91: for ( String tz : ZoneId.getAvailableZoneIds() ) {
92: System.out.println( tz );
93: ++count;
94: if ( count > 15 )
95: break; // No need to show more zones than that.
96: }
97: System.out.print( "Pick a time zone: " );
98: String zoneName = in.nextLine();
99: ZoneId selectedZone = null;
100: try {
101: selectedZone = ZoneId.of( zoneName );
102: } catch ( DateTimeException tde ) {
103: System.err.println( "Invalid time zone entered; using UTC.");
104: selectedZone = ZoneOffset.UTC;
105: }
106:
107: ZonedDateTime selectedZoneDateTime =
108: myTime.withZoneSameInstant( selectedZone );
109: System.out.println( "In that time zone, it is currently: "
110: + selectedZoneDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME) );
111: System.out.println( "Or, in other words: " + selectedZoneDateTime.format(
112: DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL) ) );
113: }
114: }