Reputation: 91
I use TimeStamps in my system. Everything works correctly, however I find it annoying to have [12:2:2] as TimeStamp. Because you don't know if it's 20 or 2.
I am tired and sick, and starting to think it is the compiler.
java.util.Date date= new java.util.Date();
Timestamp time = new Timestamp(date.getTime());
super.print("[" + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + "] ");re
How do you guys get correct TimeStamps with the zero's?
Upvotes: 1
Views: 109
Reputation: 172548
You can use the DateTimeFormatter
like this:
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
DateTime dateTime = format.parseDateTime(s);
where s is your datatime string.
Upvotes: 0
Reputation: 23665
Have a look at the DateFormat or SimpleDateFormat class.
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());
Upvotes: 4
Reputation: 8217
Use simple String.format
with %2d
flag, i.e. decimal number with at least 2 digits.
String.format("[%2d:%2d:%2d] ", time.getHours(), time.getMinutes(), time.getSeconds())
Upvotes: 2