Date date = new Date();
System.out.println(date); // Thu Feb 25 05:03:59 IST 2016
Here this Date
object contains the current date and time when this object was created.
Calendar calendar = Calendar.getInstance();
calendar.set(90, Calendar.DECEMBER, 11);
Date myBirthDate = calendar.getTime();
System.out.println(myBirthDate); // Mon Dec 31 00:00:00 IST 1990
Date
objects are best created through a Calendar
instance since the use of the data constructors is deprecated and discouraged. To do se we need to get an instance of the Calendar
class from the factory method. Then we can set year, month and day of month by using numbers or in case of months constants provided py the Calendar class to improve readability and reduce errors.
calendar.set(90, Calendar.DECEMBER, 11, 8, 32, 35);
Date myBirthDatenTime = calendar.getTime();
System.out.println(myBirthDatenTime); // Mon Dec 31 08:32:35 IST 1990
Along with date, we can also pass time in the order of hour, minutes and seconds.