Over 10 Million Study Resources Now at Your Fingertips

Download as :
Rating : ⭐⭐⭐⭐⭐
Price : $10.99
Pages: 2

Although the exam pretends time zones dont exist

Working with Dates and Times 145

Old way

New way (Java 8 and later)

Adding a day

public Date addDay(Date date) {
addDay(LocalDate date) {



Subtracting a

public LocalDate


. getInstance();

return date.

return cal.getTime();

Working with Periods

Converting to a long

LocalDate and LocalDateTime have a method to convert them into long equivalents in rela-tion to 1970. What’s special about 1970? That’s what UNIX started using for date standards, so Java reused it. And don’t worry—you don’t have to memorize the names for the exam.

public static void main(String[] args) {
LocalDate start = LocalDate.of(2015, Month.JANUARY, 1);
LocalDate end = LocalDate.of(2015, Month.MARCH, 30);
Period period = Period.ofMonths(1); // create a period performAnimalEnrichment(start, end, period);
private static void performAnimalEnrichment(LocalDate start, LocalDate end, Period period) { // uses the generic period
LocalDate upTo = start;
while (upTo.isBefore(end)) {
System.out.println("give new toy: " + upTo);
upTo = upTo.plus(period); // adds the period

The method can add an arbitrary period of time that gets passed in. This allows us to reuse the same method for different periods of time as our zookeeper changes her mind.

How It Works
Login account
Login Your Account
Add to cart
Add to Cart
Make payment
Document download
Download File
PageId: ELI533441F
Uploaded by :
Page 1 Preview
although the exam pretends time zones dont exist
Sell Your Old Documents & Earn Wallet Balance