GetTime 不能正确显示零
GetTime doesn't correctly display zero's
我在我的系统中使用时间戳。一切正常,但我发现将 [12:2:2] 作为时间戳很烦人。因为你不知道它是 20 还是 2。
累死了,开始以为是编译器的问题。
java.util.Date date= new java.util.Date();
Timestamp time = new Timestamp(date.getTime());
super.print("[" + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + "] ");re
你们如何获得带零的正确时间戳?
使用带有 %2d
标志的简单 String.format
,即至少有 2 位数字的十进制数。
String.format("[%2d:%2d:%2d] ", time.getHours(), time.getMinutes(), time.getSeconds())
看看 DateFormat or SimpleDateFormat class.
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());
您可以像这样使用 DateTimeFormatter
:
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
DateTime dateTime = format.parseDateTime(s);
其中 s 是您的数据时间字符串。
我在我的系统中使用时间戳。一切正常,但我发现将 [12:2:2] 作为时间戳很烦人。因为你不知道它是 20 还是 2。
累死了,开始以为是编译器的问题。
java.util.Date date= new java.util.Date();
Timestamp time = new Timestamp(date.getTime());
super.print("[" + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + "] ");re
你们如何获得带零的正确时间戳?
使用带有 %2d
标志的简单 String.format
,即至少有 2 位数字的十进制数。
String.format("[%2d:%2d:%2d] ", time.getHours(), time.getMinutes(), time.getSeconds())
看看 DateFormat or SimpleDateFormat class.
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());
您可以像这样使用 DateTimeFormatter
:
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
DateTime dateTime = format.parseDateTime(s);
其中 s 是您的数据时间字符串。