bingooyong / note

1 stars 1 forks source link

格式化模板配置错误 SimpleDateFormat jdk1.6 vs jdk1.8 #8

Open bingooyong opened 5 years ago

bingooyong commented 5 years ago

模板

正确 yyyy-MM-dd 错误 YYYY-MM-dd 在jdk 1.6 里面报错,1.8里面解析正常

jdk1.6

  private static String datePattern = "'.'YYYY-MM-dd";

  public static void main(String[] args) {
    System.out.println("---");
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
    String format = simpleDateFormat.format(new Date());
    System.out.println(format);
    System.out.println(simpleDateFormat);
  }
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'Y'
    at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:768)
    at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:575)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:500)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:475)
    at Test.main(Test.java:14)

jdk1.8

---
.2019-06-26
java.text.SimpleDateFormat@b289ea20