如何从 Java 8 中的 LocalTime 中删除毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25536500/
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
How to remove milliseconds from LocalTime in java 8
提问by Nikolas
Using the java.time framework, I want to print time in format hh:mm:ss
, but LocalTime.now()
gives the time in the format hh:mm:ss,nnn
. I tried to use DateTimeFormatter
:
使用 java.time 框架,我想以 format 打印时间hh:mm:ss
,但以 formatLocalTime.now()
给出时间hh:mm:ss,nnn
。我尝试使用DateTimeFormatter
:
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
LocalTime time = LocalTime.now();
String f = formatter.format(time);
System.out.println(f);
The result:
结果:
22:53:51.894
How can I remove milliseconds from the time?
如何从时间中删除毫秒?
采纳答案by Jon Skeet
Just create the DateTimeFormatter
explicitly:
只需DateTimeFormatter
明确创建:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.US);
LocalTime time = LocalTime.now();
String f = formatter.format(time);
System.out.println(f);
(I prefer to explicitly use the US locale, to make it clear that I don'twant anything from the default format locale.)
(我更喜欢明确使用美国语言环境,以明确表示我不想要默认格式语言环境中的任何内容。)
回答by demos74dx
Edit: I should add that these are nanoseconds not milliseconds.
编辑:我应该补充一点,这些是纳秒而不是毫秒。
I feel these answers don't really answer the question using the Java 8 SE Date and Time API as intended. I believe the truncatedTo method is the solution here.
我觉得这些答案并没有像预期的那样使用 Java 8 SE 日期和时间 API 真正回答问题。我相信 truncatedTo 方法是这里的解决方案。
LocalDateTime now = LocalDateTime.now();
System.out.println("Pre-Truncate: " + now);
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
System.out.println("Post-Truncate: " + now.truncatedTo(ChronoUnit.SECONDS).format(dtf));
Output:
输出:
Pre-Truncate: 2015-10-07T16:40:58.349
Post-Truncate: 2015-10-07T16:40:58
Alternatively, if using Time Zones:
或者,如果使用时区:
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zoned = now.atZone(ZoneId.of("America/Denver"));
System.out.println("Pre-Truncate: " + zoned);
DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
System.out.println("Post-Truncate: " + zoned.truncatedTo(ChronoUnit.SECONDS).format(dtf));
Output:
输出:
Pre-Truncate: 2015-10-07T16:38:53.900-06:00[America/Denver]
Post-Truncate: 2015-10-07T16:38:53-06:00
回答by Mike_vin
cut to minutes:
缩短到分钟:
localTime.truncatedTo(ChronoUnit.MINUTES);
cut to seconds:
切到秒:
localTime.truncatedTo(ChronoUnit.SECONDS);
Example:
例子:
LocalTime.now().truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_LOCAL_TIME)
Outputs 15:07:25
输出 15:07:25
回答by Justin
Use this in your first line
在你的第一行使用它
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
回答by dpassy
Try to use patterns defined here: http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
尝试使用此处定义的模式:http: //docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
For example:
例如:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd HH. mm. ss");
String text = date.toString(formatter);
回答by Bohemian
You can so it simply by using regex on the string:
您只需在字符串上使用正则表达式即可:
String f = formatter.format(time).replaceAll("\.[^.]*", "");
This deletes (by replacing with blank) the last dot and everything after.
这将删除(通过替换为空白)最后一个点和之后的所有内容。