Thursday, August 6, 2009

Java : Date and Time Formats and Calendar Class

Java Date and Time Formats



Customized Date and Time Formats
PatternOutput
dd MMMM yyyy EEEE06 August 2009 Thursday
dd.MM.yy06.08.09
yyyy.MM.dd G 'at' hh:mm:ss z2009.08.06 AD at 04:26:45 IST
EEE, MMM d, ''yyThu, Aug 6, '09
hh:mm:ss:SSS a04:40:52:110 PM
HH:mm z16:41 IST
dd/MM/yyyy EE06/08/2009 Thu
dd-MMM-yyyy hh:mm:ss a EEEE06-Aug-2009 04:36:22 PM Thursday


Simple Java program to get today's date,yesterday's date and tomorrow's date using Calendar class.

/* GetDate.java */

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class GetDate
{
public static void main(String[] args)
{
//get yesterday's date
Calendar calYesterday = Calendar.getInstance();
calYesterday.add(Calendar.DAY_OF_MONTH, -1);
String yesterday=new SimpleDateFormat("dd/MM/yyyy").format(calYesterday.getTime());

//get today's date
Calendar calToday = Calendar.getInstance();
String today=new SimpleDateFormat("dd/MM/yyyy").format(calToday.getTime());

//get tomorrow's date
Calendar calTomorrow = Calendar.getInstance();
calTomorrow.add(Calendar.DAY_OF_MONTH, +1);
String tomorrow=new SimpleDateFormat("dd/MM/yyyy").format(calTomorrow.getTime());


System.out.println("Yesterday : "+yesterday);

System.out.println("Today : "+today);

System.out.println("Tomorrow : "+tomorrow);

}
}

/*******OUTPUT********/

Yesterday : 05/08/2009
Today : 06/08/2009
Tomorrow : 07/08/2009

You can also use Date class to get today's date

//String today = new SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date());


Calendar Class




Lets look at a program for testing Calendar fields.

/* CalendarField.java */

import java.util.Calendar;

public class CalendarField
{
public static void main(String[] args)
{
//get today's date
Calendar calToday = Calendar.getInstance();

//Field indicating the day of the month.
System.out.println("DATE : "+calToday.get(Calendar.DATE));

//Field indicating the month.
System.out.println("MONTH : "+calToday.get(Calendar.MONTH ));

//Field indicating the day of the month.
System.out.println("DAY_OF_MONTH : "+calToday.get(Calendar.DAY_OF_MONTH ));

//Field indicating the day of the week.
System.out.println("DAY_OF_WEEK : "+calToday.get(Calendar.DAY_OF_WEEK ));

//Field indicating the ordinal number of the day of the week within the current month.
System.out.println("DAY_OF_WEEK_IN_MONTH : "+calToday.get(Calendar.DAY_OF_WEEK_IN_MONTH ));

//Field indicating the day number within the current year.
System.out.println("DAY_OF_YEAR : "+calToday.get(Calendar.DAY_OF_YEAR ));

//Field indicating the week number within the current month.
System.out.println("WEEK_OF_MONTH : "+calToday.get(Calendar.WEEK_OF_MONTH ));

//Field indicating the hour of the day.
System.out.println("HOUR_OF_DAY : "+calToday.get(Calendar.HOUR_OF_DAY ));

//Field indicating the hour of the morning or afternoon.
System.out.println("HOUR : "+calToday.get(Calendar.HOUR ));

//Field indicating the minute within the hour.
System.out.println("MINUTE : "+calToday.get(Calendar.MINUTE ));

//Field indicating the second within the minute.
System.out.println("SECOND : "+calToday.get(Calendar.SECOND));

//Field indicating the millisecond within the second.
System.out.println("MILLISECOND : "+calToday.get(Calendar.MILLISECOND ));

//Field indicating the year.
System.out.println("YEAR : "+calToday.get(Calendar.YEAR ));

//Field indicating the week number within the current year.
System.out.println("WEEK_OF_YEAR : "+calToday.get(Calendar.WEEK_OF_YEAR ));

//Field indicating the era, e.g., AD or BC in the Julian calendar.
System.out.println("ERA : "+calToday.get(Calendar.ERA ));
}
}


/*******OUTPUT********/

DATE : 6
MONTH : 7
DAY_OF_MONTH : 6
DAY_OF_WEEK : 5
DAY_OF_WEEK_IN_MONTH : 1
DAY_OF_YEAR : 218
WEEK_OF_MONTH : 2
HOUR_OF_DAY : 19
HOUR : 7
MINUTE : 40
SECOND : 47
MILLISECOND : 157
YEAR : 2009
WEEK_OF_YEAR : 32
ERA : 1