Wednesday, May 11, 2011

Date, GregorianCalendar, SimpleDateFormat Classes Explained

import java.util.*;
import java.text.SimpleDateFormat;
public class MyCalendar
{
private Date curr_date;
private GregorianCalendar greg_targ_date;
private String display_curr_date;
private SimpleDateFormat sf;
private String msg;

public MyCalendar()
{
curr_date = new Date();
//By default instantiation of this Date returns current Date. The date returned is in a long format.
//If you would like a simplified format here is how the Date object is converted into simple text format which is of type String.
sf = new SimpleDateFormat("K:mm a, z");
display_curr_date = sf.format(curr_date);

//You can use the gregorian format Object to set Year, month, day and time specifically.
greg_targ_date = new GregorianCalendar();
greg_targ_date.set(greg_targ_date.YEAR,3000);
greg_targ_date.set(greg_targ_date.MONTH,0);
greg_targ_date.set(greg_targ_date.DATE,1);
greg_targ_date.set(greg_targ_date.AM_PM,0);
greg_targ_date.set(greg_targ_date.HOUR,0);
greg_targ_date.set(greg_targ_date.MINUTE,0);
greg_targ_date.set(greg_targ_date.SECOND,0);
}
//This function returns the current date/time converted into simple date format object.
public String getCurrentDate()
{
return (display_curr_date);
}

//This function calculates the number of days remaining from today's date/time to Year 3000 stored in targ_date
public String getMessage()
{

Date targ_date= (Date)greg_targ_date.getTime();

// Gregorian calendar Date object is converted into Date object using getTime() method.
// When getTime() method is applied on a Date object, the Date/Time is returned in milliseconds.
// Therefore (greg_targ_date.getTime()).getTime() would yield the date/time in milliseconds.
switch(targ_date.compareTo(curr_date)) // Comparing the Date object using compareTo since Date object implements Comparable Interface.
{
case 1:
long milseconds = targ_date.getTime() - curr_date.getTime(); // Difference in milliseconds between target date and current date.
long msInDay = 1000 * 60 * 60 * 24;
long daysToGo = (long) (milseconds / msInDay);
return ("The number of days to go is " + daysToGo + " from target date");
case 0:
return ("Current date equal to target date");
case -1:
default:
return ("Expired Counter");
}
}


public static void main (String args[])
{
MyCalendar mc = new MyCalendar();
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Date converted into SimpleFormat (displaying time and AM/PM)=" + mc.getCurrentDate());
System.out.println("Display current date/time using Date Object=" + mc.curr_date);
System.out.println("Display current date in milliseconds=" +mc.curr_date.getTime());
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Example displaying GregorianCalendar object applied getTime() method multiple times");
System.out.println("--------------------------------------------------------------------------------");
GregorianCalendar temp = new GregorianCalendar();
System.out.println("Original GregorianCalendar Object(temp)=" + temp);
System.out.println("");
System.out.println("");
System.out.println("Applying getTime() on GregorianCalendar Object yields Date Object");
System.out.println("temp.getTime()=" + temp.getTime());
System.out.println("");
System.out.println("");
System.out.println("Applying getTime() on Date Object yields Date in seconds(datatype = long)");
System.out.println("temp.getTime()).getTime()=" + (temp.getTime()).getTime());
System.out.println("--------------------------------------------------------------------------------");
//Calling the method which displays number of days from today until Year 3000 (target date)
System.out.println(mc.getMessage());
}

}
The java output is displayed below

No comments: