Parsing string to date with timezone in national format

南笙酒味 提交于 2020-01-06 03:05:54

问题


I try to parse string (russian locale) "01 августа 2014, пятница. 20:00 МСК" to java.util.Date. I try this code:

String dateString = "01 августа 2014, пятница. 20:00 МСК"
Locale rusLocale = new Locale.Builder().setLanguage("ru").setScript("Cyrl").build();
String pattern = "dd MMMM yyyy, EEEE. HH:mm z"
Date date = SimpleDateFormat(pattern, rusLocale).parse(dateString)

With month and weekday this code work fine, but when I try to parse string with timezone name МСК I get java.text.ParseException: Unparseable date. When I change MCK to MSK "01 августа 2014, пятница. 20:00 MSK" code also work fine. So we can parse strings month and weekday, but can't do it with timezone or "MCK" is just not valid?


回答1:


Try this code. I think it is something relevant with your code.

    String dateString = "17 октябрь 2014, пятница. 20:00";
    Locale rusLocale = new Locale.Builder().setLanguage("ru").setScript("Cyrl").build();
    String pattern = "dd MMMM yyyy, EEEE. HH:mm";
    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, rusLocale);
        dateFormat.setTimeZone(TimeZone.getTimeZone("МСК"));  
        Date date = dateFormat.parse(dateString);   
    } catch (ParseException e) {
    ...


来源:https://stackoverflow.com/questions/26083062/parsing-string-to-date-with-timezone-in-national-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!