>>My Problem is iam passing 4/6/2004
Well the format String should then be
strFormat = "m/d/yyyy";
or
strFormat = "d/m/yyyy";
Main Topics
Browse All TopicsHi
Iam trying to Conver a String(mm/dd/yyyy) to Date
Here is code
DateFormat myDateFormat = new SimpleDateFormat(strFormat
Date myDate = null;
try {
myDate = myDateFormat.parse(strDate
} catch (ParseException e) {
System.out.println("Invali
e.printStackTrace();
}
System.out.println("Finish
My Problem is iam passing 4/6/2004
but return value month is coming as 2 how to get Day of the Month
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
actually the getDay() method will return the order of the day in the week so it returns 2 which is Tuesday . since u have given April 6th which is actually Tuesday. so try
myDate.getDate();
Also when you are parsing the string for Date using SimpleDateFormat make sure u add this line too,
myDateFormat.setLenient(fa
try {
myDateFormat.setLenient(fa
myDate = myDateFormat.parse(strDate
} catch (ParseException e) {
System.out.println("Invali
e.printStackTrace();
}
as it would prevent the automatic conversion of the java to add one to the mext date is u have exceeded the Date for the given month.
ie.. if u give 31 and month as 4(April) which really has 30 days only then ur date will be shown as 1 may.
hope this will work.
The comple story from date to string and back is:
prt("Date is 25 may 2007");
prt("25/05/05");
SimpleDateFormat sim = new SimpleDateFormat("dd/MM/yy
java.util.Date datum = new java.util.Date();
try{
datum = sim.parse("25/05/05");
}catch(ParseException e){}
prt("Java data object is"+datum);
prt("");
prt("datum formatted is then ...");
SimpleDateFormat dmj= new SimpleDateFormat("dd/MMM/y
String thisDate = dmj.format(datum);
prt("And after all it's this string:"+thisDate);
}
public void prt(String s){System.out.println(s); }
One caveaty of the above posts to be aware of is if the first part of the string mattches the pattern a valid date will be returned. Below is a DateTimeUtil method I created to specify whether the whole string should be matched or not. So I can choose if date format pattern "dd/mm/yyyy" should throw an exception when the value "10/10/2007HelloWorld" is passed. You can not rely on text.length() as a format pattern may have things like mmm which may evaluate to September etc.
/**
* Get Date from Text using the date format specified
* @param dateAsText Text date portion
* @param dateFormat Format the date in text is stored as
* @param matchWholeString If you want an error to be thrown when a match is found and text exists after the pattern has been matched on the date as text value passed</b>
* <p>
* e.g.<br/>
* Pattern: dd/mm/yyyy<br/>
* Value: 10/10/1977HelloWorld
* </p>
* @return Date representation of the text date passed
* @throws ParseException if text date can not be converted to a date using the format passed
*/
public static Date getDateFromString(final String dateAsText, final String dateFormat, final boolean matchWholeString) throws ParseException {
final DateFormat df = new SimpleDateFormat(dateForma
df.setLenient(false);
final ParsePosition parsePosition = new ParsePosition(0);
final Date date = df.parse(dateAsText, parsePosition);
// Check ParsePosition is to end of string, otherwise throw ParseException error
if (matchWholeString && parsePosition.getIndex() < dateAsText.length()) {
throw new ParseException("Date pattern '" + dateFormat + "' when resolved and applied to date text '"
+ dateAsText + "' does not match whole of text string passed, only to position " + parsePosition.getIndex(), parsePosition.getIndex());
}
return date;
}
Business Accounts
Answer for Membership
by: LakshmanaRavulaPosted on 2004-04-07 at 11:03:48ID: 10776992
Here is the values
format = "MM/dd/yyyy"
date = 4/6/2004