/home/wpollock1/public_html/Java/DateTime.java
// DateTime.java - A demonstration of several Java date and time
// classes and techniques. (Note SQL databases use time and date
// formats defined by classes in the package java.sql.)
//
// Written 1999 by Wayne Pollock, Tampa, Florida USA.
// Updated 2006 to use Scanner, and to show SimpleDateFormat class
import java.util.*; // for Date and Calendar
import java.text.*; // for DateFormat and TimeFormat
class DateTime
{
private static Date now = new Date();
public static void main ( String [] args )
{
// *************************************************************************
// Here's an example showing how to time something:
System.out.println( "\nStarting timer...." );
Date start = new Date();
Scanner in = new Scanner( System.in );
System.out.print( "\tWhat is \"2 + 3\"? " );
String name = in.nextLine();
Date end = new Date();
long duration = Math.round( (end.getTime() - start.getTime()) / 1000.0 );
System.out.println( "You took " + duration
+ " seconds to work that out!" );
// *************************************************************************
// Here's how to format a Date as a human-readable string:
// One command to format both the date and time:
String dt = DateFormat.getDateTimeInstance().format( now );
System.out.println( "\nThe Date and time is now: " + dt + "." );
// You can get the date and time strings seperately:
String d1 = DateFormat.getDateInstance().format( now );
String t1 = DateFormat.getTimeInstance().format( now );
System.out.println( "\n\tDefault formats:" );
System.out.print( "At the tone the time will be " );
System.out.println( d1 + " " + t1 + " ...Beeeeep!" );
// You can use SHORT, MEDIUM, LONG, or FULL formats too:
String d2 = DateFormat.getDateInstance(DateFormat.FULL).format( now );
String t2 = DateFormat.getTimeInstance(DateFormat.SHORT).format( now );
System.out.println( "\n\tFULL date and SHORT time formats:" );
System.out.print( "At the tone the time will be " );
System.out.println( d2 + " " + t2 + " ...Beeeeep!" );
// *************************************************************************
// You can use SimpleDateFormat to format (or parse) dates and
// times in any format. Here we format the current date as an
// RFC-5322 (formally RFC-2822) standard date and time string:
SimpleDateFormat rfc2822Fmt = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss Z (zzz)" );
System.out.print( "\nRFC-2822 standard Date format for today: " );
System.out.println( rfc2822Fmt.format(now) );
// Much of the world uses the flexible ISO-8601 formatted dates and times;
// web technology uses a (nearly identical) variant called RFC-3339:
TimeZone tz = TimeZone.getTimeZone( "UTC" );
DateFormat iso8601Fmt = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm'Z'" );
iso8601Fmt.setTimeZone( tz );
System.out.print( "\nISO-8601 standard Date format for today: " );
System.out.println( iso8601Fmt.format(now) );
// *************************************************************************
// You can use a DateFormat object to convert a string to a Date object,
// (but you must use the right formats!):
Date d = null;
DateFormat df = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT );
df.setLenient( true ); // May allow some sloppyness in user input.
String someDate = df.format(now);
System.out.print( "\nEnter a date and time (e.g., \""
+ someDate + "\"): " );
for ( ; ; )
{
try
{ someDate = in.nextLine();
d = df.parse( someDate );
break;
}
catch ( ParseException pe )
{ System.out.print( "Invalid date and time format, "
+ "please try again: " );
}
}
// The above code could be modified, to try several different
// DateFormat objects, one aftert the other, and break out if
// any of them match. (Left as an exercise to the reader.)
String d3 = DateFormat.getDateInstance( DateFormat.MEDIUM ).format( d );
System.out.println( "\nThe date part of \"" + someDate +"\" is: " + d3 );
String t3 = DateFormat.getTimeInstance( DateFormat.LONG ).format( d );
System.out.println( "and the time part is: " + t3 + "." );
// In production-quality code, consider not doing this at all.
// Instead, use a GUI date-picker widget, or have separate inputs
// for the month, day, and year (using drop-down lists, for example).
// *************************************************************************
// Here's how to compare two Dates:
if ( d.after( now ) )
System.out.println( "(And that date is in the future!)" );
else
System.out.println( "(And that date is in the past!)" );
// *************************************************************************
// You can use a Calendar object to get parts of a date, or to
// construct a Date:
Calendar today = Calendar.getInstance(); // or: new GregorianCalendar();
today.setTime( now );
// Which day of the week is it? Here's how to find out:
String weekday[] = new DateFormatSymbols().getWeekdays();
String day = weekday[ today.get( Calendar.DAY_OF_WEEK ) ];
System.out.println( "\n\nToday is " + day + "." );
// Here's how to construct a date. (Note you can use
// Calendar.getTime() to convert to a Calendar to a Date):
Calendar christmas = Calendar.getInstance(); //or new GregorianCalendar();
christmas.set( today.get( Calendar.YEAR ), Calendar.DECEMBER, 25 );
int daysLeft = christmas.get(Calendar.DAY_OF_YEAR)
- today.get(Calendar.DAY_OF_YEAR);
System.out.println( "\nOnly " + daysLeft + " shopping days left "
+ "until Christmas!" );
}
}