Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calendar stringToCalendar(String)

Status
Not open for further replies.

LucyL

Technical User
Feb 20, 2002
113
US
Hi,
I want to convert a string to a calendar. I am using the function Calendar stringToCalendar(String). Where does the actual conversion take place. Do I need to separate each YY,MM,DD etc. I am new to java and am unsure of the syntax.
any suggestions?
help appreciated.
 
Here is an example on how to convert a string to a Date object and check if the date is valid:

import java.text.SimpleDateFormat;
import java.util.*;
import java.text.ParseException;
import java.text.DateFormat;



class DateParser
{
public static void main(String[] args) throws Exception
{
String someStringDate = "06/11/2003";

DateFormat formater = new SimpleDateFormat("MM/dd/yyyy");
//only accept exact values
formater.setLenient(true);


try
{
Date date = formater.parse(someStringDate);

System.out.println("The date entered is correct: " + formater.format(date));
}
catch(ParseException p)
{
System.out.println("This is not a valid date. The Date must be in the following format: two digit for the" +
"month, two digit for the year and four digit for the year. Please correct and resubmit.");
}
}
}
*******
When I execute it I get the following output:
java -cp . DateParser
The date entered is correct: 06/11/2003

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top