解析字符串用空格日期,导致ParseException的 [英] Parsing String with spaces to Date, lead to ParseException

查看:111
本文介绍了解析字符串用空格日期,导致ParseException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种形式的日期字符串周四8月2日00:00:00 GMT + 00:00 2012

I have date strings in this form Thu Aug 02 00:00:00 GMT+00:00 2012

我曾尝试使用这种方法Date对象来解析这些字符串

I have tried to use this method to parse these String in a Date object

public Date fromStringToDate(String data) {
        Date result;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
        try {
            result = sdf.parse(data);
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

但不工作,我得到这个错误

But doesn't works and I get this error

java.text.ParseException: Unparseable date: "Thu Aug 02 00:00:00 GMT+00:00 2012"

我想这个问题是由错误的的SimpleDateFormat 引起的,但我不知道正确的语法来修复它。

I suppose that the problem is caused by a wrong SimpleDateFormat, but I don't know the right syntax to fix it.

推荐答案

您需要的日期格式调整到给定的字符串:

You need to adjust the date format to the given string:

EEE MMM dd HH:mm:ss Z yyyy

请确保使用正确的占位符,区分大小写等看看到的日期和时间模式的。

Make sure use the correct placeholders, case sensitive, etc. Take a look to the Date and Time Patterns.

对不起,我曾与Z模式的错误,'Z'是:

Sorry, I had a mistake with the 'z' pattern, 'Z' is:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);

看一看到 Locale.US ,以适用,因为个月和日是英语是很重要的。

Take a look to Locale.US, it is important to apply because the months and and days are in english.

这篇关于解析字符串用空格日期,导致ParseException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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