question about DateFormat class

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, although i m sure that it's written in a defined and correct format, 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

bye

[990 byte] By [natoskiiia] at [2007-10-1 19:27:00]
# 1

Apparently its not. Else it would work. You can just define the mask, by reading the Java docs. So new SimpleDateFormat("MMM d, yyyy k:mm aa")

should do it.

I believe in Sun's JDK implementation you can even get the used displaymask because the DateFormat instance returned by getDateTimeInstance is actually a SimpleDateFormat:

SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);

System.out.println("The pattern is: " + sdf.toPattern());

Note that this might not remain this way, and it might not work in other JVMs. Anyway then you can see which format it really uses. Note that the returned format is locale specific. Maybe you need to add the locale to your invocation:

DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US);

Well, good luck

Vincent79a at 2007-7-11 15:36:09 > top of Java-index,Other Topics,Algorithms...