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


Contents of page :
  • Program 1.1) Convert GregorianCalendar to Date
  • Program 1.2) Convert Calendar to Date

  • Program 2.1) Convert Date to Calendar
  • Program 2.2) Convert Date to GregorianCalendar



Program 1.1) Convert GregorianCalendar to Date
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          GregorianCalendar currentDate = new GregorianCalendar();
          Date date=currentDate.getTime();
          System.out.println("Date converted from GregorianCalendar= " + date);
   }
}
/*OUTPUT
Date converted from GregorianCalendar= Mon Jul 06 20:08:38 IST 2015
*/



Program 1.2) Convert Calendar to Date
import java.util.Calendar;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String args[]) {
          Calendar currentDate = Calendar.getInstance();
          Date date=currentDate.getTime();
          System.out.println("Date converted form Calendar= " + date);
   }
}
/*OUTPUT
Date converted form Calendar= Mon Jul 06 20:08:48 IST 2015
*/





Program 2.1) Convert Date to Calendar
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 Date date = sdf.parse("21-02-2015");
                 System.out.println("date = " + date);
                 System.out.println("\n----Now, convert date to Calendar----");
                 Calendar calendar = Calendar.getInstance();
                 calendar.setTime(date); // convert date to calendar
                 System.out.println(calendar.getTime());
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
date = Sat Feb 21 00:00:00 IST 2015
----Now, convert date to Calendar----
Sat Feb 21 00:00:00 IST 2015
*/


Program 2.2) Convert Date to GregorianCalendar
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 Date date = sdf.parse("21-02-2015");
                 System.out.println("date = " + date);
         
                 System.out.println("\n----Now, convert date to GregorianCalendar---");
                 GregorianCalendar gregorianCalendar = new GregorianCalendar();
                 gregorianCalendar.setTime(date); //convert date to GregorianCalendar
                 System.out.println(gregorianCalendar.getTime());
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
date = Sat Feb 21 00:00:00 IST 2015
----Now, convert date to GregorianCalendar---
Sat Feb 21 00:00:00 IST 2015
*/



RELATED LINKS>
Creating Date >

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




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>

Check whether a given date lies between other two dates


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


Must read for you :

No comments:

Post a Comment