JetBrains Academy: LocalDateTime - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: LocalDateTime

Add 11 hours and print the date:

Write a program that reads date-time, adds 11 hours to it and then prints out the resulting date.

The input date-time format should be, for example, 2017-12-31T22:30. The resulting date then must be 2018-01-01.

import java.util.Scanner;
import java.time.LocalDateTime;
import java.time.LocalDate;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LocalDateTime dateTime = LocalDateTime.parse(scanner.nextLine());
        LocalDate date = dateTime.plusHours(11).toLocalDate();
        
        System.out.println(date);
    }
}

Merging date-time instances:

Implement a method that takes two instances of LocalDateTime class and merges them into one object by choosing the largest value of each component for the target object. Consider the following components: years, months, days of months, hours, minutes and seconds.

Output the resulting LocalDateTime object.

import java.time.LocalDateTime;
import java.util.Scanner;

public class Main {

    public static LocalDateTime merge(LocalDateTime dateTime1, LocalDateTime dateTime2) {
        // compare year
        int year1 = dateTime1.getYear();
        int year2 = dateTime2.getYear();
        int year = Math.max(year1, year2);
        // compare month
        int m1 = dateTime1.getMonthValue();
        int m2 = dateTime2.getMonthValue();
        int month = Math.max(m1, m2);
        // compare day
        int d1 = dateTime1.getDayOfMonth();
        int d2 = dateTime2.getDayOfMonth();
        int day = Math.max(d1, d2);
        // compare hours
        int h1 = dateTime1.getHour();
        int h2 = dateTime2.getHour();
        int hour = Math.max(h1, h2);
        // compare minutes
        int min1 = dateTime1.getMinute();
        int min2 = dateTime2.getMinute();
        int minute = Math.max(min1, min2);
        // compare seconds
        int s1 = dateTime1.getSecond();
        int s2 = dateTime2.getSecond();
        int sec = Math.max(s1, s2);
        // compare milliseconds
        int millis1 = dateTime1.getNano();
        int millis2 = dateTime2.getNano();
        int millis = Math.max(millis1, millis2);

        return LocalDateTime.of(year, month, day, hour, minute, sec, millis);
    }

    /* Do not change code below */
    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        final LocalDateTime firstDateTime = LocalDateTime.parse(scanner.nextLine());
        final LocalDateTime secondDateTime = LocalDateTime.parse(scanner.nextLine());
        System.out.println(merge(firstDateTime, secondDateTime));
    }
}

The passed hours since the beginning of year:

Write a program that reads a date-time pair and calculates how many hours have passed since the beginning of the year (1st January, 00:00).

Input data format The first line contains a date-time pair in the format year-month-dayThour:minute:second.

Output data format The line containing an integer number.

import java.util.Scanner;
import java.time.LocalDateTime;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LocalDateTime dateTime = LocalDateTime.parse(scanner.nextLine());
        
        System.out.println(countHours(dateTime));
    }
    
    public static int countHours(LocalDateTime dateTime) {
        int day = dateTime.getDayOfYear() - 1;
        int h = dateTime.getHour();
        return h + 24 * day;
    }
}

Add days and subtract hours:

Write a program that changes the given point of time: adds a certain number of days and subtracts a few hours.

Input and output date-time like this 2017-12-31T22:30.

Input data format The single line containing date-time and two numbers: days to add and hours to subtract. Input elements are separated by spaces.

Output data format The output must contain only a date-time in the specified format.

import java.util.Scanner;
import java.time.LocalDateTime;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] inputLine = scanner.nextLine().split("\\s+");
        LocalDateTime dateTimeInput = LocalDateTime.parse(inputLine[0]);
        int daysPlus = Integer.parseInt(inputLine[1]);
        int hoursMinus = Integer.parseInt(inputLine[2]);
        
        LocalDateTime dateTime = modifyDate(dateTimeInput, daysPlus, hoursMinus);
        System.out.println(dateTime);
    }
    
    public static LocalDateTime modifyDate(LocalDateTime dateTime, int daysToAdd, int hoursToSubtract) {
        return dateTime.plusDays(daysToAdd).minusHours(hoursToSubtract);
    }
}

Adding N minutes:

Write a program that adds N minutes to a given date and time and prints out the resulting year, day of the year, hours and minutes.

The input date-time should look like 2017-12-31T22:30:15, the result date must be similar to 2018 139 19:50:15 (year, day of the year, hours, minutes, seconds).

If the result has no seconds, the program must not print them.

Input data format Two lines: the first one containing date-time, the second one containing a number of minutes to add.

Output data format The single line must contain a string with the result.

import java.util.Scanner;
import java.time.*;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LocalDateTime dateTime = LocalDateTime.parse(scanner.nextLine());
        int minutes = Integer.parseInt(scanner.nextLine());
        
        addMinutesAndPrint(dateTime, minutes);
    }
    
    public static void addMinutesAndPrint(LocalDateTime dateTime, int minutes) {
        LocalDateTime dt = dateTime.plusMinutes(minutes);
        LocalTime time = dt.toLocalTime();
        int year = dt.getYear();
        int day = dt.getDayOfYear();
        
        System.out.printf("%d %d %s", year, day, time);
    }
}

Subtracting hours and adding minutes:

Write a program that subtracts N hours and adds M minutes to a date-time pair.

Input data format The first line contains a date-time pair (year-month-dayThours:minutes). The second line contains two numbers separated by a space: hours to subtract and minutes to add.

Output data format A single line with a date-time pair (year-month-dayThours:minutes).

import java.util.Scanner;
import java.time.LocalDateTime;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LocalDateTime dateTime = LocalDateTime.parse(scanner.nextLine());
        String[] inputs = scanner.nextLine().split("\\s+");
        
        int hours = Integer.parseInt(inputs[0]);
        int minutes = Integer.parseInt(inputs[1]);
        
        System.out.println(dateTime.minusHours(hours).plusMinutes(minutes));
    }
}

Whole hours between two date-time pairs:

Write a program that calculates how many whole hours are between the two date-time pairs of the same year.

Input data format Two lines containing a date-time in a year-month-dayThour:minute format (without seconds and nanoseconds).

Output data format The line containing an integer non-negative number.

import java.util.Scanner;
import java.time.LocalDateTime;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LocalDateTime dt1 = LocalDateTime.parse(scanner.nextLine());
        LocalDateTime dt2 = LocalDateTime.parse(scanner.nextLine());
        
        System.out.println(countHoursBetween(dt1, dt2));
    }
    
    public static int countHoursBetween(LocalDateTime dt1, LocalDateTime dt2) {        
        int minutesOfYear1 = dt1.getDayOfYear() * 24 * 60; // days * hours per day * minutes in an hour
        int minutesOfYear2 = dt2.getDayOfYear() * 24 * 60;
        
        minutesOfYear1 += dt1.getHour() * 60;
        minutesOfYear1 += dt1.getMinute();
        
        minutesOfYear2 += dt2.getHour() * 60;
        minutesOfYear2 += dt2.getMinute();
        
        int minutesDifference = Math.abs(minutesOfYear1 - minutesOfYear2);
        return minutesDifference / 60;
    }
}