好问题!
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
好了,现在约(旧)乔达时间。它缺少像appendText(field, lookupMap)
这样的方法。但是,我们可以写一个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数据点的缩写工作日名称的后面使用,例如“PON”。而不是“pon”(我自己的图书馆也使用CLDR内容)。根据你的语言知识和关于波兰语的感觉,什么更为常见?使用点还是不点?
更常见的是点用法。我的情况很不典型;) – pixel 2014-11-04 21:55:46
@ pixel谢谢你的回答。那么,用我的库Time4J将提供一个非常简单的解决方案;-)否则似乎你必须定义自己的文本资源 - 无论如何 - 等于你使用的库。每个库都可以使用这种解决方法,即使是使用'DateFormatSymbols'的旧'java.text.SimpleDateFormat'也是如此。 – 2014-11-05 08:53:58