O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Converting Between Date and Calendar Objects

// Date to Calendar conversion
											Date myDate = new java.util.Date();
											Calendar myCal = Calendar.getInstance();
											myCal.setTime(myDate);
											// Calendar to Date conversion
											Calendar newCal = Calendar.getInstance();
											Date newDate = newCal.getTime();

If you’re working with times and dates, you’ll often find it necessary to convert between java Date and Calendar objects. Fortunately, as shown in the phrase, this is a very simple thing to do. A Calendar object has a setTime() method that takes a java.util.Date object as a parameter and sets the Calendar object to the date and time contained in the Date object passed in. To convert in the opposite direction, you can use the getTime() method of the Calendar class, which ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required