简体   繁体   English

无法解析的日期例外-月日期格式

[英]Unparseable date exception - Month Date Format

I have this format(String) of date: 4 AM CST 9 DEC 16 我的日期格式为(String):4 CST 9 DEC 16

I want to parse it using this code: 我想使用以下代码解析它:

String sDate = "4 AM CST  9 DEC 16";
Date st = new SimpleDateFormat("h aaa z d MMM yy",Locale.ENGLISH).parse(sDate);
DateFormat formatter = DateFormat.getDateTimeInstance(
                            DateFormat.LONG, 
                            DateFormat.FULL, 
                            new Locale("no"));
                    sDate = formatter.format(st);

But i'm having this exception instead: 但是我却有这个例外:

java.text.ParseException: Unparseable date: "4 AM CST 9 DEC 16" java.text.ParseException:无法解析的日期:“ CST 9 DEC 16 AM 4”

I'm really not sure but was it because the month in my sDate, the characters are all in uppercase value and the dateformat "MMM" the only uppercase letter is the first letter of each month? 我真的不确定,但这是因为sDate中的月份,字符全部为大写值,而dateformat“ MMM”唯一的大写字母是每个月的第一个字母吗?

This code: 这段代码:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
  public static void main(String[] args) throws ParseException {
    String sDate = "4 AM CST  9 DEC 16";
    Date st = new SimpleDateFormat("h aaa z d MMM yy", Locale.ENGLISH).parse(sDate);
    DateFormat formatter = DateFormat.getDateTimeInstance(
        DateFormat.LONG,
        DateFormat.FULL,
        new Locale("no"));
    sDate = formatter.format(st);
    System.out.print(sDate);
  }
}

works fine. 工作正常。 I have java version "1.8.0_77" 我有Java版本“ 1.8.0_77”

One of the reason of this exception may be of your regional setup in the computer. 出现此异常的原因之一可能是您在计算机中进行的区域设置。 Unless you don't define Locale, in the DateFormat.getDateTimeInstance() method, it will take Locale info from your computer default regional location and format. 除非您没有定义语言环境,否则在DateFormat.getDateTimeInstance()方法中,它将从您计算机的默认区域位置和格式中获取语言环境信息。

I have experienced such exception while modifying existing project when my colleague didn't get such exception in the same project and exactly same development environment. 当我的同事在同一个项目和完全相同的开发环境中没有得到这样的例外时,我在修改现有项目时遇到了这样的例外。 Then I crossed checked with his regional setting in control panel. 然后我在控制面板中检查了他的区域设置。 The project was in Japanese Locale and date format but by computer date format was English (UK). 该项目采用日语语言环境和日期格式,但按计算机日期格式为英语(英国)。 After changing the format code didn't give error further. 更改格式代码后,不再显示错误。

Go to control panel -> Clock, Language and Region -> Region 转到控制面板->时钟,语言和区域->区域

在此处输入图片说明 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM