2017-12-03 6 views
2

文字列を解析してLocalDateTimeにするためにDateUtilを作成しようとしています。年がパターン "yyyy"を持っていれば問題はありませんが、年が "yy"のような場合は例外が発生します。LocalDateTime短い年の形式を解析するdd-MMM-YY HH:DateTimeFormatterBuilderを使用するmm

Exception in thread "main" java.time.format.DateTimeParseException: Text '06-mar-17 11:52' could not be parsed at index 3 
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) 
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) 
at java.time.LocalDateTime.parse(LocalDateTime.java:492) 
at rinex.service.impl.utils.date.Test.parseToLocalDateTime(Test.java:43) 
at rinex.service.impl.utils.date.Test.main(Test.java:53) 

Testクラス

import java.time.LocalDateTime; 
    import java.time.format.DateTimeFormatter; 
    import java.time.format.DateTimeFormatterBuilder; 
    import java.time.format.DateTimeParseException; 
    import java.time.temporal.ChronoField; 
    import java.util.LinkedHashMap; 
    import java.util.Map; 

    public class Test { 

    private static final Map<String, String> DATE_FORMAT_REGEXPS = new LinkedHashMap<String, String>() {{ 
      put("^\\d{4}\\d{1,2}\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyyMMdd HH:mm:ss"); 
      put("^\\d{1,2}-[a-z]{3}-\\d{2}\\s\\d{1,2}:\\d{2}$", "dd-MMM-yy HH:mm"); 
     }}; 

    private static final Map<String, DateTimeFormatter> DATE_SHORT_FORMAT_REGEXPS = new LinkedHashMap<String, DateTimeFormatter>() {{ 
     put("dd-MMM-yy HH:mm", new DateTimeFormatterBuilder(). 
       appendPattern("dd-MMM-"). 
       appendValueReduced(ChronoField.YEAR, 2, 2, 2000). 
       appendPattern(" HH:mm").toFormatter()); 
    }}; 

    public static String determineDateFormat(String dateString) { 
     for (String regexp : DATE_FORMAT_REGEXPS.keySet()) { 
      if (dateString.toLowerCase().matches(regexp)) { 
       return DATE_FORMAT_REGEXPS.get(regexp); 
      } 
     } 
     return null; // Unknown format. 
    } 

    public static LocalDateTime parseToLocalDateTime(String date) { 
     date = date.trim().toLowerCase().replaceAll("utc",""); 
     String format = determineDateFormat(date); 
     LocalDateTime local; 

     try { 
      local = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format)); 
     } catch (DateTimeParseException e) { 
      DateTimeFormatter formatter = DATE_SHORT_FORMAT_REGEXPS.get(format); 
      local = LocalDateTime.parse(date, formatter); 
     } 
     return local; 
    } 

    public static void main(String[] args) { 
     String date = "20170506 11:52:00UTC"; 
     LocalDateTime dateTime = Test.parseToLocalDateTime(date); 

     date = "06-MAR-17 11:52"; 
     dateTime = Test.parseToLocalDateTime(date); 
    } 
} 

Debug for "20170506 11:52:00UTC" date - "yyyyMMdd HH:mm:ss"

Debug for "06-MAR-17 11:52" date - "dd-MMM-yy HH:mm"

ので、 "06-MAR-17 11時52" の日付を解析するためにDateTimeFormatterBuilderで何が間違っているのですか?

答えて

1

月の文字列表現ではありませんが、parsing is case sensitive by defaultです。

あなたは正しい(タイトル)ケースを使用することができ、次のいずれか

// works 
LocalDateTime.parse("06-Mar-17 11:52", 
     DateTimeFormatter.ofPattern("dd-MMM-yy HH:mm", Locale.ENGLISH)); 

それとも、大文字と小文字を区別を解析DateTimeFormatterを構築することができます:あなたのデフォルトの場合

// works 
DateTimeFormatter formatter = new DateTimeFormatterBuilder() 
     .parseCaseInsensitive().appendPattern("dd-MMM-yy HH:mm") 
     .toFormatter(Locale.ENGLISH); 
formatter.parse("06-mar-17 11:52"); 

(明示的なロケールが必要とされていませんロケールはすでに英語です)

+0

Thanx、それは動作します! – Evgeniy

関連する問題