The java.util.Calendar class also encapsulates a date. The Calendar class has some more features than the java.util.Date class. It provides the ability to extract the day, month, year, and other fields corresponding to the date that the Calendar represents. So, sometimes, you may need to convert a String date to a Calendar instance.
For this, you first need to convert the String date to a java.util.Date instance via the SimpleDateFormat class. You can then use this Date to create a Calendar instance. The following code demonstrates this:
As before, this code uses a SimpleDateFormat and creates a java.util.Date instance from the String date. The Calendar.getInstance() method is invoked which creates a Calendar object corresponding to the current time. The calendar.setTime() method is then invoked with the Date object. This changes the Calendar’s time to the time in the Date object. So, this code prints the following output:
Fri Jun 12 00:00:00 IST 2015