[英]GetTime doesn't correctly display zero's
我在系統中使用TimeStamps。 一切正常,但是我發現將[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
你們如何獲得零的正確時間戳?
看一下DateFormat或SimpleDateFormat類。
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());
使用帶有%2d
標志的簡單String.format
,即十進制數字至少2位。
String.format("[%2d:%2d:%2d] ", time.getHours(), time.getMinutes(), time.getSeconds())
您可以像這樣使用DateTimeFormatter
:
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
DateTime dateTime = format.parseDateTime(s);
其中s是您的數據時間字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.