Share on Google+Share on Google+

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.

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();
 System.out.println("Today is " +date );
  catch (ParseException e)
  {System.out.println("Exception :"+e);  } 

Output of this program:

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

Download this example.


Posted on: June 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Convert String to Calendar   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 9, 2011

Thank you for this short and sweet answer. Judging by the date used in the example I'm quite late to the party, but the internet is a wonderful thing and you have answered my question from 4 years in the past! Hope you still get notified/frequent this, so you can receive my thanks. -Chris