java.text.ParseException:不可解析的日期
我在尝试以下代码时遇到解析异常:java.text.ParseException:不可解析的日期,java,simpledateformat,parseexception,Java,Simpledateformat,Parseexception,我在尝试以下代码时遇到解析异常: String date="Sat Jun 01 12:53:10 IST 2013"; SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss"); Date currentdate; currentdate=sdf.parse(date); System.out.println(currentdate); 例外情况: 线程主java.text.Pa
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
Date currentdate;
currentdate=sdf.parse(date);
System.out.println(currentdate);
例外情况:
线程主java.text.ParseException中出现异常:不可解析日期:2013年6月1日12:53:10
位于com.ibm.icu.text.DateFormat.parseDateFormat.java:510
输入:Sat Jun 01 12:53:10 IST 2013
预计产量:2013年6月1日12:53:10
如何解决这个问题?模式是错误的
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd hh:mm:ss Z yyyy");
Date currentdate;
currentdate=sdf.parse(date);
System.out.println(currentdate);
您的模式与输入字符串完全不对应。。。它不起作用也就不足为奇了。这可能会更好:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
然后,要使用所需格式打印,您需要另一个SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
注:
如果您的区域设置不是英语,则应包括区域设置,因为可能无法识别日期名称
因此,如果可能的话,您应该在输入中使用正确的时区名称。
将格式更新为:
SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd hh:mm:ss Z yyyy");
我找到了一个简单的解决方案,可以在没有任何解析错误的情况下获取当前日期
Calendar calendar;
calendar = Calendar.getInstance();
String customDate = "" + calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
我需要向SimpleDataFormat类的parse方法添加ParsePosition表达式:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
正如其他答案所指出的,您的格式设置模式与输入字符串不匹配。
你的输入格式很糟糕。
您使用的是多年前被java.time类取代的麻烦的旧日期时间类。
ISO 8601
与您的格式不同,使用标准格式将日期时间值交换为文本
在解析/生成字符串时,java.time类默认使用标准ISO 8601格式
正确的时区名称
以大陆/地区的格式指定,例如,或太平洋/奥克兰。切勿使用3-4个字母的缩写,如EST或IST,因为它们不是真正的时区,不标准,甚至不是唯一的
您的IST可能是指冰岛标准时间、印度标准时间、爱尔兰标准时间或其他时间。time类只剩下猜测,因为没有逻辑解决方案来解决这种歧义
java.time
现代方法使用java.time类
定义与输入字符串匹配的格式模式
String input = "Sat Jun 01 12:53:10 IST 2013";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );
zdt.toString:2013-06-01T12:53:10Z[大西洋/雷克雅未克]
如果您的输入不适用于冰岛,则应预解析字符串以调整为正确的时区名称。例如,如果您确定输入是针对印度的,请将IST更改为Asia/Kolkata
zdt.toString:2013-06-01T12:53:10+05:30[亚洲/加尔各答]
关于java.time
该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&
要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是
该项目现已启动,建议迁移到类
您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要java.sql.*类。Hibernate5和JPA2.2支持java.time
从哪里获得java.time类
、和更高版本-标准Java API的一部分,带有捆绑实现。
带来了一些小功能和修复。
和
大多数java.time功能都在中向后移植到Java6和Java7。
Android 26+的更高版本捆绑了java.time类的实现。
对于早期的Android
此模式与发生异常的输入字符串不符
您需要使用以下模式来完成工作
E MMM dd HH:mm:ss z yyyy
以下代码将帮助您跳过异常
使用SimpleDataFormat
检查您的模式DD-MMM-YYYY和parse29-11-2018方法的输入。 解析方法的输入应该如下:DD-MMM-YYYY i,e。2019年8月21日 在我的代码中:
String pattern = "DD-MMM-YYYY";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
startDate = simpleDateFormat.parse("29-11-2018");// here no pattern match
endDate = simpleDateFormat.parse("28-AUG-2019");// Ok
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我在以下日期得到了相同的例外:2013年10月24日,格式如下:MMMMM dd,yyyy。只有当我像你提到的那样指定Locale.ENGLISH时,问题才得以解决。tnx@assylias如何在这里包含时区。请帮助我。@KJEjava48这取决于您想要的格式-这在SimpleDataFormat的javadoc中都有解释。如果IST是从API返回的呢?它总是以这种格式发送给我?同时使用这两个代码行,但首先在开始时定义对象,如R.id.object e.t.c.FYI,麻烦的旧日期时间类,如,和java.text.simpleDataFormat,现在被java 8和更高版本中内置的类所取代。看,错了。我第一次得到java.text.ParseException:不可解析日期:2019年8月28日。在向格式化程序提供Locale.ENGLISH时,我得到的是2018年12月30日星期日00:00:00 CET,这与预期相差甚远。同样,当2013年提出这个问题时,使用SimpleDataFormat是合理的,但不是今天,它设计拙劣且早已过时。如Basil Bourque的答案所示,使用java.time。最后,你似乎没有回答这个问题?
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
E MMM dd HH:mm:ss z yyyy
String date="Sat Jun 01 12:53:10 IST 2013"; // Input String
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy"); // Existing Pattern
Date currentdate=simpleDateFormat.parse(date); // Returns Date Format,
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("MMM dd,yyyy HH:mm:ss"); // New Pattern
System.out.println(simpleDateFormat1.format(currentdate)); // Format given String to new pattern
// outputs: Jun 01,2013 12:53:10
String pattern = "DD-MMM-YYYY";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
startDate = simpleDateFormat.parse("29-11-2018");// here no pattern match
endDate = simpleDateFormat.parse("28-AUG-2019");// Ok
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}