Learn Java Easy. Chapter 10 Exercises

Exercises Topic 10 – Date and Time in Java

Exercises Topic 10

Exercise 1. Get the current date

Solution:

import java.time.LocalDate; 

public class Main {

       public static void main(String[] args) {

             LocalDate currentDate = LocalDate.now(); 
             System.out.println("The current date is: " + currentDate);

       }

}

Exercise 2. Get the current time

Solution:

import java.time.LocalTime; 

public class Main {

          public static void main(String[] args) { 

                  LocalTime currentTime = LocalTime.now();

                  System.out.println("The current time is: " + currentTime);

         }

}

Exercise 3. Get the current date and time

Solution:

import java.time.LocalDateTime; 

public class Main {

       public static void main(String[] args) {

               LocalDateTime currentDateTime = LocalDateTime.now();      
               System.out.println("The current date and time is: " + currentDateTime);

        }

}

Exercise 4. Create a specific date

Solution:

import java.time.LocalDate; 

public class Main {

      public static void main(String[] args) { 

               LocalDate date = LocalDate.of(2024, 5, 29);

               System.out.println("Specific date: " + date);

       }

}

Exercise 5. Create a specific time

Solution:

import java.time.LocalTime; 

public class Main {

      public static void main(String[] args) { 
 
             LocalTime time = LocalTime.of(14, 30, 45);

             System.out.println("Specific time: " + time);

      }

}

Exercise 6. Add days to a date

Solution:

import java.time.LocalDate; 

public class Main {

       public static void main(String[] args) { 

            LocalDate currentDate = LocalDate.now();

            LocalDate newDate = currentDate.plusDays(10); 

            System.out.println("Current date: " + currentDate); 

            System.out.println("Date after 10 days: " + newDate);

     }

}

Exercise 7. Subtract hours from a time

Solution:

import java.time.LocalTime; 

public class Main {

      public static void main(String[] args) { 

           LocalTime currentTime = LocalTime.now();

           LocalTime newTime = currentTime.minusHours(3); 

           System.out.println("Current time: " + currentTime); 

           System.out.println("Time after subtracting 3 hours: " + newTime);

     }

}

Exercise 8. Compare two dates

Solution:

import java.time.LocalDate; 

public class Main {

        public static void main(String[] args) { 

               LocalDate date1 = LocalDate.of(2024, 5, 29);

               LocalDate date2 = LocalDate.of(2023, 12, 25);

               if (date1.isAfter(date2)) {

                      System.out.println(date1 + " is after " + date2);

               } else if (date1.isBefore(date2)) { 

                      System.out.println(date1 + " is before " + date2);

               } else {

                      System.out.println(date1 + " is equal to " + date2);

               }

        }

}

Exercise 9. Format a date

Solution:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) {

        LocalDate date = LocalDate.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); 
        String formattedDate = date.format(formatter);

        System.out.println("Formatted date: " + formattedDate);

    }

}

Exercise 10. Get the difference between two dates

Solution:

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

public class Main {

      public static void main(String[] args) {

      LocalDate startDate = LocalDate.of(2023, 5, 1);

      LocalDate endDate = LocalDate.of(2024, 5, 29);

      Period difference = Period.between(startDate, endDate); 

      System.out.println("Difference between dates: " +
      difference.getYears() + " years, " + difference.getMonths() + " months, " + difference.getDays() + " days.");

      }

}

Did you like it? Don’t keep it to yourself — share it like juicy gossip! 😏