简体   繁体   中英

Java SimpleDateFormat parsing

Trying to parse date as follows:

String startDate = "Tue May 15 00:00:01 MSK 2012";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = (Date) parser .parse(startDate );
String formattedDate = parser .format(date );

and I get this error:

08-15 11:18:22.005: W/System.err(1012): java.text.ParseException: Unparseable date: "Tue May 15 00:00:01 MSK 2012" 08-15 11:18:22.045: W/System.err(1012): at java.text.DateFormat.parse(DateFormat.java:626)

How can I resolve it?

SimpleDateFormat by default uses the default locale: the given code will work when run on a system configured for English and fail elsewhere. You can set the locale to a known one when creating the parser, for example:

String startDate = "Tue May 15 00:00:01 MSK 2012";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.US);
Date date = (Date) parser .parse(startDate );
String formattedDate = parser .format(date );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM