简体   繁体   English

Dateformat 增加了额外的 30 分钟时间

[英]Dateformat adds extra 30 minutes to time

I'm using this Android code for converting milliseconds to mm:ss.SS format but in result dateformat adds 30 extra minutes in date.我正在使用此 Android 代码将毫秒转换为 mm:ss.SS 格式,但结果 dateformat 增加了 30 分钟的日期。

        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.其实我想将毫秒转换为 m:ss.SS 格式。 Is there any other best way to achieve this?有没有其他最好的方法来实现这一目标?

Thanks in advance.提前致谢。

to set the TImezone :设置时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...我的猜想...如果您生活在 GMT+30 分钟时区,并且除非您指定不同的时区,否则您的格式化程序将选择您当前的时区,因此它将 0 小时视为 GMT,而您处于 GMT+30 分钟,它输出 + 30 分钟...

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 "它会返回像“ IST ”这样的时区

Also you can try the following code to find the GMT offset of a Timezone:您也可以尝试以下代码来查找时区的 GMT 偏移量:

 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它会像这样返回时区: GMT+05:30这种格式

By using timezone you can find accurate time in all devices..通过使用时区,您可以在所有设备中找到准确的时间。

see this also:https://developer.android.com/reference/java/util/TimeZone.html另请参阅:https ://developer.android.com/reference/java/util/TimeZone.html

您可以将代码更改为:

DurationFormatUtils.formatDuration(millis, "mm:ss.SS");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM