1

私は次の日付を持っています:

例えば。String rawDate = "pon, 17 lis 2014, 15:51:12";

そして、私はそれを解析したいと思います。

電話する:

DateTime time = new DateTimeFormatterBuilder()
                    .append(DateTimeFormat.forPattern("EEE, dd MMM yyyy, HH:mm:ss")
                            .getParser())
                    .toFormatter().withLocale(new Locale("pl")).parseDateTime(rawDate);

しかし、私は得る:

java.lang.IllegalArgumentException: Invalid format: "pon, 17 lis 2014, 15:51:12"

4

2 に答える 2

1

良い質問!

JDK は独自のテキスト リソースを使用します。したがって、次のJava-8 コードは例外を生成します。

String input = "pon, 17 lis 2014, 15:51:12";

DateTimeFormatter dtf1 = 
  DateTimeFormatter.ofPattern("EEE, dd MMM yyyy, HH:mm:ss", new Locale("pl"));
LocalDateTime ldt1 = LocalDateTime.parse(input, dtf1);
System.out.print(ldt1);
// error message:
// java.time.format.DateTimeParseException:
// Text 'pon, 17 lis 2014, 15:51:12' could not be parsed at index 0

何が問題なのかを突き止めようとすると、JDK が "Pn" を使用していることがわかります。

DateTimeFormatter dtf1 = 
  DateTimeFormatter.ofPattern("EEE, dd MMM yyyy, HH:mm:ss", new Locale("pl"));
String output = LocalDateTime.of(2014, 11, 17, 15, 51, 12).format(dtf1);
System.out.println(output); // "Pn, 17 lis 2014, 15:51:12"
LocalDateTime ldt1 = LocalDateTime.parse(output, dtf1);

通常、人々は入力を変更することはできません。幸いなことに、独自のテキスト リソースを定義する回避策があります。

String input = "pon, 17 lis 2014, 15:51:12";

TemporalField field = ChronoField.DAY_OF_WEEK;
Map<Long,String> textLookup = new HashMap<>();
textLookup.put(1L, "pon");
textLookup.put(2L, "wt");
textLookup.put(3L, "\u0347r"); // śr
textLookup.put(4L, "czw");
textLookup.put(5L, "pt");
textLookup.put(6L, "sob");
textLookup.put(7L, "niedz");

DateTimeFormatter dtf2 = 
  new DateTimeFormatterBuilder()
  .appendText(field, textLookup)
  .appendPattern(", dd MMM yyyy, HH:mm:ss")
  .toFormatter()
  .withLocale(new Locale("pl"));
LocalDateTime ldt2 = LocalDateTime.parse(input, dtf2);
System.out.print(ldt2);
// output: 2014-11-17T15:51:12

さて、(古い)Joda-Timeについて。のようなメソッドがありませんappendText(field, lookupMap)。しかし、 a の実装を書くことができますDateTimeParser:

  final Map<String, Integer> textLookup = new HashMap<String, Integer>();
  textLookup.put("pon", 1);
  textLookup.put("wt", 2);
  textLookup.put("\u0347r", 3); // śr
  textLookup.put("czw", 4);
  textLookup.put("pt", 5);
  textLookup.put("sob", 6);
  textLookup.put("niedz", 7);

  DateTimeParser parser =
    new DateTimeParser() {
    @Override
    public int estimateParsedLength() {
        return 5;
    }
    @Override
    public int parseInto(DateTimeParserBucket bucket, String text, int position) {
        for (String key : textLookup.keySet()) {
            if (text.startsWith(key, position)) {
                int val = textLookup.get(key);
                bucket.saveField(DateTimeFieldType.dayOfWeek(), val);
                return position + key.length();
            }
        }
        return ~position;
    }
  };
  DateTimeFormatter dtf =
    new DateTimeFormatterBuilder().append(parser)
    .appendPattern(", dd MMM yyyy, HH:mm:ss").toFormatter()
    .withLocale(new Locale("pl"));
  String input = "pon, 17 lis 2014, 15:51:12";
  LocalDateTime ldt = LocalDateTime.parse(input, dtf);
  System.out.println(ldt); // 2014-11-17T15:51:12.000

最後に質問です。Unicode-CLDR-data では、「pon」のように、省略された曜日名の後ろにドットが使用されます。「pon」の代わりに(私のライブラリもCLDRコンテンツを使用しています)。あなたの言語知識とポーランド語に対する感覚からすると、どちらがより一般的ですか? ドットを使用するかどうか

于 2014-11-04T13:35:22.820 に答える