Android的SimpleDateFormat的解析错误 [英] Android SimpleDateFormat Parsing Error

查看:348
本文介绍了Android的SimpleDateFormat的解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一些与SimpleDateFormatter中的Andr​​oid或Java处理计算器上的很多其他问题,但我一直无法找到帮我解答我的问题的任何问题。

I know there are many other questions on stackoverflow that deal with SimpleDateFormatter in Android or Java, but I have been unable to find any questions that help me answer my own question.

我有这个时间,格式为一个String 2014-06-28T21:00:00-05:00 ,我试图将其转换为下午9:00 (或至少9:00)。我是pretty肯定我的问题是实际上写出了上面的时间正确的符号,但这里是我的code:

I have a String with this time format 2014-06-28T21:00:00-05:00 and I am trying to convert it to 9:00 PM (or at least 9:00). I'm pretty sure my issue is with actually writing out the correct notation for the above time, but here is my code:

        String input = '2014-06-28T21:00:00-05:00';
        DateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+|-hh:mm");
        fromFormat.setLenient(false);
        DateFormat toFormat = new SimpleDateFormat("hh:mm");
        toFormat.setLenient(false);
        try{
            String output = toFormat.format(fromFormat.parse(input));
            return output;
        } catch(ParseException pe){
            pe.printStackTrace();
        }
        return "No Date Listed";

如果我看的堆栈跟踪,它告诉我无法解析日期偏移#19

If I look at the stack trace, it tells me unparseable date at offset #19.

我的非常肯定的的code背后的逻辑不工作,因为我切换SimpleDateFormats以简单的东西有点像 YYYY-MM-DD MMMM DD,YY 它完美地工作。所以,任何人都可以点我在正确的方向,帮我找出适当的时间符号?

I am fairly certain the logic behind the code does work because I switched the SimpleDateFormats to something a little simpler like yyyy-MM-dd and MMMM dd, yy and it worked perfectly. So, can anyone point me in the right direction and help me figure out the proper time notation?

我AP preciate所有的帮助。

I appreciate all of your help.

推荐答案

您所遇到的主要问题是,你被赋予同一个时区格式的时间,这是不被支持的的SimpleDateFormat

The main problem you're having is that you are being given a time with a time zone format which is not supported by SimpleDateFormat.

有两种支持的时区的格式,它可以解析,

There are two supported time zone formats that it can parse,

常规时区:

General time zone: Time zones are interpreted as text if they have names. For time zones representing a GMT offset value, the following syntax is used:
     GMTOffsetTimeZone:
             GMT Sign Hours : Minutes
     Sign: one of
             + -
     Hours:
             Digit
             Digit Digit
     Minutes:
             Digit Digit
     Digit: one of
             0 1 2 3 4 5 6 7 8 9
Hours must be between 0 and 23, and Minutes must be between 00 and 59. The format is locale independent and digits must be taken from the Basic Latin block of the Unicode standard.

...和RFC 822时区:

...and RFC 822 time zones:

RFC 822 time zone: For formatting, the RFC 822 4-digit time zone format is used:
     RFC822TimeZone:
             Sign TwoDigitHours Minutes
     TwoDigitHours:
             Digit Digit
TwoDigitHours must be between 00 and 23. Other definitions are as for general time zones.

正如你可以看到,一般的时区中有一个冒号,但必须以GMTpfixed $ P $,而RFC 822格式没有冒号。你正在尝试解析是一种两个私生子的。

As you can see, the general time zone has a colon in it, but must be prefixed with "GMT", whereas the RFC 822 format has no colon. What you are trying to parse is a sort of bastardization of the two.

以下情况之一会的工作,根据不同的时区格式的如果的你有一个合法的语法:

One of the following would work, depending on the time zone format, if you had a legal syntax:

    String generalInput = "2014-06-28T21:00:00GMT-05:00"; // legal General time zone
    String rfcInput = "2014-06-28T21:00:00-0500"; // legal RFC 822 time zone

    DateFormat generalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); // format for general time zone
    DateFormat rfcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // format for RFC 822 time zone

由于您的输入格式错误,我建议你根本不要试图在所有分析它的时区的一部分,并把它当作一个本地时间。既然你想转换 21时下午九时无论如何,这应该为你工作:

Since your input is malformed, I would suggest that you simply don't try to parse the time zone part of it at all, and treat it as a local time. Since you're trying to convert 21:00 to 9:00 pm anyway, this should work for you:

    String input = "2014-06-28T21:00:00-05:00"; // not a legal time zone format
    DateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // don't even try to parse time zone

这篇关于Android的SimpleDateFormat的解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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