Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Date Problem when Formatting

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below returns empty String. Could somoene tell me what I did wrong?
<code>
String sTimeStamp = "12-03-2003 11:47:30";
//NAM Date format MM-dd-yyyy HH:mm a
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm a");
String newDate = dateFormat.format(sTimeStamp);

</code>
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code sample is a bit confusing. If you want to convert a string to a date you use the "parse()" method. If you want to convert a date to a string you use the "format()" method. But in your example you pass a string into the "format()" method, which won't compile.
Also, in your pattern you indicate there is an hour, minute and am/pm component but no second component. However in the string you are trying to format (parse?) you have a second component but no am/pm. That will cause the "parse()" method to throw an exception since the string (sTimeStamp) doesn't match the pattern.
Here's some sample code that shows both the parse() and format() methods in action:

[ January 14, 2004: Message edited by: Wayne L Johnson ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic