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


Contents of page :
  • Program 1) Forming date manually using following java.util.Date’s constructor >

  • Program 2) Forming date manually using following GregorianCalendar’s constructor >

  • Program 3.1) Forming date manually using following GregorianCalendar’s set method >
  • Program 3.2) Forming date manually using following GregorianCalendar’s set method >

Program 1) Forming date manually using following java.util.Date’s constructor >
  • new Date(YEAR - 1900, MONTH, DATE);
  • new Date(YEAR - 1900, MONTH, DATE, HOUR, MINUTE, SECOND);

Note: Both constructors are deprecated

import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          //Setting MONTH, DATE and YEAR manually
          System.out.println("---Setting MONTH, DATE and YEAR manually---");
          int MONTH = 7;
          int DATE = 23;
          int YEAR = 2015;
          Date date = new Date(YEAR - 1900, MONTH, DATE);
          System.out.println("date = " + date); //Note : it will print HOUR, MINUTE and SECOND as 00:00:00
          //Setting MONTH, DATE and YEAR + HOUR, MINUTE and SECOND manually
          System.out.println("\n--Setting MONTH, DATE and YEAR + HOUR, MINUTE and SECOND manually--");
          int HOUR = 12;
          int MINUTE = 44;
          int SECOND = 33;
          date = new Date(YEAR - 1900, MONTH, DATE, HOUR, MINUTE, SECOND);
          System.out.println("date = " + date);
         
   }
}
/*OUTPUT
---Setting MONTH, DATE and YEAR manually---
date = Sun Aug 23 00:00:00 IST 2015
--Setting MONTH, DATE and YEAR +    HOUR, MINUTE and SECOND manually--
date = Sun Aug 23 12:44:33 IST 2015
*/



Program 2) Forming date manually using following GregorianCalendar’s constructor >
  • new GregorianCalendar(YEAR, MONTH, DATE);
  • new GregorianCalendar(YEAR, MONTH, DATE,    HOUR, MINUTE, SECOND);

import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          //Setting MONTH, DATE and YEAR manually
          System.out.println("---Setting MONTH, DATE and YEAR manually---");
          int MONTH = 7;
          int DATE = 23;
          int YEAR = 2015;
         
          //using GregorianCalendar constructor
          GregorianCalendar greCalendar = new GregorianCalendar(YEAR, MONTH, DATE);
          Date date = greCalendar.getTime();
         
          //Note : it will print HOUR, MINUTE and SECOND  as 00:00:00
          System.out.println("date formed from Calendar= " + date);
         
         
         
          //Setting MONTH, DATE and YEAR + HOUR, MINUTE and SECOND manually
          System.out.println("\n--Setting MONTH, DATE and YEAR + HOUR, MINUTE and SECOND manually--");
          int HOUR = 12;
          int MINUTE = 44;
          int SECOND = 33;
          //using GregorianCalendar's constructor
          greCalendar = new GregorianCalendar(YEAR, MONTH, DATE,    HOUR, MINUTE, SECOND);
          date = greCalendar.getTime();
          System.out.println("date formed from Calendar= " + date);
         
   }
}
/*OUTPUT
---Setting MONTH, DATE and YEAR manually---
date formed from Calendar= Sun Aug 23 00:00:00 IST 2015
--Setting MONTH, DATE and YEAR +    HOUR, MINUTE and SECOND manually--
date formed from Calendar= Sun Aug 23 12:44:33 IST 2015
*/



Program 3.1) Forming date manually using following GregorianCalendar’s set method >
  • greCalendar.set(YEAR, MONTH, DATE);
  • greCalendar.set(YEAR, MONTH, DATE, HOUR, MINUTE, SECOND);

import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          //Setting MONTH, DATE and YEAR manually
          System.out.println("---Setting MONTH, DATE and YEAR manually---");
          int MONTH = 7;
          int DATE = 23;
          int YEAR = 2015;
         
          GregorianCalendar greCalendar = new GregorianCalendar(); //currently gregorianCalendar consists of currentDate
          greCalendar.set(YEAR, MONTH, DATE);
      Date date = greCalendar.getTime();
         
          //Note : it will print current  HOUR, MINUTE and SECOND.
          System.out.println("date formed from Calendar= " + date);
         
         
         
          //Setting MONTH, DATE and YEAR + HOUR, MINUTE and SECOND manually
          System.out.println("\n--Setting MONTH, DATE and YEAR + HOUR, MINUTE and SECOND manually--");
          int HOUR = 12;
          int MINUTE = 44;
          int SECOND = 33;
          greCalendar.set(YEAR, MONTH, DATE, HOUR, MINUTE, SECOND);
          date = greCalendar.getTime();
          System.out.println("date formed from Calendar= " + date);
         
   }
}
/*OUTPUT
---Setting MONTH, DATE and YEAR manually---
date formed from Calendar= Sun Aug 23 22:15:20 IST 2015
--Setting MONTH, DATE and YEAR +    HOUR, MINUTE and SECOND manually--
date formed from Calendar= Sun Aug 23 12:44:33 IST 2015
*/



Program 3.2) Forming date manually using following GregorianCalendar’s set method >
  • greCalendar.set(Calendar.MONTH, 7);
  • greCalendar.set(Calendar.DATE, 23);
  • greCalendar.set(Calendar.YEAR, 2015);
  • greCalendar.set(Calendar.HOUR_OF_DAY, 12);
  • greCalendar.set(Calendar.MINUTE, 44);
  • greCalendar.set(Calendar.SECOND, 33);

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
         
          GregorianCalendar greCalendar = new GregorianCalendar();
          Date currentDate = greCalendar.getTime();
          System.out.println("currentDate              = " + currentDate);
          //Setting MONTH, DATE and YEAR + HOUR, MINUTE and SECOND manually
          //using GregorianCalendar set method
          greCalendar.set(Calendar.MONTH, 7);
          greCalendar.set(Calendar.DATE, 23);
          greCalendar.set(Calendar.YEAR, 2015);
          greCalendar.set(Calendar.HOUR_OF_DAY, 12);
          greCalendar.set(Calendar.MINUTE, 44);
          greCalendar.set(Calendar.SECOND, 33);
         
          Date dateAfterModification = greCalendar.getTime();
          System.out.println("date after modification = " + dateAfterModification);
   }
}
/*OUTPUT
currentDate              = Mon Jul 06 22:36:47 IST 2015
date after modification = Sun Aug 23 12:44:33 IST 2015
*/




RELATED LINKS>

Date comparison>

Date comparison using Date and Calendars’s before(), after() and equals() method | Also using Date.compareTo() method




Adding and subtracting from Dates>

Adding and subtracting days, month, year, hour, second, minutes from Date (using GregorianCalendar) | Adding days to manually created Date




Difference between two Dates>

2 approaches to check whether difference between given two dates is greater than specified number of days or not


No comments:

Post a Comment