How do I use plus and minus method in the Java Date-Time API?

In the Java Date-Time API, the plus and minus methods can be used to calculate and modify dates, times, date/times, and durations.

Each temporal class (LocalDate, LocalTime, LocalDateTime, and Duration) includes these methods.

Here’s a basic example using the LocalDate class:

package org.kodejava.datetime;

import java.time.LocalDate;

public class PlusMinusExample {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();

        // Calculate the date 5 days into the future
        LocalDate futureDate = date.plusDays(5);
        System.out.println("Date five days in the future: " + futureDate);

        // Calculate the date 5 days in the past.
        LocalDate pastDate = date.minusDays(5);
        System.out.println("Date five days in the past: " + pastDate);
    }
}

Output:

Date five days in the future: 2024-01-22
Date five days in the past: 2024-01-12

You can also use plusWeeks, plusMonths, plusYears, minusWeeks, minusMonths, minusYears methods in a similar manner to add or subtract the respective time period.

Note: All the datetime manipulation methods return a new instance of the date/time object; they do not modify the original object because the classes are immutable.

The plus() and minus() methods in the Java Date-Time API offer finer control over date-time arithmetic by allowing you to add or subtract different types of date-time units such as days, months, or years.

The plus() method is used to add specific time units to a date or time, while the minus() method is used to subtract specific time units.

Here’s an example:

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.Period;

public class PlusMinusOtherExample {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        // 1 year, 2 months, and 3 days.
        Period periodToAdd = Period.of(1, 2, 3);
        LocalDate futureDate = today.plus(periodToAdd);
        System.out.println("Date after adding a period: " + futureDate);

        // 2 years, 4 months, and 6 days.
        Period periodToSubtract = Period.of(2, 4, 6);
        LocalDate pastDate = today.minus(periodToSubtract);
        System.out.println("Date after subtracting a period: " + pastDate);
    }
}

The Period class is part of the Java Date-Time API and is used to represent a quantity of time in terms of years, months, and days.

Remember, you can create a Period using the Period.of(int years, int months, int days) method, where years, months, and days are the specific units of time to be represented.

Here are examples using LocalTime and LocalDateTime:

package org.kodejava.datetime;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class PlusMinusTime {
    public static void main(String[] args) {
        // Creating a LocalTime object and adding/subtracting hours, minutes, seconds
        LocalTime time = LocalTime.now();

        LocalTime futureTime = time.plus(2, ChronoUnit.HOURS);
        System.out.println("Time after two hours: " + futureTime);

        LocalTime pastTime = time.minus(30, ChronoUnit.MINUTES);
        System.out.println("Time 30 minutes ago: " + pastTime);

        // Creating a LocalDateTime object and adding/subtracting days, months, years
        LocalDateTime dateTime = LocalDateTime.now();

        LocalDateTime futureDateTime = dateTime.plus(1, ChronoUnit.YEARS);
        System.out.println("Date and Time one year into the future: " + futureDateTime);

        LocalDateTime pastDateTime = dateTime.minus(2, ChronoUnit.MONTHS);
        System.out.println("Date and Time two months ago: " + pastDateTime);

        // You can also use plus or minus Days, Weeks, Months, Years directly
        LocalDateTime exactDateTimeFuture = dateTime.plusDays(1).plusWeeks(1).plusMonths(1).plusYears(1);
        System.out.println("Date and Time after one day, week, month, and year: " + exactDateTimeFuture);
    }
}

Note that when we are adding/subtracting units like hours, minutes, and seconds, we use java.time.temporal.ChronoUnit. When adding/subtracting days, weeks, months, and years, we use directly plusDays or minusDays and so on.

How do I use atDate() method of Java Date-Time API?

The atDate() method is a part of LocalTime class in the Java Date-Time API. This method combines this time with a date to create an instance of LocalDateTime.

Here’s an example:

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class AtDateExample {
    public static void main(String[] args) {
        // Create a LocalTime instance
        LocalTime time = LocalTime.of(14, 20);

        // Create a LocalDate instance
        LocalDate date = LocalDate.of(2023, 1, 23);

        // Using atDate to combine time and date into a LocalDateTime
        LocalDateTime dateTime = time.atDate(date);

        System.out.println(dateTime);
    }
}

Output:

2023-01-23T14:20

In this example, a LocalTime and a LocalDate are combined into a LocalDateTime using the atDate() method. This method is useful when you have a LocalTime instance and want to combine it with a date. It’s in some sense a converse operation to LocalDate‘s atTime().

How do I use atTime() method of Java Date-Time API?

The atTime() method belongs to the LocalDate class in the Java Date-Time API, not the Date class. This method combines this date with a time to create a LocalDateTime.

Here’s an example:

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class AtTimeExample {
    public static void main(String[] args) {
        // Create a LocalDate instance
        LocalDate date = LocalDate.of(2023, 1, 23);

        // Create a LocalTime instance
        // 24-hour clocks
        LocalTime time = LocalTime.of(13, 45);

        // Use atTime() to combine date and time into a LocalDateTime
        LocalDateTime dateTime = date.atTime(time);

        System.out.println(dateTime);
    }
}

Output:

2023-01-23T13:45

In this example, a LocalDate and a LocalTime are combined into a LocalDateTime using the atTime() method.

The LocalDate class also has an overloaded atTime method that takes the hour and minute directly, instead of a LocalTime instance.

Here’s an example where we set 14 hours and 30 minutes directly.

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class AtTimeOtherExample {
    public static void main(String[] args) {
        // Create a LocalDate instance
        LocalDate date = LocalDate.of(2023, 1, 23);

        // Use atTime() to combine date and hour and minute into a
        // LocalDateTime
        LocalDateTime dateTime = date.atTime(14, 30);

        System.out.println(dateTime);
    }
}

Output:

2023-01-23T14:30

In this example, 14:30 (in 24-hour clock) is directly passed into atTime. There is another version of atTime() that takes hrs, min, and sec. That would look like this:

// include seconds
LocalDateTime dateTime = date.atTime(14, 30, 20);
// include nano seconds
LocalDateTime dateTime = date.atTime(14, 30, 20, 200); 

These are all the overloaded versions of atTime() in LocalDate.

Remember, LocalDate, LocalTime, LocalDateTime and others from Java Date-Time API are designed to replace the old Date and Calendar classes from java.util package. They are more consistent, easier to understand and use.

How do I use map() method of Optional object?

The map method of the Optional class in Java is used to transform the value contained in the Optional. map allows you to apply a function on the value inside the Optional and returns an Optional that contains the result of the function.

Here is an example of how to use it:

package org.kodejava.util;

import java.util.Optional;

public class OptionalMapExample {
    public static void main(String[] args) {

        // Create an Optional<String>
        Optional<String> optional = Optional.of("Hello");

        // Use map method to transform the contained value
        Optional<Integer> transformedOptional = optional.map(String::length);

        // Use ifPresent to print the result if the Optional is not empty
        transformedOptional.ifPresent(System.out::println);
    }
}

In this example, we start with an Optional<String> that contains the string “Hello”. We then use map to apply the String::length method on the contained string. This transforms the Optional<String> into an Optional<Integer>, where the integer is the length of the string.

Lastly, we use ifPresent to print the result. In this case, the integer 5 will be printed.

Here is another example, where map helps us to handle null values:

Optional<String> optional = Optional.ofNullable(null);

// If optional is not present, it will print "0"
System.out.println(optional.map(String::length).orElse(0));

In this case, trying to apply String::length on a null value would result in a NullPointerException. However, using map in combination with Optional, allows us to safely transform the value and even provide a default result (“0” in this case) if the Optional is empty. This makes handling null values more reliable and your code less error-prone.

How do I use flatMap() method of Optional object?

The flatMap method is a special method in the Optional class in Java, if a method returns an Optional, you can use flatMap to avoid nested Optional<Optional<T>> situations.

Here is an example:

package org.kodejava.util;

import java.util.Optional;

public class OptionalFlatMap {
    public static void main(String[] args) {
        Optional<String> nonEmptyGender = Optional.of("male");
        Optional<String> emptyGender = Optional.empty();

        System.out.println("Non-Empty Optional:: " + nonEmptyGender.flatMap(OptionalFlatMap::getGender));
        System.out.println("Empty Optional:: " + emptyGender.flatMap(OptionalFlatMap::getGender));
    }

    static Optional<String> getGender(String gender) {
        if (gender.equals("male")) {
            return Optional.of("Gender is male");
        } else if (gender.equals("female")) {
            return Optional.of("Gender is female");
        } else {
            return Optional.empty();
        }
    }
}

In this example, two Optional<String> objects are created: one with a value (nonEmptyGender) and one without a value (emptyGender).

The flatMap method is used to apply the method getGender to the value of each Optional<String> (if it exists). Since getGender returns an Optional<String>, using flatMap avoids creating Optional<Optional<String>> objects, and instead directly returns an Optional<String>, that we can easily consume.

The getGender method returns an Optional object, that describes the gender if it is “male” or “female”, or an empty Optional if the gender is neither “male” nor “female”.

The result of calling flatMap will hence be an Optional<String> describing the gender if the gender is “male” or “female”, or an empty Optional in all other cases. This applies to both the non-empty and the empty Optional<String> in the example.

The final output will be:

Non-Empty Optional:: Optional[Gender is male]
Empty Optional:: Optional.empty

In both cases, note that flatMap directly returns the result of getGender, which itself is an Optional. This is different from if map was used, which would have resulted in a nested Optional.