相关疑难解决方法(0)

DateTimeFormatter月份模式字母"L"失败

我注意到java.time.format.DateTimeFormatter无法按预期解析.见下文:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Play {
  public static void tryParse(String d,String f) {
    try { 
      LocalDate.parse(d, DateTimeFormatter.ofPattern(f)); 
      System.out.println("Pass");
    } catch (Exception x) {System.out.println("Fail");}
  }
  public static void main(String[] args) {
    tryParse("26-may-2015","dd-L-yyyy");
    tryParse("26-May-2015","dd-L-yyyy");
    tryParse("26-may-2015","dd-LLL-yyyy");
    tryParse("26-May-2015","dd-LLL-yyyy");
    tryParse("26-may-2015","dd-M-yyyy");
    tryParse("26-May-2015","dd-M-yyyy");
    tryParse("26-may-2015","dd-MMM-yyyy");
    tryParse("26-May-2015","dd-MMM-yyyy");
  }
}
Run Code Online (Sandbox Code Playgroud)

只有最后一次尝试tryParse("26-May-2015","dd-MMM-yyyy");将"通过".根据文档LLL应该能够解析出文本格式.也不是大写'M'与小写'm'的细微差别.

这真的很烦人,因为我无法默认解析Oracle DB默认格式化的字符串

SELECT TO_DATE(SYSDATE,'DD-MON-YYYY') AS dt FROM DUAL;
Run Code Online (Sandbox Code Playgroud)

同样,对于以下程序:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Play {
  public static void output(String f) {
    LocalDate d = LocalDate.now();
    Locale l = …
Run Code Online (Sandbox Code Playgroud)

java date java-8 java-time

24
推荐指数
2
解决办法
1万
查看次数

为什么我不能在Java 8中正确解析这个英文日期?

我想执行一个简单的例子,用模式解析字符串到日期.

String input = "Sep 31 2013";
LocalDate localDate = LocalDate.parse(input,
                DateTimeFormatter.ofPattern("MMM d yyyy"));
Run Code Online (Sandbox Code Playgroud)

它抛出异常:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sep 31 2013' could not be parsed at index 0
    at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
    at java.time.format.DateTimeFormatter.parse(Unknown Source)
    at java.time.LocalDate.parse(Unknown Source)
    at lambda.DateTime.main(DateTime.java:78)
Run Code Online (Sandbox Code Playgroud)

我使用java 8中的java.time包.

java date

2
推荐指数
1
解决办法
4804
查看次数

标签 统计

date ×2

java ×2

java-8 ×1

java-time ×1