-2

When I parse some Dates (times) using simple date format, the code below gives me five hours more than the expected time. I'm parsing milliseconds to string. I'm parsing the duration of some Media files. For example, it gives 5 hour 10 minute when my media file is actually 10 minute long. What is the problem?

public static String formatTime(String time)  {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("H:mm:ss");
    Date  date = new Date(Long.parseLong(time));
    return simpleDateFormat.format(date);
Rob
  • 2,243
  • 4
  • 29
  • 40
Ameer Hamza
  • 27
  • 1
  • 5
  • Can you add some examples of input values for `Time`? – Henrik Aasted Sørensen Sep 26 '17 at 13:14
  • add some time parameters, what you are passing?? – Mohd Asif Ahmed Sep 26 '17 at 13:16
  • 2
    You are using a `java.util.Date` object for storing only a time (hours, minutes, seconds), but a `Date` object is not suitable for storing only a time. – Jesper Sep 26 '17 at 13:18
  • It would help to post some examples of input and output. It might be worth calling `simpleDateFormat.setLenient(false)` before parsing to highlight invalid values. Also, you mention milliseconds. Are you trying to parse 00:10:00.18000000 which interprets the milliseconds as 5 hours? – Sarah Phillips Sep 26 '17 at 13:24
  • I'm not passing myself I'm passing what mediaStore return.... String duration = cursor.getString(durationColumn); – Ameer Hamza Sep 26 '17 at 13:29
  • 1
    You could add `Log.d("FormatTime",Time)` in the beginning of the method, then look for entries tagged "FormatTime" in LogCat to provide an example of the value passed to your method. – Haem Sep 26 '17 at 13:34
  • 1
    The one millisecond is like 227682 – Ameer Hamza Sep 26 '17 at 13:38

2 Answers2

1

You shouldn't be representing durations with Date. If Time is the media file's duration in milliseconds, then Date date = new Date(Long.parseLong(Time)); will construct a date that was that many milliseconds after 00:00 on January 1, 1970 UTC, in your case that will be 00:10 on that day.

I'm afraid that you have to parse your milliseconds into hours, minutes and seconds by hand, and then format it with String.format("%d:%02d:%02d",hours,minutes,seconds).

Haem
  • 929
  • 6
  • 15
  • 31
  • 2
    This is the correct answer. Parsing a duration into a `Date` is wrong on a very basic level. Look into the [`Duration`](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html) class instead. – Henrik Aasted Sørensen Sep 26 '17 at 13:44
  • How can it parse all millisecond by hand? – Ameer Hamza Sep 26 '17 at 13:48
  • @AmeerHamza use `Long.parseLong(time)` to get the milliseconds in a `long`, then use the `%` operator to take the remainder. Also, dividing an integer-type value like a `long` with another integer-type value always rounds down (i.e. discards the remainder). – Haem Sep 26 '17 at 13:52
  • 1
    @AmeerHamza: `Duration.of(51, ChronoUnit.MILLIS);`. Replace 51 with a variable containing the milliseconds. – Henrik Aasted Sørensen Sep 26 '17 at 13:56
  • 1
    @AmeerHamza It would sound like you have the wrong `Duration` class. You need either `java.time.Duration` or `org.threeten.bp.Duration` (depending on your setup). Check you import statement for the class. – Ole V.V. Sep 27 '17 at 07:59
-1

I use this way it's working fine

public static String FormatTime(String Time) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
    Date d=new Date(Long.parseLong(Time));
    String formatted=simpleDateFormat.format(d);
    if (Long.parseLong(Time) > 3600000&&Long.parseLong(Time)<7200000 ) {
        formatted=String.format("%d %s",1,formatted);
    }
    else if (Long.parseLong(Time)>=7200000&&Long.parseLong(Time)<10800000 ){
        formatted=String.format("%d %s",2,formatted);
    }
    return formatted;
}
Ameer Hamza
  • 27
  • 1
  • 5