Convert String to Calendar

In this section, you will learn to convert the string value into a calendar.

Convert String to Calendar

In this section, you will learn to convert the string value into a calendar.

Convert String to  Calendar

Convert String to Calendar

     

In this section, you will learn to convert the string value into a calendar.

Description of program:

The given example helps you, in converting the string value into a calendar through some Java methods and APIs. Your given date is taken as a string that is converted into a date type by using the parse() method. The parse() method invokes an object of DateFormat. The setTime(Date date) sets the formatted date into Calendar. This method invokes to Calendar object with the Date object.

 

 Here is the code of progarm:

import java.util.*;
import java.text.*;
public class StringToCalender {
 public static void main(String[] args) {
 try {  String str_date="11-June-07";
 DateFormat formatter ; 
 Date date ; 
  formatter = new SimpleDateFormat("dd-MMM-yy");
  date = (Date)formatter.parse(str_date); 
 Calendar cal=Calendar.getInstance();
 cal.setTime(date);
 System.out.println("Today is " +date );
  catch (ParseException e)
  {System.out.println("Exception :"+e);  } 
 
 }
  

Output of this program:

C:\date>javac StringToCalender.java
C:\date>java StringToCalender
Today is Mon Jun 11 00:00:00 GMT+05:30 2007M

Download this example.