This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic