aspose file tools*
The moose likes Java in General and the fly likes Convert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert "mon dd yyyy" to "mm/dd/yyyy"" Watch "Convert "mon dd yyyy" to "mm/dd/yyyy"" New topic
Author

Convert "mon dd yyyy" to "mm/dd/yyyy"

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
try{

Log.writeMessage(Log.LOG_LEVEL_DEBUG, "1");
String strTmp = "sep 21 2004";
Log.writeMessage(Log.LOG_LEVEL_DEBUG, "2");
java.util.Date dtTmp = new SimpleDateFormat("month dd, yyyy").parse(strTmp);
Log.writeMessage(Log.LOG_LEVEL_DEBUG, "3");
String strOutDt = new SimpleDateFormat("mm/dd/yyyy").format(dtTmp);
Log.writeMessage(Log.LOG_LEVEL_DEBUG, "strOutDt = " + strOutDt);

}catch(Exception ex){System.out.println(ex.getMessage()); }

I get unparseable date? How to solve this? The user enters date in this fashion. How do I convert it to mm/dd/yyyy?
[ October 07, 2004: Message edited by: shan javan ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by shan javan:
java.util.Date dtTmp = new SimpleDateFormat("month dd, yyyy").parse(strTmp);

Looking at SimpleDateFormat, I believe you want "MMM dd, yyyy". "MMMM" represents the full name, "MMM" the abbreviated three-letter name. It sees "month" as "m" the minute within the hour, "ont" as four unknown symbols, and "h" as the hour in the day.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanx.

Any date format I want to convert it into mm/dd/yyyy format? How to do that?

How do I know what format the user has entered?
[ October 07, 2004: Message edited by: shan javan ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by shan javan:
Any date format I want to convert it into mm/dd/yyyy format? How to do that?
From the SimpleDateFormat JavaDoc: "MM/dd/yyyy".
How do I know what format the user has entered?

Usually you simply tell the user to use a particular format. If you want to detect the format, you'll need to write a very complex parser.

For example, how do you know if they entered numbers as mm/dd/yyyy or dd/mm/yyyy? If mm turns out to be > 12, you can guess it's reversed, but cases like 03/03/1980 are impossible to distinguish.

In short, tell the user to use a specific format and then enforce it by displaying an error message when it fails to parse and letting the user enter it again.
 
Don't get me started about those stupid light bulbs.
 
subject: Convert "mon dd yyyy" to "mm/dd/yyyy"