使用SimpleDateFormatter时结果不一致。

huangapple go评论77阅读模式
英文:

Getting inconsistent results with SimpleDateFormatter

问题

以下是翻译的部分:

Snippet 1:

我正在运行下面的代码段但结果不一致

    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss.SSS");
    Date date;
    date = inputFormat.parse("2020-6-30 11:45:45.123");
    SimpleDateFormat outputFormat = new SimpleDateFormat("MM-dd-yy hh:mm:ss.SSS");
    System.out.println(outputFormat.format(date));//06-30-20 11:45:45.123是输出结果

Snippet 2:

Snippet 2:

    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss.SSSSSS");
    Date date;
    date = inputFormat.parse("2020-6-30 11:45:45.123456");
    SimpleDateFormat outputFormat = new SimpleDateFormat("MM-dd-yy hh:mm:ss.SSSSSS");
    System.out.println(outputFormat.format(date));//06-30-20 11:47:48.000456是输出结果

Snippet 3:

Snippet 3:

    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss.SSSSSSSSS");
    Date date;
    date = inputFormat.parse("2020-6-30 11:45:45.123456789");
    SimpleDateFormat outputFormat = new SimpleDateFormat("MM-dd-yy hh:mm:ss.SSSSSSSSS");
    System.out.println(outputFormat.format(date));//07-01-20 10:03:21.000000789是输出结果

我相信小数秒部分在转换前后应该是一致的。如何实现一致的结果?

英文:

I am running below snippet and I am getting inconsistent resluts

SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss.SSS");
         Date date;
         date = inputFormat.parse("2020-6-30 11:45:45. 123");
     SimpleDateFormat  outputFormat = new SimpleDateFormat("MM-dd-yy hh:mm:ss. SSS");
         System.out.println(outputFormat.format(date));//06-30-20 11:45:45. 123 is the output

Snippet 2:

SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss.SSSSSS");
         Date date;
         date = inputFormat.parse("2020-6-30 11:45:45. 123456");
     SimpleDateFormat  outputFormat = new SimpleDateFormat("MM-dd-yy hh:mm:ss. SSSSSS");
         System.out.println(outputFormat.format(date));//06-30-20 11:47:48. 000456 is output

snippet 3:

SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-M-dd HH:mm:ss.SSSSSSSSS");
         Date date;
         date = inputFormat.parse("2020-6-30 11:45:45. 123456789");
     SimpleDateFormat  outputFormat = new SimpleDateFormat("MM-dd-yy hh:mm:ss. SSSSSSSSS");
         System.out.println(outputFormat.format(date));//07-01-20 10:03:21. 000000789 is output

I believe the fraction seconds should be same before and after conversion. How can I achieve consistent results

答案1

得分: 3

第一个是正确的,因为你的毫秒在0 - 999之间。第二和第三个在技术上也是正确的,除非你的123456毫秒被转换成了123秒 + 456毫秒,这将导致时间为+2分钟03秒。

所以,不要试图修复简单日期格式的输出,而是要修复你处理输入的方式。简单日期格式无法解析比毫秒更小的单位。如果你提供的数字大于999毫秒,它会影响到秒数等等...

英文:

The first one is correct, because your milliseconds are between 0 - 999. The second and third one are technically also correct, except your 123456 milliseconds are converted in 123 seconds + 456 milliseconds, which results in a time of +2 mins 03 secs.

So instead of trying to fix the output of simple date format, you have to fix the way you handle the input. Simple date format can not parse anything smaller than milliseconds. If you provide it with a number bigger than 999 milliseconds, it affects the seconds and so on...

huangapple
  • 本文由 发表于 2020年7月30日 15:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63167924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定