1

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();
Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62
  • Whats the datatype of sum? Hope you have taken it as long – Ankur Aggarwal Dec 20 '18 at 07:53
  • datatype of sum is long – Parth Anjaria Dec 20 '18 at 07:55
  • you are in which timezone ? – Shivakumar ss Dec 20 '18 at 08:13
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Dec 20 '18 at 08:26
  • 1
    A `Date` and a duration are two quite different beasts. Your problem is that you are misusing one for the other. See for example [Java 8 Time API: how to parse string of format “mm:ss” to Duration?](https://stackoverflow.com/questions/24642495/java-8-time-api-how-to-parse-string-of-format-mmss-to-duration) – Ole V.V. Dec 20 '18 at 08:29
  • Minor point, had `01:00:07` been a time of day, you would have needed uppercase `HH` for hour of day. – Ole V.V. Dec 20 '18 at 08:30
  • @OleV.V. it is the duration of a workout not time of day. – Parth Anjaria Dec 20 '18 at 09:41
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Dec 20 '18 at 21:31

4 Answers4

2

The problem is TimeZone. You will get correct time by providing TimeZone

simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

or

simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40
1

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();
}
Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
1

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

simpleDateFormat .setTimeZone(TimeZone.getTimeZone("GMT")); you have to add your timezone

Alexandru
  • 98
  • 8