Language:EN
Pages: 2
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Page 1 Preview
although the exam pretends time zones dont exist

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) {

cal.setTime(date);

plusDays(1);

Subtracting a

public LocalDate

day

. 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.

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment
Both online and downloadable
No strings attached
How It Works
Login account
Login Your Account
Place in cart
Add to Cart
send in the money
Make payment
Document download
Download File
img

Uploaded by : Robert Morgan

PageId: ELI533441F