问题
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.
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"
Code for achieving this :
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();
回答1:
The problem is TimeZone
. You will get correct time by providing TimeZone
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
or
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
回答2:
java.time
String durationString = "01:00:07";
durationString = durationString
.replaceFirst("(\\d{2}):(\\d{2}):(\\d{2})", "PT$1H$2M$3S");
Duration dur = Duration.parse(durationString);
System.out.println(dur);
A Duration
outputs this odd string:
PT1H7S
Read as: a period of time of 1 hour 7 seconds.
That string is in standard ISO 8601 format. A Duration
can only parse the standard format. So I am using String.replaceFirst
to convert your string to it.
Let me guess, you need to sum up durations? There’s a plus
method for that:
Duration sum = Duration.ZERO;
sum = sum.plus(dur);
There are also methods for converting to seconds or milliseconds.
Don’t use a date-time class for a duration. It will lead to confusion and errors.
If you absolutely don’t want an external dependency (only until you move to API level 26), hand parse your string into seconds and represent them in an int
. Wrap it in a custom class of your own.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining how to use
java.time
. - Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Wikipedia article: ISO 8601
回答3:
simpleDateFormat .setTimeZone(TimeZone.getTimeZone("GMT"));
you have to add your timezone
回答4:
Problem is timezone offset. Use:
long sum = 0;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");// I have also tried "HH:mm:ss" format. it gives same result
Date date = null;//duration is "01:00:07"
try {
date = simpleDateFormat.parse("01:00:07");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
sum= cal.getTimeInMillis() + (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET));
System.out.println("Date =" + date);
System.out.println("sum =" + sum);
} catch (ParseException e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/53864387/getting-negative-value-in-date-gettime