简体   繁体   English

经过时间格式HH:mm:ss

[英]Elapsed time format HH:mm:ss

It's a simple problem but I'm not getting it to work. 这是一个简单的问题,但我无法使其正常工作。

I'm incrementing a variable each second and setting it in a GregorianCalendar in miliseconds. 我每秒增加一个变量,并以毫秒为单位将其设置在GregorianCalendar中。

And I'm using this format HH:mmss to present the elpased time. 我正在使用这种格式HH:mmss来表示提前时间。

The problem is that the hour starts showing 01 instead of 00. For instance, after 1 minute and 35 seconds what is shown is: 01:01:35 instead of 00:01:35 . 问题是小时开始显示01而不是00。例如,在1分35秒之后,显示的是: 01:01:35 : 01:01:35 : 01:01:35而不是00:01:35

Where could be the problem? 问题可能出在哪里?

There is the important code: 有重要的代码:

GregorianCalendar timeIntervalDone = new GregorianCalendar(TimeZone.getTimeZone("GMT-1")); //initially I didn't have the TimeZone set, but no difference
SimpleDateFormat dateTimeIntervalFormat = new SimpleDateFormat("HH:mm:ss");

public String getTimeIntervalDoneAsString() {
    timeIntervalDone.setTimeInMillis(mTimeIntervalDone); //mTimeIntervalDone is the counter: 3seccond -> mTimeIntervalDone = 3000
    return dateTimeIntervalFormat.format(timeIntervalDone.getTime());
}

I think the reason is you set the time zone to GMT-1, but the output is utc. 我认为原因是您将时区设置为GMT-1,但输出是utc。 Please try it without that time zone and it should work. 请在没有该时区的情况下尝试它,它应该可以工作。

I finally got it: 我终于明白了:

GregorianCalendar timeIntervalDone = new GregorianCalendar(); 
SimpleDateFormat dateTimeIntervalFormat = new SimpleDateFormat("HH:mm:ss");
dateTimeIntervalFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

Your approach is a hack, trying to use a date-time moment class ( GregorianCalendar ) to represent a span of time. 您的方法是破解,试图使用日期时间矩类( GregorianCalendar )来表示时间跨度。 Plus your format is ambiguous, looking like a time-of-day rather than a duration. 另外,您的格式是模棱两可的,看起来像是一天中的时间而不是持续时间。

ISO 8601 ISO 8601

An alternative is to use the ISO 8601 standard way of describing a duration: PnYnMnDTnHnMnS where the P marks the beginning and the T separates the year-month-day portion from hour-minute-second portion. 一种替代方法是使用ISO 8601标准描述持续时间: PnYnMnDTnHnMnS ,其中P标记开始,而T则将年月日部分与时分秒部分分开。

java.time java.time

The java.time framework in Java 8 and later supplants the old java.util.Date/.Calendar classes. Java 8和更高版本中的java.time框架取代了旧的java.util.Date/.Calendar类。 The old classes have proven to be troublesome, confusing, and flawed. 老类被证明是麻烦,混乱和有缺陷的。 Avoid them. 避免他们。

The java.time framework is inspired by the highly-successful Joda-Time library, defined by JSR 310 , extended by the ThreeTen-Extra project, and explained in the Tutorial . java.time框架的灵感来自于JSR 310定义的,由ThreeTen-Extra项目扩展的,非常成功的Joda-Time库,并在Tutorial中进行了说明。

The java.time framework does use ISO 8601 as its defaults, this otherwise excellent set of classes lacks a class to represent an entire period of years-months-days-hours-minutes-seconds. java.time框架确实使用ISO 8601作为其默认值,否则,这套极好的类集缺少一个类来表示整年的年月日天小时分钟数秒。 Instead it breaks the concept into two. 相反,它将概念分为两​​部分。 The Period class handles years-months-days while the Duration class handles hours-minutes-seconds. Period类处理年-月-天,而Duration类则处理小时-分钟-秒。

Instant now = Instant.now ();
Instant later = now.plusSeconds ( 60 + 35 ); // One minute and 35 seconds later.

Duration duration = Duration.between ( now , later );
String output = duration.toString ();

Dump to console. 转储到控制台。

System.out.println ( "output: " + output );

output: PT1M35S 输出:PT1M35S

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

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