Date对象的SimpleDateFormat不能正确解析时间戳字符串中的Java(安卓)环境字符串、对象、正确、环境

2023-09-04 07:44:03 作者:怪硪入戏太深

我使用的日期对象的SimpleDateFormat 对象,如下图所示。问题LIS的日期对象是否显示了错误的日期,这是一个几分钟由原始的字符串。该日期对象出现存储总毫秒调试的时间。

对这个问题的任何想法?

 进口java.text.SimpleDateFormat的;

进口java.util.Date;

日期played_at_local;

DATEFORMAT =新的SimpleDateFormat(YYYY-MM-dd'T'hh:MM:ss.SSSSSSZ);

played_at_local = dateFormat.parse(2011-04-11T22:27:18.491726-05:00);

// played_at_local显示的调试器周一年04月11 22点35分29秒美国/芝加哥2011
 

解决方案

尝试从格式字符串删除小数秒。我只是碰到了同样的问题,但有一个稍微不同的格式。我的输入格式是不是在ISO格式(没有T,而没有Z),但症状是相同的 - 一些随机数分秒的时间了,但一切都很好。这是我的日志结果看起来像:

在使用小数秒格式为:

 的SimpleDateFormat dateFormater =新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS.SSSSSS);

#解析的时间:2011-05-27 17:11:15.271816 =>周五5月27日17时15分46秒美国东部时间2011
#解析的时间:2011-05-27 17:09:37.750343 =>周五5月27日17时22分07秒美国东部时间2011
#解析的时间:2011-05-27 17:05:55.182921 =>周五5月27日17时08分五十七秒EDT 2011
#解析的时间:2011-05-27 16:55:05.69092 =>周五5月27日16时56分14秒美国东部时间2011
#解析的时间:2011-05-27 16:38:35.50348 =>周五5月27日16点39分25秒美国东部时间2011
 
还在使用SimpleDateFormat 你的项目崩没

我从格式取下分数秒不动了。

 的SimpleDateFormat dateFormater =新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS);

#解析的时间:2011-05-27 17:11:15.271816 =>周五5月27日17时11分15秒美国东部时间2011
#解析的时间:2011-05-27 17:09:37.750343 =>周五5月27日17时○九分37秒美国东部时间2011
#解析的时间:2011-05-27 17:05:55.182921 =>周五5月27日17点05分55秒美国东部时间2011
#解析的时间:2011-05-27 16:55:05.69092 =>周五5月27日十六时55分05秒美国东部时间2011
#解析的时间:2011-05-27 16:38:35.50348 =>周五5月27日16时38分35秒美国东部时间2011
 

我认为正在发生的是输入字符串的我的小数秒的部分过长(同样是在OP例如真)。这似乎是期待只有三小数。如果你做数学(拿第一个例子):

在小数秒=0.271816秒 在什么日期格式看到的是一千分之二十七万一千八百十六第二的 一千分之二十七万一千八百十六==271秒 六十分之二百七十一= 4分 271%60 =31秒 在17点十一分15秒至17时15分46秒,正是4分钟,31秒掉

I'm using the SimpleDateFormat object with the Date object as shown below. The problem lis that the Date object shows the wrong date, which is a few minutes off from the original string. The Date object appears to store the time in total milliseconds in the debugger.

Any ideas on the problem?

import java.text.SimpleDateFormat;

import java.util.Date;

Date played_at_local; 

dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSSSSZ");

played_at_local = dateFormat.parse("2011-04-11T22:27:18.491726-05:00"); 

//played_at_local shows "Mon Apr 11 22:35:29 America/Chicago 2011" in debugger

解决方案

Try removing the fractional seconds from the format string. I just ran into the same issue, but with a slightly different format. My input format wasn't in ISO format (no "T", and no "Z"), but the symptom was the same -- time was off by some random number of minutes and seconds, but everything else was fine. This is what my log results looked like:

When using the fractional second format:

SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");

# Parsed date: 2011-05-27 17:11:15.271816 => Fri May 27 17:15:46 EDT 2011
# Parsed date: 2011-05-27 17:09:37.750343 => Fri May 27 17:22:07 EDT 2011
# Parsed date: 2011-05-27 17:05:55.182921 => Fri May 27 17:08:57 EDT 2011
# Parsed date: 2011-05-27 16:55:05.69092 => Fri May 27 16:56:14 EDT 2011
# Parsed date: 2011-05-27 16:38:35.50348 => Fri May 27 16:39:25 EDT 2011

I fixed it by removing the fractional seconds from the format.

SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

# Parsed date: 2011-05-27 17:11:15.271816 => Fri May 27 17:11:15 EDT 2011
# Parsed date: 2011-05-27 17:09:37.750343 => Fri May 27 17:09:37 EDT 2011
# Parsed date: 2011-05-27 17:05:55.182921 => Fri May 27 17:05:55 EDT 2011
# Parsed date: 2011-05-27 16:55:05.69092 => Fri May 27 16:55:05 EDT 2011
# Parsed date: 2011-05-27 16:38:35.50348 => Fri May 27 16:38:35 EDT 2011

What I think is happening is that my "fractional seconds" part of the input string is too long (the same is true in the OP example). It appears to be expecting only three decimal places. If you do the math (take the first example):

fractional seconds = 0.271816 seconds What DateFormat sees is 271816 / 1000 of a second 271816 / 1000 == 271 seconds 271 / 60 = 4 minutes 271 % 60 = 31 seconds 17:11:15 to 17:15:46 is exactly 4 minutes, 31 seconds off