I am trying to convert a long value (The number of milliseconds elapsed from 1/1/1970) to a time of format h:m:s:ms
The long value I use as timestamp, I get from the field timestamp
of logging event from log4j.
How do I do the conversion?
For example to get the minutes I tried the following and all fail:
logEvent.timeStamp/ (1000*60*60) and
TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
but I get garbage:
I get
1289375173771 for logEvent.timeStamp
358159 for logEvent.timeStamp/ (1000*60*60)
21489586 for TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
How can I convert this?
Thanks
Try this:
I'll show you three ways to (a) get the minute field from a long value, and (b) print it using the Date format you want. One uses java.util.Calendar, another uses Joda-Time, and the last uses the java.time framework built into Java 8 and later.
The java.time framework supplants the old bundled date-time classes, and is inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project.
The java.time framework is the way to go when using Java 8 and later. Otherwise, such as Android, use Joda-Time. The java.util.Date/.Calendar classes are notoriously troublesome and should be avoided.
java.util.Date & .Calendar
Joda-Time
Output:
java.time
Doing
will give you hours, not minutes. Try:
and you will end up with the same answer as