String-Date转换为纳秒 [英] String-Date conversion with nanoseconds
问题描述
我一直在为Android应用程序的这段代码挣扎一段时间,我无法理解它。我已经阅读并尝试了我在stackoverflow和其他地方找到的每个解决方案,但仍然没有运气。
我想要的是有一个转换字符串的函数,如17.08.2012 05:35:19:7600000
到UTC日期和一个 UTC日期
的函数,并将其转换为类似的字符串。
String value =17.08.2012 05:35:19:7600000;
DateFormat df = new SimpleDateFormat(dd.MM.yyyy HH:mm:ss:SSSSSSS);
尝试
{
今日日期= df.parse(价值);
System.out.println(Today =+ df.format(today)++ today.toGMTString());
}
catch(ParseException e)
{
e.printStackTrace();
}
这导致:今天= 17.08.2012 07 :41:59:0000000 17 Aug 2012 04:41:59 GMT
哪都错了。
我尝试将 SDF的时区
设置为 UTC
,否运气。
我注意到的另一件事:如果我做 df.setLenient(false);
它给了我: java.text.ParseException:无法解析的日期:17.08.2012 05:35:19:7600000
。
如果有人能提供一些解释/示例代码,我将非常感激。提前致谢
您获得的结果绝对正确。
<让我们分析一下:
17.08.2012 05:35:19:7600000
- 17:日期(17日)
- 08:一年中的一个月(八月)
- 2012:年(2012)
- 05:一天中的小时(早上5点)
- 35:分钟(:35)
- 19:秒分钟(:19)
- 7600000:毫秒秒(7,600,000)
现在,VM看到这种情况的方式是你将时间宣布为5:35:19 am,然后增加7,600,000毫秒它。 7,600,000毫秒= 7,600秒= 2小时6分40秒。凌晨5:35:19 + 02:06:40 = 7:41:59 am(和0毫秒)这是你得到的结果。 (您似乎没有正确设置时区,因此GMT字符串比您的结果落后3小时。)
如果您想保留:7600000
,据我所知这是不可能的。由于这可以简化为几秒钟,因此VM会自动将其减少到其他时间增量。毫秒( SSSS
)应该用于存储值<1000。
我建议您创建一个输出的新 SimpleDateFormat
;但请记住,毫秒将被吸收到其他时间(因为它们都存储为日期
long >对象)。
I've been struggling for a while with this piece of code for an Android app and I can't get the hang of it. I've read and tried every solution I found on stackoverflow and other places, but still no luck.
What I want to do is have a function to convert a string like "17.08.2012 05:35:19:7600000"
to a UTC date and a function that takes an UTC date
and converts it to a string like that.
String value = "17.08.2012 05:35:19:7600000";
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
try
{
Date today = df.parse(value);
System.out.println("Today = " + df.format(today) + " " + today.toGMTString());
}
catch (ParseException e)
{
e.printStackTrace();
}
This results in : Today = 17.08.2012 07:41:59:0000000 17 Aug 2012 04:41:59 GMT
which are both wrong.
I tried setting SDF's timezone
to UTC
, no luck.
Another thing that I noticed: if I do df.setLenient(false);
It gives me : java.text.ParseException: Unparseable date: "17.08.2012 05:35:19:7600000"
.
If anyone can provide me with some explanations / sample code, I would be very grateful. Thanks in advance
The result you are getting is absolutely right.
Let's analyze this:
17.08.2012 05:35:19:7600000
- 17: Day of month (17th)
- 08: Month of year (August)
- 2012: Year (2012)
- 05: Hour of day (5am)
- 35: Minute of hour (:35)
- 19: Second of minute (:19)
- 7600000: Milliseconds of second (7,600,000)
Now, the way the VM sees this is that you are declaring the time of day as 5:35:19am, then adding 7,600,000 milliseconds to it. 7,600,000 milliseconds = 7,600 seconds = 2 hours, 6 minutes, 40 seconds. 5:35:19am + 02:06:40 = 7:41:59am (and 0 milliseconds). This is the result you are getting. (It also appears that you are not setting the timezone properly, so the GMT string is 3 hours behind your result.)
If you want to retain the :7600000
, to my knowledge this is not possible. As this can be simplified into seconds, the VM will automatically reduce it into the other time increments. The milliseconds (the SSSS
) should be for storing values <1000.
I'd suggest you create a new SimpleDateFormat
for your output; but remember that the milliseconds will be absorbed into the other times (since they are all stored as a single long
in the Date
object).
这篇关于String-Date转换为纳秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!