Calendar Class in Java
Calendar Class in Java
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Used to provide an example that exercises most of the functionality of the
* java.util.Calendar Class
* @author Vivek Venkatesh G
* @version
*/
public class CalendarExample {
/**
* Calendar's getTime() method returns a Date Object
* This can then be passed to println() to print today's date (and time) in the traditional format
* @param No parameter for this function
* @return No return value for this function
*/
public static void doCalendarTimeExample()
{
System.out.println("CURRENT DATE/TIME\n\n");
Date now = Calendar.getInstance().getTime();
System.out.println("Calendar.getInstance().getTime():"+ now);
System.out.println();
}
/**
* Simple Date Format from java.text.package
* @param for this no parameter required
*/
public static void doSimpleDateFormat()
{
System.out.println("Simple Date Format \n\n");
// Get Today's Date
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println(" It is now :"+ formatter.format(now.getTime()));
System.out.println();
}
/**
* Simple Date difference from java.text.Package
*/
public static void doDataDifference()
{
System.out.println("DIFFERENCE BETWEEN TWO DATES\n\n");
Date startDate1 = new GregorianCalendar(1994,02,14,14,00).getTime();
Date endDate1 = new Date();
long diff = endDate1.getTime() - startDate1.getTime();
System.out.println("Difference between " + endDate1);
System.out.println(" and "+ startDate1 + "is" + (diff/(1000L*60L*60L*24L)) + "days");
System.out.println();
}
public static void doGetMethods()
{
System.out.println("CALENDAR GET METHODS");
Calendar c=Calendar.getInstance();
System.out.println("YEAR : " + c.get(Calendar.YEAR));
System.out.println("MONTH : " + c.get(Calendar.MONTH));
System.out.println("DAY_OF_MONTH : " + c.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK : " + c.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_YEAR : " + c.get(Calendar.DAY_OF_YEAR));
System.out.println("WEEK_OF_YEAR : " + c.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH : " + c.get(Calendar.WEEK_OF_MONTH));
System.out.println("DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("HOUR : " + c.get(Calendar.HOUR));
System.out.println("AM_PM : " + c.get(Calendar.AM_PM));
System.out.println("HOUR OF DAY(24-hour) : " + c.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE : " + c.get(Calendar.MINUTE));
System.out.println("SECOND : " + c.get(Calendar.SECOND));
System.out.println();
}
public static void main(String[] args)
{
System.out.println();
doCalendarTimeExample();
doSimpleDateFormat();
doDataDifference();
doGetMethods();
}
}
Download the Source Code
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Used to provide an example that exercises most of the functionality of the
* java.util.Calendar Class
* @author Vivek Venkatesh G
* @version
*/
public class CalendarExample {
/**
* Calendar's getTime() method returns a Date Object
* This can then be passed to println() to print today's date (and time) in the traditional format
* @param No parameter for this function
* @return No return value for this function
*/
public static void doCalendarTimeExample()
{
System.out.println("CURRENT DATE/TIME\n\n");
Date now = Calendar.getInstance().getTime();
System.out.println("Calendar.getInstance().getTime():"+ now);
System.out.println();
}
/**
* Simple Date Format from java.text.package
* @param for this no parameter required
*/
public static void doSimpleDateFormat()
{
System.out.println("Simple Date Format \n\n");
// Get Today's Date
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println(" It is now :"+ formatter.format(now.getTime()));
System.out.println();
}
/**
* Simple Date difference from java.text.Package
*/
public static void doDataDifference()
{
System.out.println("DIFFERENCE BETWEEN TWO DATES\n\n");
Date startDate1 = new GregorianCalendar(1994,02,14,14,00).getTime();
Date endDate1 = new Date();
long diff = endDate1.getTime() - startDate1.getTime();
System.out.println("Difference between " + endDate1);
System.out.println(" and "+ startDate1 + "is" + (diff/(1000L*60L*60L*24L)) + "days");
System.out.println();
}
public static void doGetMethods()
{
System.out.println("CALENDAR GET METHODS");
Calendar c=Calendar.getInstance();
System.out.println("YEAR : " + c.get(Calendar.YEAR));
System.out.println("MONTH : " + c.get(Calendar.MONTH));
System.out.println("DAY_OF_MONTH : " + c.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK : " + c.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_YEAR : " + c.get(Calendar.DAY_OF_YEAR));
System.out.println("WEEK_OF_YEAR : " + c.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH : " + c.get(Calendar.WEEK_OF_MONTH));
System.out.println("DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("HOUR : " + c.get(Calendar.HOUR));
System.out.println("AM_PM : " + c.get(Calendar.AM_PM));
System.out.println("HOUR OF DAY(24-hour) : " + c.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE : " + c.get(Calendar.MINUTE));
System.out.println("SECOND : " + c.get(Calendar.SECOND));
System.out.println();
}
public static void main(String[] args)
{
System.out.println();
doCalendarTimeExample();
doSimpleDateFormat();
doDataDifference();
doGetMethods();
}
}
Download the Source Code
0 comments: