解析2位数年份:使用未知日期模式设置数据透视日期 [英] Parsing 2-digit years: Setting the pivot date with an unknown date pattern

查看:120
本文介绍了解析2位数年份:使用未知日期模式设置数据透视日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户将以不同的模式输入日期到我的应用程序。对于2位数年份,他还必须确定生产日期。

The user will input dates in different patterns to my application. For 2-digit years, he must also determine the pivot date.

示例: < br>
pattern = yy-MM-dd

pivot date = 70 (我添加了当前的千年和上个世纪的更多内容动态编程=> 1970

Example:
pattern = yy-MM-dd
pivot date = 70 (I add the current millennium and the last century for more dynamics programmatically => 1970)

69-04-22 变为 2069-04-22

70-04-22 成为 1970-04-22

此答案中所述,很容易构建 DateTimeFormatter 何时知道日期模式(此处 ddMMyy )。

但是由于用户输入了模式,我不知道如何构建 DateTimeFormatterBuilder 正确。我不能硬编码。

Like described in this answer it's easy to build a DateTimeFormatter when the date pattern (here ddMMyy) is known.
But since the user inputs the pattern, I'm not sure how to build the DateTimeFormatterBuilder properly. I can't hard code it.

我是否必须自己解析模式并调用 appendPattern()并按正确的顺序 appendValueReduced()?或者是否有内置解决方案?

Do I have to parse the pattern by myself and call appendPattern() and appendValueReduced() in the right order? Or is there a built-in solution?

推荐答案

这里是Java-8解决方案(它看起来像一个黑客):

Here the Java-8-solution (it rather looks like a hack):

String pattern = "MM/dd/yy 'US'"; // user-input
String text = "10/04/69 US"; // user-input
Locale locale = Locale.US; // user-input, too?

int yy = pattern.indexOf("yy");
DateTimeFormatter dtf;

if (
    (yy != -1) // explanation: condition ensures exactly two letters y
    && ((yy + 2 >= pattern.length()) || pattern.charAt(yy + 2) != 'y')
) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    String part1 = pattern.substring(0, yy);
    if (!part1.isEmpty()) {
        builder.appendPattern(part1);
    }
    builder.appendValueReduced(ChronoField.YEAR, 2, 2, 1970);
    String part2 = pattern.substring(yy + 2);
    if (!part2.isEmpty()) {
        builder.appendPattern(part2);
    }
    dtf = builder.toFormatter(locale);
} else {
    dtf = DateTimeFormatter.ofPattern(pattern, locale);
}

LocalDate ld = LocalDate.parse(text, dtf);
System.out.println("user-date: " + ld); // 2069-10-04

只有一个小小的警告:如果有任何用户得到了疯狂的想法要在模式中单独定义两次yy,则建议的解决方案将失败。然后正确的解决方案需要对模式字符进行某种迭代,但我认为这有点过分,所以我把它留在这里。

There is only one tiny caveat: If any user gets the crazy idea to define two times separately "yy" in the pattern then the proposed solution will fail. The correct solution would then require some kind of iterating over the pattern chars but I think that is overkill, so I leave it out here.

仅用于比较,使用我的外部库Time4J支持以下简短解决方案,因为它的解析引擎还具有设置任何合适格式属性的概念:

Just for comparison, using my external library Time4J enables following short solution because its parse engine also has the concept of setting any suitable format attributes:

LocalDate ld = 
  ChronoFormatter.ofDatePattern(pattern, PatternType.CLDR, locale)
  .with(Attributes.PIVOT_YEAR, 2070).parse(text).toTemporalAccessor();

这篇关于解析2位数年份:使用未知日期模式设置数据透视日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆