ParseException-无法找出正确的模式 [英] ParseException - Can't figure out the right pattern

查看:245
本文介绍了ParseException-无法找出正确的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串:dateToParse = "Fri May 16 23:59:59 BRT 2014",并想使用DateFormat进行解析:

I have the following string: dateToParse = "Fri May 16 23:59:59 BRT 2014", and want to parse it using DateFormat:

DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));
cal.setTime(dateFormat.parse(dateToParse));

现在我正在用pattern = "EEE MMM dd HH:mm:ss z yyyy"尝试,但是遇到了这个异常:

right now I'm trying it with pattern = "EEE MMM dd HH:mm:ss z yyyy", but get this exception:

java.text.ParseException: Unparseable date: "Fri May 16 23:59:59 BRT 2014" (at offset 0)

我无法弄清楚这种模式有什么问题,特别是在索引0处...我有什么想念的吗?谢谢.

I can't figure out what's wrong with this pattern, specially at index 0... any idea what am I missing? Thanks.

所以部分问题是我正在使用Locale.getDefault(),因此可能尝试使用葡萄牙语中的dateFormat解析英语中的日期...使用正确的语言环境,我仍在获取ParseException,但这次的偏移量为20 ,这意味着在解析时区时出现问题(在我的情况下为"BRT")...

So part of the problem was I was using Locale.getDefault(), so problably trying to parse a date in english with a dateFormat in portuguese... with the correct Locale, I'm still getting ParseException, but this time at offset 20, which means something is going wrong when parsing the timezone ('BRT', in my case)...

推荐答案

可能是因为语言环境.

尝试更改

Locale.getDefault()

Locale.ENGLISH

像这样

        String date_ = "Fri May 16 23:59:59 BRT 2014";
    DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
    Calendar date = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));
    dateFormat.setCalendar(date);
    try {
        date.setTime(dateFormat.parse(date_));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

这篇关于ParseException-无法找出正确的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆