I'm looking for the name of the standard for time, represented in milliseconds since midnight January 1, 1970 UTC. We can get one calling System.currentTimeMillis() in Java. For example when we are talking about the same time, represented in seconds - we use term Unix time. It would be great if you provide a link to standard in the answer.
In the GNU Linux documentation it is called:
seconds since the epoch
Extrapolating you could call it milliseconds since the epoch.
Related
I have seen people using System.currentTimeInMillis and converting it into seconds to store unix utc epoch time. But in Java 8 we can also use Instant.ofEpochSeconds. Which one is more accurate? Is it better to use Instant.ofEpochMillis and convert it into seconds for better accuracy ?
Well, Instant.ofEpochSecond(long) takes an argument and is used to represent an Instant. I think you meant Instant.now() which says (in part)
This will query the system UTC clock to obtain the current instant.
And, system UTC clock says (in part)
This may use System.currentTimeMillis(), or a higher resolution clock if one is available.
So the answer is, it depends. But calling System.currentTimeMillis() which (per the Javadoc) returns
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
Note that unix epoch is in seconds.
When I use System.currentTimeMillis() to measure the time, is it safe?
E.g. When a time-shift happens (summer->winter time) will this produce an error?
No, System.currentTimeMillis() returns the number of elapsed milliseconds since the Unix epoch (midnight at the start of January 1st 1970, UTC). This does not depend on your local time zone at all.
In other words, if you call it once per second, it will always increase by about 1000 (the "about" is only due to clock accuracy and the impossibility of the calls being exactly 1 second apart). It doesn't matter if your local time zone changes its offset from UTC, or if you even change your whole time zone - that will be irrelevant.
In theory, leap seconds make all of this trickier - but in practice most applications can get away with notionally sticking their fingers in their ears and saying "I'm not at home to Mr Leap Second."
System.currentTimeMillis() will return the milliseconds since epoch (1/1/1970 00:00:00). How those are interpreted is a completely different story (and summer/winter time as well as timezone belong to the interpretation part). So yes, it would be save.
I am generating Continuity Of Care document that requires effective time. The document states:
CONF-9: ClinicalDocument / effectiveTime SHALL be expressed with precision to include seconds.
CONF-10: ClinicalDocument / effectiveTime SHALL include an explicit time zone offset.
Well, I can get current time in seconds:
long timeMillis = System.currentTimeMillis();
long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);
But I have no idea what the second part says about having time zone. A example of correct effective time would be 20000407130000+0500. It is from one of the sample.
System.currentTimeMillis() is UTC by definition.
From the javadoc
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
Just hard code UTC as the time zone for your timestamps.
In Joda, if I print
DateTime(GregorianChronology.getInstance())
.withYear(1970)
.withMonthOfYear(1)
.withDayOfMonth(1)
.withHourOfDay(0)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0).getMillis();
I see 18000000 (this also happens to be 1/4th of MILLIS_PER_DAY, FWIW).
What I don't understand is that if the milliseconds represents the offset from the epoch which is defined as Jan-1970-01-01, then shouldn't the milliseconds be 0?
The epoch is Jan-1970-01-01 GMT. The instance you have, obviously has a different DateTimeZone. In fact it lookds like you're at GMT+5. (18000000 millis = 5 hours)
I believe the issue is related to the way Java dates include the time zone as part of there calculations.
For me, this means I'm +10 hours ahead of the epoc.
Try creating a Date/Time value that is set to 0 GMT.
The "epoch" is a specific and universal instant, a point in the universe time (like, say the moment in which the Apollo XI landed on the moon). This reference point can be represented differently in different countries (and a martian could also represent it with his own calendar). For example, for the people in England (GMT), that's the moment in which the hands of their clocks marked "00:00:00" and their (Gregorian) calendars marked "1/1/1970"; but that's just an example.
The line
DateTime(GregorianChronology.getInstance()).withYear(1970).withMonthOfYear(1)
.withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0)
.withSecondOfMinute(0).withMillisOfSecond(0)
gives you the instant in which the clocks and the calendars in your country marked "00:00:00 1970-01-01". That's, in general, a different instant.
How does java.util.Date.getTime method convert a given date & time into long number?
Java API documents say that - "Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object."
Appreciate any help.
Check out the Date.java source code.
You'll see that in the simplest case, the Date object stores the number of milliseconds since 1970, rather than the date/time etc.
Actually, despite the apparently unambiguous definition in the Java API doc, it is interesting to note that the number of milliseconds reported is not the actual number of physical milliseconds, or seconds for that matter, that have elapsed since January 1st 1970 00:00:00 GMT. It is really the number of physical seconds plus the number of leap seconds that have been artificially inserted.