Java从几种日期格式中确定一种日期格式 [英] Java determine one dateformat from several dateformats

查看:141
本文介绍了Java从几种日期格式中确定一种日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望从具有不同语言和日志机制的不同系统生成的LOG文件中获取正确的dateformat。

We want to get a correct dateformat from LOG files generated by different systems with different language and log mechanism.

我们认为可以将 SimpleDataFormat.parse try-catch异常的方式。但是以下代码显示了一个问题。

We thought this can be done be a SimpleDataFormat.parse and try-catch exception way. But the follow code shows a problem.

    String tryparseString = "18-01-22 00:03:34:071";
    try {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf1.parse(tryparseString));
        System.out.println(sdf1.format(sdf1.parse(tryparseString)));
        System.out.println("Yeah! We found a CAN PARSE datefromat = " + sdf1.toPattern());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yy-MM-dd HH:mm:ss:SSS");
        System.out.println(sdf2.parse(tryparseString));
        System.out.println(sdf2.format(sdf2.parse(tryparseString)));
        System.out.println("Yeah! We found a CAN PARSE datefromat = " + sdf2.toPattern());
    } catch (ParseException e) {
        e.printStackTrace();
    }

结果为:

Sat Jan 22 00:03:34 CST 18
0018-01-22 00:03:34
Yeah! We found a CAN PARSE datefromat = yyyy-MM-dd HH:mm:ss
Mon Jan 22 00:03:34 CST 2018
18-01-22 00:03:34:000
Yeah! We found a CAN PARSE datefromat = yy-MM-dd HH:mm:ss:SSS

18-01-22 00:03:34:071 都可以用 yyyy-MM-dd HH:mm:ss格式化 yy-MM-dd HH:mm:ss:SSS

前一个

那么,有没有办法从服务器日期格式确定正确的日期格式?

So, is there a way to determine a correct dateformat from serveral dateformats?

推荐答案

java.time 进行救援。

    String[] formats = {
            "yyyy-MM-dd HH:mm:ss",
            "yy-MM-dd HH:mm:ss:SSS"
    };

    String tryparseString = "18-01-22 00:03:34:071";

    for (String format : formats) {
        try {
            LocalDateTime.parse(tryparseString, DateTimeFormatter.ofPattern(format));
            System.out.println("Yeah! We found a CAN PARSE date format = " + format);
        } catch (DateTimeParseException dtpe) {
            System.out.println(dtpe);
        }
    }

此打印:

java.time.format.DateTimeParseException: Text '18-01-22 00:03:34:071' could not be parsed at index 0
Yeah! We found a CAN PARSE datefromat = yy-MM-dd HH:mm:ss:SSS

话说,第一种格式 yyyy-MM-dd HH:mm:ss 无法解析您的示例字符串,第二种格式可以按您希望的那样成功。

In other words, the first format, yyyy-MM-dd HH:mm:ss, fails to parse your sample string, and the second succeeds as you wanted it to.

如果您仍然遇到 DateTimeFormatter 解析不应该解析的字符串的情况,则可以尝试 DateTimeFormatter.ofPattern(format).withResolverStyle(ResolverStyle.STRICT)。它可能会捕获更多不匹配的字符串。在这种情况下,您将需要 uuuu uu 而不是 yyyy yy 格式格式。

If you still experience a case where a DateTimeFormatter parses a string it shouldn’t, you may try DateTimeFormatter.ofPattern(format).withResolverStyle(ResolverStyle.STRICT). It may catch a few more non-matching strings. In this case you will need uuuu and uu instead of yyyy and yy in the format patterns, though.

这篇关于Java从几种日期格式中确定一种日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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