JetBrains Academy: LocalDate - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Convert numbers to days:
Write a program that reads a year and three days of this year (by their numbers) from the standard input and output all dates corresponding to these numbers in the same order.
import java.util.*;
import java.time.*;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = Integer.parseInt(scanner.nextLine());
int day1 = Integer.parseInt(scanner.nextLine());
int day2 = Integer.parseInt(scanner.nextLine());
int day3 = Integer.parseInt(scanner.nextLine());
LocalDate date1 = convertNumberToDate(year, day1);
LocalDate date2 = convertNumberToDate(year, day2);
LocalDate date3 = convertNumberToDate(year, day3);
System.out.println(date1);
System.out.println(date2);
System.out.println(date3);
}
public static LocalDate convertNumberToDate(int year, int day) {
return LocalDate.ofYearDay(year, day);
}
}
10 days before:
Write a program that reads a date from the standard input and prints a date that is 10 days before.
import java.util.*;
import java.time.*;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LocalDate date = LocalDate.parse(scanner.nextLine());
System.out.println(earlier10Days(date));
}
public static LocalDate earlier10Days(LocalDate date) {
return date.minusDays(10);
}
}
The first day of a month or not:
We are used to talking about days in relation to their position in a month, for example, February 22. But as we've already mentioned, there's another way to address a day: to give an order number to each day in a year. Then we could say, for example, "53rd day of the year".
Write a program that reads a year and the order number of a day, and checks if this day is the first day of a month or not.
The program must output either "true" or "false".
import java.time.*;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] data = scanner.nextLine().split("\\s+");
final int year = Integer.parseInt(data[0]);
final int dayOfYear = Integer.parseInt(data[1]);
LocalDate date = LocalDate.ofYearDay(year, dayOfYear);
System.out.println(isFirstDayOfTheMonth(date));
}
public static boolean isFirstDayOfTheMonth(LocalDate date) {
return date.getDayOfMonth() == 1;
}
}
30 years before and after:
Write a program that reads a date from the standard input and prints two dates: 30 years before and after compared to the given date.
import java.time.*;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LocalDate date = LocalDate.parse(scanner.nextLine());
System.out.println(get30YearsBefore(date));
System.out.println(get30YearsAfter(date));
}
public static LocalDate get30YearsBefore(LocalDate date) {
return date.minusYears(30);
}
public static LocalDate get30YearsAfter(LocalDate date) {
return date.plusYears(30);
}
}
2 weeks after:
Write a program that reads a date from the standard input and prints a date that is 2 weeks after.
import java.util.Scanner;
import java.time.LocalDate;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
LocalDate date = LocalDate.parse(line);
LocalDate twoWeeksAfter = twoWeeksAfter(date);
System.out.println(twoWeeksAfter);
}
public static LocalDate twoWeeksAfter(LocalDate date) {
return date.plusWeeks(2);
}
}
Getting information on a day:
Write a program that reads a date from the standard input and output the following information on it:
1. number of the day in the year;
2. the number of the day in the month.
import java.util.Scanner;
import java.time.LocalDate;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
LocalDate date = LocalDate.parse(line);
String dayInfo = getDayInfo(date);
System.out.println(dayInfo);
}
public static String getDayInfo(LocalDate date) {
int dayOfYear = date.getDayOfYear();
int dayOfMonth = date.getDayOfMonth();
return dayOfYear + " " + dayOfMonth;
}
}
The first and the last day of a month:
Write a program that reads a year and a month and outputs the first and the last day of this month.
import java.util.Scanner;
import java.time.LocalDate;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = Integer.parseInt(scanner.nextLine());
int month = Integer.parseInt(scanner.nextLine());
LocalDate[] borderDays = getFirstAndLastDayOfTheMonth(year, month);
System.out.println(borderDays[0] + " " + borderDays[1]);
}
public static LocalDate[] getFirstAndLastDayOfTheMonth(int year, int month) {
LocalDate firstDay = LocalDate.of(year, month, 1);
LocalDate lastDay = firstDay.plusMonths(1).minusDays(1);
return new LocalDate[]{firstDay, lastDay};
}
}
The last day of a month:
We normally speak of a day by addressing a month, for example, August 30. Another way to indicate a day is to give an order number to each day in a year. Then we could say, for example, "242nd day of the year".
Write a program that reads a year and the number of a day in this year, and checks if the day is the last day of a month or not.
The program must output either "true" or "false".
import java.util.Scanner;
import java.time.LocalDate;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] line = scanner.nextLine().split("\\s+");
int year = Integer.parseInt(line[0]);
int day = Integer.parseInt(line[1]);
System.out.println(isLastDayOfTheMonth(year, day));
}
public static boolean isLastDayOfTheMonth(int year, int day) {
LocalDate date = LocalDate.ofYearDay(year, day);
return date.plusDays(1).getDayOfMonth() == 1;
}
}
The n-th day from the end of a month:
Write a program that prints the n-th day from the end of a month.
The program must read a year, a month and an offset from the end of the month and output the date.
import java.util.Scanner;
import java.time.LocalDate;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] line = scanner.nextLine().split("\\s+");
int year = Integer.parseInt(line[0]);
int month = Integer.parseInt(line[1]);
int shift = Integer.parseInt(line[2]);
LocalDate shiftedDate = getShiftedDateFromTheEndOfTheMonth(year, month, shift);
System.out.println(shiftedDate);
}
public static LocalDate getShiftedDateFromTheEndOfTheMonth(int year, int month, int shift) {
LocalDate date = LocalDate.of(year, month, 1);
return date.plusMonths(1).minusDays(shift);
}
}
Print dates of a year with an offset:
Write a program that prints all dates of the given year with a specified offset applied.
It should read a starting date and a value of an offset (in days).
In the output, dates should be printed in ascending order with the starting date included. Do not output the dates from the next year.
import java.util.*;
import java.time.LocalDate;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LocalDate date = LocalDate.parse(scanner.nextLine());
int offset = Integer.parseInt(scanner.nextLine());
List<LocalDate> offsetDates = getOffsetDates(date, offset);
offsetDates.forEach(System.out::println);
}
public static List<LocalDate> getOffsetDates(LocalDate date, int offset) {
List<LocalDate> dates = new ArrayList<>();
LocalDate current = date;
while (current.getYear() == date.getYear()) {
dates.add(current);
current = current.plusDays(offset);
}
return dates;
}
}
Find all mondays:
Write a program that reads a year and the number of a month (1-12) and prints dates of all Mondays of this month in the correct order (from the first to the last).
import java.util.*;
import java.time.LocalDate;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] line = scanner.nextLine().split("\\s+");
int year = Integer.parseInt(line[0]);
int month = Integer.parseInt(line[1]);
int day = 1;
LocalDate date = LocalDate.of(year, month, day);
List<LocalDate> mondays = findMondays(date);
mondays.forEach(System.out::println);
}
public static List<LocalDate> findMondays(LocalDate month) {
List<LocalDate> mondays = new ArrayList<>();
LocalDate current = month;
while (current.getMonth() == month.getMonth()) {
if ("MONDAY".equals(current.getDayOfWeek().toString())) {
mondays.add(current);
current = current.plusWeeks(1);
continue;
}
current = current.plusDays(1);
}
return mondays;
}
}