

Adding 5 hours and 4 minutes to an Instant

Instant API provides some useful methods to allow calculations using Instants and other classes in the package, below is a first example: The second example above creates an Instant by parsing a String, this string must by a valid representation of a UTC Instant (NB: Instant is a point of time, it doesn't store any TimeZone information and as such it supports only UTC formatted strings). Instant instant = Instant.ofEpochMilli(new Date().getTime()) Here are some other ways to create Instants: Instant.toString() returns a ISO8601 formatted string (such as: 'T16:22:52.966Z'), a much more logical choice than the one used by the old Date class. Let's start with obtaining an Instant instance and print its value: Representing a point in time using nanoseconds precision requires more space than a Long can provide, therefore the internal representation is composed of two Long fields, the first holds the number of seconds since (or before) the standard Java epoch and the other the number of nanoseconds of the last seconds (so never larger than 999,999,999). An Instant represents a point in time (similar to ) with nanoseconds precision (unlike the old Date which has milliseconds precision).

Probably the best place to start with the java.time package is the Instant class. This post would be the first in a series, starting with some basic classes of the package: Instant, LocalDate, LocalTime, and LocalDateTime. As there is much to cover in this new long awaited API I think it worth few posts with some examples. I started to experience the Java 8 new APIs (still Early Access at the moment of writing this post) and the first thing I looked into was the new date and time API ( JSR 310).
