Java Reference
In-Depth Information
// Using the until() method
long days2 = ld1.until(ld2, DAYS);
long hours2 = lt1.until(lt2, HOURS);
long minutes2 = lt1.until(lt2, MINUTES);
System.out.println("Using until (days): " + days2);
System.out.println("Using until (hours): " + hours2);
System.out.println("Using until (minutes): " + minutes2);
}
}
Using between (days): 131
Using between (hours): 2
Using between (minutes): 150
Using until (days): 131
Using until (hours): 2
Using until (minutes): 150
It is not always possible to compute the amount of time elapsed between two dates and times. For example,
you cannot tell the number of hours between a LocalDate and a LocalDateTime as the LocalDate does not store a
time component. If such parameters are passed to the methods, a runtime exception is thrown. The rule is that the
specified end date/time should be convertible to the start date/time.
Partials
A partial is a date, time, or datetime that does not fully specify an instant on a timeline, but still makes sense to
humans. With some more information, a partial may match multiple instants on the timeline. For example, December
25 is not a complete date that can be determined uniquely on a timeline; however, it makes sense when we talk about
Christmas. Similarly, January 1 makes sense as New Year's Day.
You must have a date, time, and time zone to identify an instant uniquely on the timeline. If you have some, but
not all, of the three pieces of information, you have a partial. You cannot obtain an Instant from a partial without
supplying some more pieces of information. I already discussed some partials in previous sections.
A LocalDate , LocalTime , LocalDateTime , and OffsetTime are examples of partials. An OffsetDateTime and
ZonedDateTime are not partials; they have information to identify an instant on the timeline uniquely.
I will discuss three more partials in this section:
Year
YearMonth
MonthDay
The names of these partials easily describe them. A Year represents a year, for example, 2012, 2013, etc. A
YearMonth represents a valid combination of a year and a month, for example, 2012-05, 2013-09, etc. A MonthDay
represents a valid combination of a month and a day of month, for example, - -12-15. Listing 12-13 shows some of the
operations you can perform on these partials.
 
Search WWH ::




Custom Search