1

I'm using this Android code for converting milliseconds to mm:ss.SS format but in result dateformat adds 30 extra minutes in date.

        Date date = new Date(millis);
        DateFormat dateFormat= new SimpleDateFormat("mm:ss.SS", Locale.US);
        best.add(dateFormat.format(date));\

Actually I want to convert milliseconds to m:ss.SS format. Is there any other best way to achieve this?

Thanks in advance.

akshay bhange
  • 2,320
  • 2
  • 28
  • 46
  • There is a problem with the timezone you are using. How are you calculating, illustrate an example? – Dharmendra Pratap Singh Jan 03 '17 at 19:25
  • I'm not using any timezone – akshay bhange Jan 04 '17 at 07:43
  • Of course you use a timezone, here implicitly your system timezone hidden in the class `SimpleDateFormat` (I consider implicit settings as evil). What does `TimeZone.getDefault()` print? And please also print the variable `millis` so we can help with further analysis. Another problem I see is the usage of only two symbols SS. You should use SSS for milliseconds. – Meno Hochschild Jan 04 '17 at 08:56
  • 1
    Looks like you want to format a duration and not a date. http://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss – laalto Jan 04 '17 at 12:19
  • @laalto Which is best way to format duration ? Dividing millis and get min, sec differently or using Date function? – akshay bhange Jan 04 '17 at 14:31
  • See the linked question and substitute "ThreeTenABP" in place of "Joda-Time". – laalto Jan 04 '17 at 14:35

2 Answers2

0

to set the TImezone:

dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

My Guess...if you are living in a GMT+30 min time zone and unless you specify a different one, your formatter will pick your current one, so it considers 0 hours as GMT and as you are in GMT+30 Min, it outputs + 30 Minutes...

To get timezone from user current place you can use:

 TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone   "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());

then you can set the Timezone in dateformat.

it will return you the timezone like "IST"

Also you can try the following code to find the GMT offset of a Timezone:

 public String getCurrentTimezoneOffset() {

    TimeZone tz = TimeZone.getDefault();  
    Calendar cal = GregorianCalendar.getInstance(tz);
    int offsetInMillis = tz.getOffset(cal.getTimeInMillis());

    String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
    offset = "GMT"+(offsetInMillis >= 0 ? "+" : "-") + offset;

    return offset;
}

it will return Timezone like: GMT+05:30 this format

By using timezone you can find accurate time in all devices..

see this also:https://developer.android.com/reference/java/util/TimeZone.html

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Yes, I live in a GMT+5:30 min time zone so this will change person to person.. what do I have to keep this same for all devices? – akshay bhange Jan 04 '17 at 11:31
0

You can change your code to:

DurationFormatUtils.formatDuration(millis, "mm:ss.SS");
Vadym
  • 436
  • 2
  • 5
  • 11