格式解析异常“EEE MMM dd HH:mm:ss Z yyyy”

时间:2016-10-19 16:39:58

标签: java android date datetime

我的日期字符串是:“10月19日星期三14:34:26 BRST 2016”,我正在尝试将其解析为“dd / MM / yyyy”,但我得到以下例外:

java.text.ParseException: Unparseable date: "Wed Oct 19 14:34:26 BRST 2016" (at offset 20)

方法

public String dataText(int lastintervalo) {

        Date mDate = new Date();
        String dt = mDate.toString();
        SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                Locale.getDefault());
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(sdf.parse(dt));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        sdf.applyPattern("dd/MM/yyyy");

        c.add(Calendar.DATE, lastintervalo);
        return sdf.format(c.getTime());

    }

我已经搜索了google和stackoverflow问题,但似乎没有任何工作

3 个答案:

答案 0 :(得分:2)

由于错误消息抱怨偏移量20,即BRST值,似乎无法解析时区。

请尝试使用此代码,以确保识别出巴西时区:

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
System.out.println(sdf.parse("Wed Oct 19 14:34:26 BRST 2016"));

自从我在美国东部以来,我就打印出这个:

Wed Oct 19 12:34:26 EDT 2016

答案 1 :(得分:1)

TL;博士

ZonedDateTime.parse ( 
    "Wed Oct 19 14:34:26 BRST 2016" , 
    DateTimeFormatter.ofPattern ( "EE MMM dd HH:mm:ss z uuuu" )
)
.toLocalDate()
.format( 
    DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT)
                     .withLocale( Locale.UK ) 
)
  

19/10/16

java.time

你正在使用麻烦的旧日期时间类,现在是旧的,取而代之的是java.time类。

使用实时区域

切勿使用BRSTESTIST这样的3-4字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

continent/region的格式指定proper time zone name。例如,America/Sao_Paulo

因此,虽然以下代码适用于此特定情况,但我建议您交换输入等数据。如果您对数据源有影响,请更改为使用标准ISO 8601格式进行日期时间值的数据交换。

  

2016-10-19T14:34:26-02:00

更好的是,通过在时方括号中附加时区名称来交换由ZonedDateTime创建的扩展ISO 8601格式的字符串。

  

2016-10-19T14:34:26-02:00 [美国/圣保罗]

最重要的是,通常是在交换数据之前将这些值转换为UTC。在java.time中,调用toInstant().toString()来执行此操作。通常最好在UTC中工作,仅在需要时应用时区,例如向用户显示。

  

2016-10-19T16:34:26Z

示例代码

String input = "Wed Oct 19 14:34:26 BRST 2016";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "EE MMM dd HH:mm:ss z uuuu" );
ZonedDateTime zdt = ZonedDateTime.parse ( input , f );
  

zdt.toString():2016-10-19T14:34:26-02:00 [America / Sao_Paulo]

要查看UTC中的相同时刻,请提取Instant

Instant instant = zdt.toInstant();
  

instant.toString():2016-10-19T16:34:26Z

LocalDate类表示没有时间且没有时区的仅限日期的值。

LocalDate ld = zdt.toLocalDate();
  

ld.toString():2016-10-19

要生成其他格式的字符串,请搜索DateTimeFormatter类的Stack Overflow。通常最好让java.time自动本地化。

要进行本地化,请指定:

  • FormatStyle确定字符串的长度或缩写。
  • Locale确定(a)翻译日期名称,月份名称等的人类语言,以及(b)决定缩写,大小写,标点符号等问题的文化规范。< / LI>

示例:

Locale l = Locale.CANADA_FRENCH ;  // Or Locale.US or Locale.ITALY etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT).withLocale( l );
String output = ld.format( f );

关于java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.Date.Calendar和&amp; java.text.SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore

答案 2 :(得分:0)

试试这个..它可能会起作用

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                    Locale.ENGLISH);
相关问题