How to parse date string to Date?
Here is a working example:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
public class j4496359 {
public static void main(String[] args) {
try {
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
Date result = df.parse(target);
System.out.println(result);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
Will print:
Thu Sep 28 13:29:30 CEST 2000
The pattern is wrong. You have a 3-letter day abbreviation, so it must be EEE
. You have a 3-letter month abbreviation, so it must be MMM
. As those day and month abbreviations are locale sensitive, you'd like to explicitly specify the SimpleDateFormat
locale to English as well, otherwise it will use the platform default locale which may not be English per se.
public static void main(String[] args) throws Exception {
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date result = df.parse(target);
System.out.println(result);
}
This prints here
Thu Sep 28 07:29:30 BOT 2000
which is correct as per my timezone.
I would also reconsider if you wouldn't rather like to use HH
instead of kk
. Read the javadoc for details about valid patterns.