Creating Date in java using Calendar, GregorianCalendar and java.util.Date | Getting Year, Month, Date, Hour, Minute, Second from current date


Contents of page :
  • Program 1) Creating date using java.util.Date

  • Program 2) Creating date using GregorianCalendar

  • Program 3) Creating date using Calendar

  • Program 4) Getting
  • Year, Month, Date,  Hour, Minute, Second
  • DAY_OF_WEEK, WEEK_OF_MONTH, WEEK_OF_YEAR, HOUR_OF_DAY, MILLISECOND
from current date using GregorianCalendar



Program 1) Creating date using java.util.Date

import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          Date currentDate = new Date();
          System.out.println(currentDate);
   }
}
/*OUTPUT
Mon Jul 06 19:46:42 IST 2015
*/




Program 2) Creating date using GregorianCalendar

import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          GregorianCalendar currentDate = new GregorianCalendar();
          System.out.println(currentDate);
   }
}
/*OUTPUT
java.util.GregorianCalendar[time=1436192161850,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=6,WEEK_OF_YEAR=28,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=187,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=46,SECOND=1,MILLISECOND=850,ZONE_OFFSET=19800000,DST_OFFSET=0]
*/



Program 3) Creating date using Calendar

import java.util.Calendar;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          Calendar currentDate = Calendar.getInstance();
          System.out.println(currentDate);
          // OR
          Calendar currentDate1 = new GregorianCalendar();
          System.out.println("\n"+currentDate1);
   }
}
/*OUTPUT
java.util.GregorianCalendar[time=1436192184868,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=6,WEEK_OF_YEAR=28,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=187,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=46,SECOND=24,MILLISECOND=868,ZONE_OFFSET=19800000,DST_OFFSET=0]
java.util.GregorianCalendar[time=1436192184868,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=6,WEEK_OF_YEAR=28,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=187,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=46,SECOND=24,MILLISECOND=868,ZONE_OFFSET=19800000,DST_OFFSET=0]
*/




Program 4) Getting
  • Year, Month, Date,  Hour, Minute, Second
  • DAY_OF_WEEK, WEEK_OF_MONTH, WEEK_OF_YEAR, HOUR_OF_DAY, MILLISECOND
from current date using GregorianCalendar

import java.util.Calendar;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          GregorianCalendar currentDate = new GregorianCalendar();
          System.out.println("Current Date="+currentDate.getTime());
          System.out.println("\n---------Most frequently used--------");
          int DATE = currentDate.get(Calendar.DATE);
          int MONTH = currentDate.get(Calendar.MONTH);
          int YEAR = currentDate.get(Calendar.YEAR);
         
          System.out.println("DATE=" + DATE);
          System.out.println("MONTH=" + MONTH);
          System.out.println("YEAR=" + YEAR);
         
          int HOUR = currentDate.get(Calendar.HOUR); //In 12 hour format
          int MINUTE = currentDate.get(Calendar.MINUTE);
          int SECOND = currentDate.get(Calendar.SECOND);
          System.out.println("HOUR=" + HOUR);
          System.out.println("MINUTE=" + MINUTE);
          System.out.println("SECOND=" + SECOND);
          System.out.println("\n---------Less used---------");
          int DAY_OF_WEEK  = currentDate.get(Calendar.DAY_OF_WEEK);
          int WEEK_OF_MONTH= currentDate.get(Calendar.WEEK_OF_MONTH);
          int WEEK_OF_YEAR = currentDate.get(Calendar.WEEK_OF_YEAR);
         
          System.out.println("DAY_OF_WEEK=" + DAY_OF_WEEK);
          System.out.println("WEEK_OF_MONTH=" + WEEK_OF_MONTH);
          System.out.println("WEEK_OF_YEAR=" + WEEK_OF_YEAR);
         
          int HOUR_OF_DAY  = currentDate.get(Calendar.HOUR_OF_DAY);  //In 24 hour format
          int MILLISECOND= currentDate.get(Calendar.MILLISECOND);
          System.out.println("HOUR_OF_DAY=" + HOUR_OF_DAY);
          System.out.println("MILLISECOND=" + MILLISECOND);
         
   }
}
/*OUTPUT
Current Date=Mon Jul 06 19:59:17 IST 2015
---------Most frequently used--------
DATE=6
MONTH=6
YEAR=2015
HOUR=7
MINUTE=59
SECOND=17
---------Less used---------
DAY_OF_WEEK=2
WEEK_OF_MONTH=2
WEEK_OF_YEAR=28
HOUR_OF_DAY=19
MILLISECOND=267
*/


RELATED LINKS>

Convert Calendar and GregorianCalendar to Date | Convert Date to Calendar and GregorianCalendar in java




Creating Date manually - by passing users parameter>

Forming date MANUALLY using java.util.Date’s constructor, GregorianCalendar’s constructor and set method in java



Convert String to Date>

Using SimpleDateFormat to convert String to Date - pass date, month, year, hour, minute, second, day, Am/Pm(E), time zone(z) || Enabling strict parsing of String by setting SimpleDateFormat’s setLenient(false)


No comments:

Post a Comment