Java Programming II on YouTube:
https://www.youtube.com/playlist?list=PLLIqcoGpl73hdVsj_auS8gs2cTFG1pYZx
Represents date/time with milliseconds accuracy
java.util.GregorianCalendar;
Common constructors of the GregorianCalendar class
GregorianCalendar() GregorianCalendar( year, month, day ) GregorianCalendar( year, month, day, hour, minute ) GregorianCalendar( year, month, day, hour, minute, second )
A statement that gets the current date:
GregorianCalendar now = new GregorianCalendar();
Statements that create specific dates:
// hardcoded dates GregorianCalendar startDate = new GregorianCalendar( 2009, 0, 30 ); GregorianCalendar startTime = new GregorianCalendar( 2012, 6, 20, 15, 30 ); // using variables GregorianCalendar birthDate = new GregorianCalendar( birthYear, birthMonth, birthDay );
The Calendar is a superclass of GregorianCalendar:
java.util.Calendar;
Common fields of the Calendar class
DATE DAY_OF_MONTH DAY_OF_WEEK DAY_OF_YEAR HOUR HOUR_OF_DAY MINUTE MONTH SECOND YEAR MONDAY…SUNDAY JANUARY...DECEMBER
Common methods of the Calendar and GregorianCalendar classes
set(intYear, intMonth, ...) set(intField, intValue) setTime(Date) // use Date object to update the date/time add(intField, intValue) // adds/subtracts the intValue roll(intField, intValue) // same as add() but does not affect other fields roll(intField, booleanValue) // +1 if true, -1 if false get(intField) getTime() // convert GregorianCalendar to Date
where the intField is one of the Calendar class fields.
Code that changes a GregorianCalendar object
GregorianCalendar endDate = new GregorianCalendar(2010, 0, 1); // January 1, 2010 endDate.set(2010, 2, 30); // March 30, 2010 endDate.set(2010, Calendar.MARCH, 30); // March 30, 2010 endDate.set(Calendar.MONTH, Calendar.JANUARY); // January 30, 2010 endDate.add(Calendar.MONTH, 5); // June 30, 2010 endDate.add(Calendar.MONTH, 14); // August 30, 2011 endDate.roll(Calendar.MONTH, 14); // October 30, 2011 endDate.roll(Calendar.MONTH, true); // November 30, 2011: +1 tick endDate.roll(Calendar.DAY_OF_MONTH, false); // November 29, 2011: -1 tick
Code that accesses fields in a GregorianCalendar object
GregorianCalendar birthday = new GregorianCalendar( 2010, Calendar.FEBRUARY, 4); // Thurs, Feb 4, 2010 int year = birthday.get( Calendar.YEAR); // year is 2010 int month = birthday.get( Calendar.MONTH); // month is 1 int day = birthday.get( Calendar.DAY_OF_MONTH); // day is 4 int dayOfWeek = birthday.get(Calendar.DAY_OF_WEEK); // dayOfWeek is 5 int dayOfYear = birthday.get(Calendar.DAY_OF_YEAR); // dayOfYear is 35
Defined as
java.util.Date;
the Date class stores date/time values with accuracy to a millisecond since midnight January 1, 1970 UTC.
Common constructors of the Date class
Date( )
Date( longMilliseconds )
Common methods of the Date class:
getTime( )
toString( )
A statement that converts a GregorianCalendar object to a Date object
Date endDate = gregEndDate.getTime();
A statement that gets a Date object for the current date/time
Date now = new Date();
Statements that convert Date objects to String and long variables
String nowAsString = now.toString(); // converts to a string long nowInMS = now.getTime(); // converts to milliseconds
Code that calculates the number of days between two dates
Date startDate = gregStartDate.getTime(); Date endDate = gregEndDate.getTime(); long startDateMS = startDate.getTime(); long endDateMS = endDate.getTime(); long elapsedMS = endDateMS – startDateMS; long elapsedDays = elapsedMS / (24 * 60 * 60 * 1000);
The DateFormat class converts Date object to String using various formats
java.text.DateFormat;
Common static methods of the DateFormat class
getDateInstance() getTimeInstance() getDateTimeInstance() getDateInstance( intField ) getTimeInstance( intField ) getDateTimeInstance( intField, intField )
where intField is DateFormat.SHORT, MEDIUM, LONG, or FULL
Common fields of the DateFormat class
Style Date example Time example ------- ---------------------------- ---------------- SHORT 12/31/10 12:00 AM MEDIUM Dec 31, 2010 7:30:00 PM LONG December 31, 2010 7:30:00 AM PST FULL Saturday, December 31, 2010 7:30:00 AM PST
Common method of the DateFormat class is
format( Date )
which returns the formatted String
Date now = new Date(); DateFormat defaultDate = DateFormat.getDateTimeInstance(); String nowString = defaultDate.format(now);
GregorianCalendar gregEndDate = new GregorianCalendar( 2010, 11, 31, 7, 30 ); Date endDate = gregEndDate.getTime(); DateFormat defaultDate = DateFormat.getDateInstance(); String endDateString = defaultDate.format( endDate );
Code that overrides the default date and time formats:
DateFormat shortDate = DateFormat.getDateInstance( DateFormat.SHORT ); DateFormat shortTime = DateFormat.getTimeInstance( DateFormat.SHORT ); DateFormat shortDateTime = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT );
public class DateUsageDemo { Date invoiceDate = DateUtils.getCurrentDate(); public Date getCurrentDate() { return currentDate; } public String getFormattedDate() { DateFormat shortDate = DateFormat.getDateInstance( DateFormat.SHORT ); return shortDate.format( currentDate ); } }// class DateUsageDemo
The proposed DateUtils class represents common operations needed by business applications:
import java.util.*;
public class DateUtils {
static final int MILLS_IN_DAY = 24 * 60 * 60 * 1000;
// Returns Date with current date and removes time component
public static Date getCurrentDate() {
GregorianCalendar currentDate =
new GregorianCalendar();
currentDate.set( Calendar.HOUR, 0 );
currentDate.set( Calendar.MINUTE, 0 );
currentDate.set( Calendar.SECOND, 0 );
return currentDate.getTime();
}
// Returns Date for the specified date
public static Date createDate(
int year, int month, int day
)
{
GregorianCalendar date =
new GregorianCalendar( year, month, day );
return date.getTime();
}
// Returns Date with the time component removed
public static Date stripTime( Date date )
{
GregorianCalendar currentDate =
new GregorianCalendar();
currentDate.setTime( date );
currentDate.set( Calendar.HOUR, 0 );
currentDate.set( Calendar.MINUTE, 0 );
currentDate.set( Calendar.SECOND, 0 );
return currentDate.getTime();
}
// Calculates diff between two Date objects in days
public static int daysDiff( Date date1, Date date2 )
{
date1 = stripTime( date1 );
date2 = stripTime( date2 );
long longDate1 = date1.getTime();
long longDate2 = date2.getTime();
long longDiff = longDate2 - longDate1;
return ( int ) ( longDiff / MILLS_IN_DAY );
}
}// class DateUtils
Code that uses some of the DateUtils methods:
GregorianCalendar currentGC = new GregorianCalendar(); int currentYear = currentGC.get( Calendar.YEAR ); Date currentDate = DateUtils.getCurrentDate(); Date christmas = DateUtils.createDate( currentYear, Calendar.DECEMBER, 25 ); int daysToChristmas = DateUtils.daysDiff( currentDate, christmas ); DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.LONG ); String formattedDate = dateFormat.format( currentDate ); System.out.println( "Today is " + formattedDate ); System.out.println( "There are " + daysToChristmas + " days until Christmas." );
Resulting output:
Today is July 11, 2011 There are 166 days until Christmas.