• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Date Problem when Formatting

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!