2012-09-14 26 views
2

如何解析日期字符串像'21Jul12'。我曾尝试以下方法:如何解析日期字符串像'21Jul12'

import org.apache.commons.lang.time._ 

DateUtils.parseDate("21Jul12", Array("ddMMMyy")); 

但不能由于错误工作:

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) 

即使我用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) 
+2

你解析'2012年7月21日或'21Jul12'吗? –

+3

异常中的消息表示您尝试解析的日期是“2012年7月21日”,而不是“21Jul12”。 –

+0

“21Jul12”的意思是“2012-07-21”。有没有办法直接解析它? –

回答

2

它工作时,我把场景设置到Locale.en_US明确。异常是因为默认的区域设置的抛出是Locale.CHINA

+0

完美的,我也为我在我的SimpleDateFormat构造函数中设置本地:'SimpleDateFormat sdf = new SimpleDateFormat(“ddMMMyy”,Locale.ENGLISH);' 谢谢你的提示:) – lboix

0

我们对日期解析使用了一个消除过程,基本上,我们使用一组常用的日期格式(用于我们的应用程序),并且不引发异常的是我们使用的。不干净的解决方案,但它的作品...

例如

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; 
} 
2

这里是上述日期的代码来解析

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" })); 

和控制台

的结果
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 

也许你试图用“ddMMMyy”解析“2012年7月21日”

System.out.println("ddMMMyy >>>" + DateUtils.parseDate("2012 Jul 21", new String[] { "ddMMMyy" })); 

给你的筹码

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) 
+0

+1 DateUtils.parseDate需要的格式和数组...我是这样和白痴...:P – MadProgrammer

+0

@ph。我刚刚更新了我的问题 –

0
String date="21JUL12"; 
     try { 
      System.out.println(new SimpleDateFormat("ddMMMyy").parse(date)); 
     } 
     catch(ParseException ex) { 
      ex.printStackTrace(); 
     } 

上面的代码给了我这样的输出:

Sat Jul 21 00:00:00 BST 2012 
+0

不,它不能在我的机器上工作 –

+0

你还在解析异常? – PermGenError

+0

是的,是的。仍然相同excepiton –

0

TL;博士

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. 
) 

无需阿帕奇工具。

java.time

现代的方法使用java.time类。

String input = "21Jul12" ; 
DateTimeFormatter f = DateTimeFormatter.ofPattern("ddMMMuu" , Locale.US) ; 
LocalDate ld = LocalDate.parse(input , f) ; 

ld.toString(): 2012-07-21

两个小技巧:

  • 使用4位数的年份。
    • 保存两位数字的空间不值得我在程序员和用户/读者之间产生的歧义,困惑和错误。现在像素和碳粉都很便宜,我怀疑你能买得起这个世纪的两个额外数字。
    • 无论如何,该java。如果省略,则时间类别假定21世纪(20xx)。
  • 使用标准ISO 8601格式交换日期时间值作为文本。
    • 这些格式被明智地设计为明确的,易于被机器解析,并易于人类跨不同文化阅读。
    • java.time类在解析/生成字符串时默认使用这些标准格式。

关于java.time

java.time框架是建立在Java 8和更高版本。这些类代替了日期时间类legacy,如java.util.Date,Calendar,& SimpleDateFormat

Joda-Time项目,现在在maintenance mode,建议迁移到java.time类。请参阅Oracle Tutorial。并搜索堆栈溢出了很多例子和解释。规格是JSR 310

使用JDBC driverJDBC 4.2或更高版本兼容的,你可以交换java.time直接对象与数据库。不需要字符串和java.sql。*类。

从何处获取java.time类?

ThreeTen-Extra项目与其他类扩展java.time。这个项目是未来可能增加java.time的一个试验场。您可以在这里找到一些有用的类,如Interval,YearWeek,YearQuartermore