• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Question about DateFormat class

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I'm trying to convert a date written in a specific format into another different format using the DateFormat class.
I have this code but it's not working(it can't parse the given date and goes to the catch clause) and i dont know what s the reason, So if anyone has worked with that, can you tell me what s the problem

public static void main(String[] args) {
String dateString = "Nov 4, 2003 8:14 PM";
DateFormat format = SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);

// Parse the date
try {
Date date = format.parse(dateString);
System.out.println("Original string: " + dateString);
System.out.println("Parsed date : " + date.toString());
}
catch(ParseException pe) {
System.out.println("ERROR: could not parse date in string \"" +
dateString + "\"");
}
}

thanks in advance
bye
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a good idea when catching Exceptions to print out the stack trace, since this usually tells you what the problems is. If you add this:

it might become clearer.

I would guess that the format you have written your dateString in does not match the format for the current Locale. If you know to expect dateStrings in a certain format, then why not explicitly create a DateFormat for this format?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic