在 Java 中打印时差的最惯用方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2704473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most idiomatic way to print a time difference in Java?
提问by Zombies
I'm familiar with printing time difference in milliseconds:
我熟悉以毫秒为单位的打印时间差:
long time = System.currentTimeMillis();
//do something that takes some time...
long completedIn = System.currentTimeMillis() - time;
But, is there a nice way print a complete time in a specified format (eg: HH:MM:SS) either using Apache Commons or even the dreaded platform API's Date/Time objects? In other words, what is the shortest, simplest, no nonsense way to write a time format derived from milliseconds in Java?
但是,是否有一种使用 Apache Commons 甚至可怕的平台 API 的日期/时间对象以指定格式(例如:HH:MM:SS)打印完整时间的好方法?换句话说,在Java中编写从毫秒派生的时间格式的最短,最简单,没有废话的方法是什么?
回答by Bill the Lizard
Apache Commons has the DurationFormatUtilsclass for applying a specified format to a time duration. So, something like:
Apache Commons 具有DurationFormatUtils类,用于将指定格式应用于持续时间。所以,像这样:
long time = System.currentTimeMillis();
//do something that takes some time...
long completedIn = System.currentTimeMillis() - time;
DurationFormatUtils.formatDuration(completedIn, "HH:mm:ss:SS");
回答by trashgod
A library designed for the purpose is the better approach, but SimpleDateFormatwith the right TimeZonemay suffice for periods less than a day. Longer periods require treating the day specially.
为此目的而设计的库是更好的方法,但如果SimpleDateFormat使用正确的方法,则TimeZone可能在不到一天的时间内就足够了。更长的时间需要特别对待这一天。
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class Elapsed {
private static final long MS_DAY = 24 * 60 * 60 * 1000;
private final DateFormat df = new SimpleDateFormat("HH : mm : ss : S");
public Elapsed() {
df.setTimeZone(TimeZone.getTimeZone("GMT"));
}
private String format(long elapsed) {
long day = elapsed / MS_DAY;
StringBuilder sb = new StringBuilder();
sb.append(day);
sb.append(" : ");
sb.append(df.format(new Date(elapsed)));
return sb.toString();
}
public static void main(String[] args) {
Elapsed e = new Elapsed();
for (long t = 0; t < 3 * MS_DAY; t += MS_DAY / 2) {
System.out.println(e.format(t));
}
}
}
Console output:
控制台输出:
0 : 00 : 00 : 00 : 0 0 : 12 : 00 : 00 : 0 1 : 00 : 00 : 00 : 0 1 : 12 : 00 : 00 : 0 2 : 00 : 00 : 00 : 0 2 : 12 : 00 : 00 : 0
回答by Basil Bourque
tl;dr
tl;博士
Duration.ofMillis( … )
.toString()
…or…
…或者…
Duration.between( // `Duration` represents a span of time unattached to the timeline, in a scale of days-hours-minutes-seconds-nanos.
instantEarlier , // Capture the current moment to start.
Instant.now() // Capture the current moment now, in UTC.
) // Returns a `Duration` object.
.toString() // Generate a string in standard ISO 8601 format.
PT1M23.407S
PT1M23.407S
ISO 8601
ISO 8601
what is the shortest, simplest, no nonsense way to write a time format derived from milliseconds
编写从毫秒派生的时间格式的最短,最简单,没有废话的方法是什么
The ISO 8601standard defines textual formats for date-time values. These formats are indeed short, simple, and no-nonsense. They are largely human-readable across various cultures, practical, and unambiguous.
在ISO 8601标准定义为考证日期时间值的格式。这些格式确实简短、简单且没有废话。它们在不同文化中基本上是人类可读的,实用且明确。
For spans of time unattached to the timeline, the standard formatis PnYnMnDTnHnMnS. The Pmarks the beginning, and the Tseparates the years-month-days from the hours-minutes-seconds. So an hour and a half is PT1H30M.
对于未附加到时间线的时间跨度,标准格式为PnYnMnDTnHnMnS. 该P标记的开始,而T分离小时-分-秒年-月-日。所以一个半小时是PT1H30M。
java.time.Duration
java.time.Duration
The java.time classes include a pair of classes to represent spans of time. The Durationclass is for hours-minutes-seconds, and Period.
java.time 类包括一对表示时间跨度的类。该Duration课程以小时-分钟-秒为单位,并且Period.
Instant
Instant
The Instantclass represents a moment on the timeline in UTCwith a resolution of nanoseconds(up to nine (9) digits of a decimal fraction).
该Instant级表示时间轴上的时刻UTC,分辨率为纳秒(最多小数的9个位数)。
Instant earlier = Instant.now() ; // Capture the current moment in UTC.
…
Instant later = Instant.now() ;
Duration d = Duration.between( earlier , later ) ;
String output = d.toString() ;
PT3.52S
PT3.52S
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.
您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9、Java SE 10及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval,YearWeek,YearQuarter,和更多。
回答by Michael Borgwardt
回答by Thorbj?rn Ravn Andersen
回答by Dmitry Podobedov
long diff = System.currentTimeMillis() - startTime;
int hours = (int)(diff / 3600000); diff -= hours * 3600000;
int mins = (int)(diff / 60000); diff -= mins * 60000;
int secs = (int)(diff / 1000); diff -= secs * 1000;
System.out.println(String.format("%d:%d:%d.%d", hours, mins, secs, diff));

