日期对象 SimpleDateFormat 在 Java (Android) 环境中无法正确解析时间戳字符串 [英] Date object SimpleDateFormat not parsing timestamp string correctly in Java (Android) environment

查看:30
本文介绍了日期对象 SimpleDateFormat 在 Java (Android) 环境中无法正确解析时间戳字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 SimpleDateFormat 对象与 Date 对象一起使用,如下所示.问题在于 Date 对象显示了错误的日期,与原始字符串相差几分钟.Date 对象似乎以总毫秒数存储在调试器中的时间.

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

推荐答案

尝试从格式字符串中删除小数秒.我刚刚遇到了同样的问题,但格式略有不同.我的输入格式不是 ISO 格式(没有T",也没有Z"),但症状是一样的——时间差了一些随机的分秒数,但其他一切都很好.这是我的日志结果的样子:

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:

当使用小数秒格式时:

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

<小时>

我认为正在发生的是我输入字符串的小数秒"部分太长(在 OP 示例中也是如此).它似乎只需要三位小数位.如果你做数学(以第一个例子为例):


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):

  • 小数秒 = 0.271816 秒
  • DateFormat 看到的是 271816/1000 一秒
  • 271816/1000 == 271 秒
  • 271/60 = 4 分钟
  • 271 % 60 = 31 秒
  • 17:11:15 到 17:15:46 正好是 4 分 31 秒

这篇关于日期对象 SimpleDateFormat 在 Java (Android) 环境中无法正确解析时间戳字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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