12:xx在SimpleDateFormat.format("hh:mm:ss")中显示为00:xx [英] 12:xx shown as 00:xx in SimpleDateFormat.format("hh:mm:ss")
问题描述
在以下代码中使用SimpleDateFormatter.format时,startDateText TextView中12:00和12:59之间的小时显示为00:00到00:59,而从13:00开始,它们正确显示为13:xx,从14:xx到23:59.
When using SimpleDateFormatter.format in the following code, hours between 12:00 and 12:59 are shown as 00:00 to 00:59 in startDateText TextView, while since 13:00 on they are correctly shown as 13:xx, 14:xx up to 23:59.
----按要求重构代码例如,当dtold.parse(...)中的字符串为in时,输出小时为00:00,当其为"13:00"时,则正确为"13:00"
---- Refactored code as requested When the string in the dtold.parse(...) is an the in example the output hour is 00:00, when it is "13:00" it is correctly "13:00"
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
SimpleDateFormat dtnew = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
SimpleDateFormat dtold = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
Calendar cal = Calendar.getInstance();
cal.setTime(dtold.parse("2017-03-12 12:33:33"));
cal.add(Calendar.SECOND, 10);
System.out.println(dtnew.format(cal.getTime()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
推荐答案
首先像您这样的格式化程序,仅使用现代Java的 java.time
中的 DateTimeFormatter
日期和时间API:
First a couple of formatters like yours, only using DateTimeFormatter
from java.time
, the modern Java date and time API:
private static DateTimeFormatter dtfOld = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static DateTimeFormatter dtfNew = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
需要注意的两件事:(1)按照逻辑顺序声明格式化程序,即使用它们的顺序.在问题中使用相反的顺序使我感到困惑,我不确定是否也使自己感到困惑.(2)在 dtfOld
中,将大写的 HH
用于一天中的小时,范围为00到23.小写的 hh
表示从01到12(在这种情况下,相同的格式模式字母适用于 SimpleDateFormat
和 DateTimeFormatter
;但是有所不同).现在,其余的内容很无聊,只比您的代码简单:
Two things to note: (1) Declare the formatters in the logical order, the order in which you are going to use them. Using the opposite order in the question confused me, and I’m unsure whether it confused yourself too. (2) In dtfOld
use uppercase HH
for hour of day in the interval 00 through 23. Lowercase hh
is for hour within AM or PM from 01 through 12 (in this case the same format pattern letters apply for SimpleDateFormat
and DateTimeFormatter
; there are differences, though). Now the rest is pretty boring, only simpler than your code:
LocalDateTime parsed = LocalDateTime.parse("2017-03-12 12:33:33", dtfOld);
System.out.println(parsed);
LocalDateTime dateTime = parsed.plusSeconds(10);
System.out.println(dateTime);
System.out.println(dateTime.format(dtfNew));
输出为:
2017-03-12T12:33:33
2017-03-12T12:33:43
12-03-2017 12:33:43
我建议使用 java.time
.您使用的旧日期和时间类- SimpleDateFormat
, Calendar
和 Date
-已经过时了.不仅是在这种情况下,现代类还允许使用更简单的代码,这非常普遍.我发现 java.time
通常更好用.
I am recommending java.time
. The old date and time classes that you used — SimpleDateFormat
, Calendar
and Date
— are long outdated. It’s not only in this case that the modern classes allow for simpler code, it’s quite common. I find java.time
generally so much nicer to work with.
我已经给出了一个提示:小写的 hh
是从01到12的AM或PM中的小时..而12:33:33 AM表示午夜后半小时多一点,并在24小时制上显示为00:33:33.
I gave a hint already: Lowercase hh
is for hour within AM or PM from 01 through 12. When you don’t supply and parse an AM/PM marker, AM is used as the default. And 12:33:33 AM means a little more than half an hour past midnight, and is rendered as 00:33:33 on a 24 hour clock.
从13:00到23:59的时间?它们在AM中不存在.显然 SimpleDateFormat
不在乎,只是从01到11的时间进行推断,因此恰好能给您所需的时间.有个窍门告诉它不要;但我不想打扰,我宁愿完全不使用该课程.
The times from 13:00 up to 23:59? They don’t exist in AM. Apparently SimpleDateFormat
doesn’t care and just extrapolates from the hours from 01 through 11 and therefore happens to give you the time you had expected. There’s a trick to tell it not to; but I wouldn’t want to bother, I’d rather not use the class at all.
Oracle教程:日期时间解释了如何使用 java.时间
.
这篇关于12:xx在SimpleDateFormat.format("hh:mm:ss")中显示为00:xx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!