/home/wpollock1/public_html/Java/JavaTimeDemo.java
// A sampling of the Java 8 Date and Time APIs. This program demos various
// tasks one might do using the new APIs.
// Written 11/2014
import java.util.Scanner;
import java.time.*;
import java.time.format.*;
import java.time.zone.*;
import static java.time.temporal.TemporalAdjusters.*;
/** @author Wayne Pollock
*/
public class JavaTimeDemo {
public static void main ( String[] args ) {
// How to time something:
System.out.println( "\nStarting timer...." );
Instant start = Instant.now();
Scanner in = new Scanner( System.in );
System.out.print( "\tWhat is \"2 + 3\"? " );
String answer = in.nextLine();
Instant end = Instant.now();
System.out.println("Starting Instant: " + start
+ " (in ISO-8601 format)" );
long duration = Duration.between(start, end).getSeconds();
System.out.println( "You took " + duration
+ " seconds to work that out!\n" );
// How to format a LocalDate as a human-readable string:
DateTimeFormatter myFormat =
DateTimeFormatter.ofPattern( "MMM d yyyy hh:mm a" );
DateTimeFormatter dateOnly =
DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL );
LocalDateTime now = LocalDateTime.now();
System.out.println( "Default format of LocalDateTime: " + now );
System.out.println( "Date-only part (as FULL): "
+ dateOnly.format(now) );
System.out.println( "Custom Format: " + myFormat.format(now) );
System.out.printf( "Using printf: %tD%n", now );
// Parse a String to a LocalDateTime:
DateTimeFormatter inputFormat =
DateTimeFormatter.ofPattern( "M/d/yy h:m a" );
String someDate = "7/9/14 3:40 PM";
System.out.print( "\nEnter a date and time (e.g., \""
+ someDate + "\"): " );
LocalDateTime dateTime;
for ( ; ; ) {
try {
someDate = in.nextLine();
dateTime = LocalDateTime.parse( someDate, inputFormat );
break;
}
catch ( DateTimeParseException pe )
{ System.out.print( "Invalid date and time format, "
+ "please try again: " );
}
}
System.out.println( "You entered: " + dateTime );
// Extract parts of a LocalDateTime (two ways):
DayOfWeek day = dateTime.getDayOfWeek();
System.out.printf( "That is a %tA, or %s%n", dateTime, day );
Period period =
Period.between(dateTime.toLocalDate(), now.toLocalDate() );
// Convert Period representation into English:
String pStr = period.toString().replaceFirst( "P-?", "" );
pStr = pStr.replaceAll("Y-?", " years, " );
pStr = pStr.replaceAll("M-?", " months, " );
pStr = pStr.replaceAll("D", " days " );
System.out.print( "That is " + pStr );
if ( dateTime.isAfter(now) )
System.out.println( "from now.\n" );
else
System.out.println( "ago.\n" );
// Performing date arithmetic:
LocalDate ldom = now.with( lastDayOfMonth() ).toLocalDate();
System.out.println( "Last day of this month: " + ldom );
System.out.println( "90 days from today: " + now.plusDays(90) );
// Using ZonedDateTime:
System.out.println( "\nThe default Time zone on this computer is: "
+ ZoneId.systemDefault() );
ZonedDateTime myTime = ZonedDateTime.of( now, ZoneId.systemDefault());
System.out.println( "Some available time zones:" );
int count = 0;
for ( String tz : ZoneId.getAvailableZoneIds() ) {
System.out.println( tz );
++count;
if ( count > 15 )
break; // No need to show more zones than that.
}
System.out.print( "Pick a time zone: " );
String zoneName = in.nextLine();
ZoneId selectedZone = null;
try {
selectedZone = ZoneId.of( zoneName );
} catch ( DateTimeException tde ) {
System.err.println( "Invalid time zone entered; using UTC.");
selectedZone = ZoneOffset.UTC;
}
ZonedDateTime selectedZoneDateTime =
myTime.withZoneSameInstant( selectedZone );
System.out.println( "In that time zone, it is currently: "
+ selectedZoneDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME) );
System.out.println( "Or, in other words: " + selectedZoneDateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL) ) );
}
}