[英]How to parse the date string like '21Jul12'
How to parse the date string like '21Jul12'. 如何解析日期字符串,例如“ 21Jul12”。 I have tried the following way:
我尝试了以下方法:
import org.apache.commons.lang.time._
DateUtils.parseDate("21Jul12", Array("ddMMMyy"));
but it can't work due to the error: 但由于错误而无法使用:
java.text.ParseException: Unable to parse the date: 21Jul21
at org.apache.commons.lang.time.DateUtils.parseDateWithLeniency(DateUtils.java:359)
at org.apache.commons.lang.time.DateUtils.parseDate(DateUtils.java:285)
at .<init>(<console>:20)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
at java.lang.Thread.run(Thread.java:662)
Even I use java.text.SimpleDateFormat
, i got a similiar exception: 即使我使用
java.text.SimpleDateFormat
,我也有类似的异常:
java.text.ParseException: Unparseable date: "21Jul12"
at java.text.DateFormat.parse(DateFormat.java:337)
at .<init>(<console>:13)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
at java.lang.Thread.run(Thread.java:662)
Here is the code for the above mentioned dates to parse 这是上述日期的解析代码
System.out.println("ddMMMyy >>>" + DateUtils.parseDate("21Jul12", new String[] { "ddMMMyy" }));
System.out.println("yyyy-MM-dd >>>" + DateUtils.parseDate("2012-07-21", new String[] { "yyyy-MM-dd" }));
System.out.println("yyyy MMM dd >>>" + DateUtils.parseDate("2012 Jul 21", new String[] { "yyyy MMM dd" }));
and the result on the console 并在控制台上显示结果
ddMMMyy >>>Sat Jul 21 00:00:00 CEST 2012
yyyy-MM-dd >>>Sat Jul 21 00:00:00 CEST 2012
yyyy MMM dd >>>Sat Jul 21 00:00:00 CEST 2012
maybe you tried to parse "2012 Jul 21" with "ddMMMyy" 也许您尝试使用“ ddMMMyy”解析“ 2012 Jul 21”
System.out.println("ddMMMyy >>>" + DateUtils.parseDate("2012 Jul 21", new String[] { "ddMMMyy" }));
gives your stack 给你的筹码
java.text.ParseException: Unable to parse the date: 2012 Jul 21
at org.apache.commons.lang.time.DateUtils.parseDateWithLeniency(DateUtils.java:359)
at org.apache.commons.lang.time.DateUtils.parseDate(DateUtils.java:285)
at com.collibra.dgc.core.model.activity.impl.TestTree.testname(TestTree.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
it works when I set the locale to Locale.en_US explicitly. 当我将语言环境明确设置为Locale.en_US时,它起作用。 Exception thrown because of the default locale setting is
Locale.CHINA
由于默认语言环境设置为
Locale.CHINA
引发异常
We use a process of elimination for our date parsing, basically, we use an array of commonly used date formats (for our application) and the one that doesn't throw an exception is the one we use. 我们使用消除法进行日期解析,基本上,我们使用一系列常用的日期格式(对于我们的应用程序),并且不会引发异常的是我们使用的一种。 Not the cleanest of solutions, but it works...
不是最干净的解决方案,但它可以工作...
For example 例如
parse("2012 Jul 21", "ddMMMyy", "yyyy MMM dd");
parse("21Jul12", "ddMMMyy", "yyyy MMM dd");
public Date parse(String value, String... formats) throws ParseException {
Date date = null;
for (String format : formats) {
try {
date = new SimpleDateFormat(format).parse(value);
} catch (ParseException exp) {
}
}
if (date == null) {
throw new ParseException(value + " is an unrecognized date format", 0);
}
return date;
}
String date="21JUL12";
try {
System.out.println(new SimpleDateFormat("ddMMMyy").parse(date));
}
catch(ParseException ex) {
ex.printStackTrace();
}
the above code gives me this output: 上面的代码给了我这个输出:
Sat Jul 21 00:00:00 BST 2012
LocalDate.parse( // Represent a date-only value without time-of-day and without time zone using the `LocalDate` class.
"21Jul12" , // A poor choice of format for date value as text. Instead use standard ISO 8601 formats.
DateTimeFormatter.ofPattern( "ddMMMuu" , Locale.US ) // Specify `Locale` to determine the human language and cultural norms used in translating.
)
No need for Apache utilities. 无需Apache实用程序。
The modern approach uses java.time classes. 现代方法使用java.time类。
String input = "21Jul12" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "ddMMMuu" , Locale.US ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
ld.toString(): 2012-07-21
ld.toString():2012-07-21
Two tips: 两个提示:
20xx
) if omitted. 20xx
)。 The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧的旧式日期时间类,例如
java.util.Date
, Calendar
和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 。 And search Stack Overflow for many examples and explanations.
并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 .
规格为JSR 310 。
Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. 使用兼容JDBC 4.2或更高版本的JDBC驱动程序 ,您可以直接与数据库交换java.time对象。 No need for strings nor java.sql.* classes.
不需要字符串或java.sql。*类。
Where to obtain the java.time classes? 在哪里获取java.time类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time.
该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as
Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如
Interval
, YearWeek
, YearQuarter
,和更多 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.