The rationale behind Instant.getEpochSecond vs. Instant.toEpochMilli

Can someone explain the rationale behind naming Instant.getEpochSecond vs Instant.toEpochMilli ? The only reason I can think of is the internal representation of the moment in seconds relative to epochs and nano seconds relative to this second, while milliseconds are calculated from these two values.

But why would you allow such an implementation detail to break into the new API?

+7
java java-time
source share
3 answers

I should have guessed, but I agree with you:

  • getEpochSecond() points to a simple getter, i.e. it just returns the value that is stored in this instance
  • toEpochMilli() , on the other hand, would calculate its return value, just as toString() would not return the stored string, but would be typed each time the method is called

This convention is actually described here: http://docs.oracle.com/javase/tutorial/datetime/overview/naming.html

The reason for this convention is probably related to the JavaBeans specification, i.e. epochSecond will be the (readonly) Instant property, while epochMilli is a different view, but not a property.

+7
source share

Recipients return portions of a Temporal object (e.g. Instant ), and to* methods return a converted version:

  • getEpochSecond() returns the second part of Instant
  • getNanos() returns the nanosecond of the second part of Instant
  • toEpochMilli() returns Instant converted to milliseconds (i.e. seconds * 1000 + nanoseconds / 1,000,000 )
+3
source share

Convenience? EpochSeconds for compatibility with UNIX compatibility, EpochMilli for java Date compatibility, from the top of the head.

Edit: Oh, I didn't even understand the question, so one of them is β€œget” and the other is β€œto”: perhaps to remind you that the second is doing the conversion and may fail.

0
source share

All Articles