• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about DateFormat class  RSS feed

 
Nadine Hossam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!