在date.gettime()中获得负值 [英] Getting negative value in date.gettime()
本文介绍了在date.gettime()中获得负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
面对一个问题,其中我试图将字符串值解析为date,而当我尝试进行date.getTime()时,我得到的是负值.
Facing an issue where I am trying to parse a string value into date and when I try to do date.getTime() I am getting negative value.
这是我正在解析的字符串"01:00:07".这是日期对象中的值,我得到的是"Thu Jan 01 01:00:07 GMT + 05:30 1970".仍然在getTime()中,我得到的是负值"-16193000"
This is the string I am parsing "01:00:07". and this is the value in date object I get "Thu Jan 01 01:00:07 GMT+05:30 1970". Still in getTime() I am getting negative value "-16193000"
实现此目的的代码:
long sum = 0;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");// I have also tried "HH:mm:ss" format. it gives same result
Date date = simpleDateFormat.parse(duration);//duration is "01:00:07"
sum= date.getTime();
推荐答案
问题是 TimeZone
.通过提供 TimeZone
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
或
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
这篇关于在date.gettime()中获得负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文