Date Calendar Conversions

Two Simple Examples given to convert from Calendar to java.sql.Date and Date to java.sql.Date in this tutorial Date Calendar Conversions.
Following program on Date Calendar Conversions converts from Calendar to java.sql.Date and Date to java.sql.Date.
public class Demo
{
  public static void main(String args[])
  {
     java.util.Calendar cal = java.util.Calendar.getInstance();  // takes current system time

     System.out.println("Current time in milliseconds: " + cal.getTimeInMillis());      // prints the current system time in milliseconds
     
     System.out.println("Current system time in yyyy-mm-dd fashion : " + new java.sql.Date(cal.getTimeInMillis()));  

// Calendar to java.sql.Date
     java.sql.Date d1 = new java.sql.Date(cal.getTimeInMillis());

// java.util.Date to java.sql.Date
    java.util.Date d = new java.util.Date();
     java.sql.Date d2 = new java.sql.Date(System.currentTimeMillis());
  }
}
The next program on Date Calendar Conversions gives Date to Calendar conversion.
import java.util.*;
public class Demo
{
  public static void main(String args[])
  {
   Date d1 = new Date();                    // create java.util.Date object
   System.out.println(d1);
	
   Calendar cal1 = Calendar.getInstance();  // create Calendar object
   cal1.setTime(d1);                        // now Date comes into Calendar
   System.out.println(cal1);    
  }
}

View All java.util Examples

Leave a Comment

Your email address will not be published.