Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic