Не приймає
package com.javarush.task.task40.task4008;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.WeekFields;
import java.util.Locale;
/*
Работа с Java 8 DateTime API
*/
public class Solution {
public static void main(String[] args) {
printDate("9.10.2017 5:56:45");
System.out.println();
printDate("21.4.2014 15:56:45");
System.out.println();
printDate("21.4.2014");
System.out.println();
printDate("17:33:40");
}
public static void printDate(String date) {
DateTimeFormatter formatDT = DateTimeFormatter.ofPattern("d.M.y H:m:s");
DateTimeFormatter formatD = DateTimeFormatter.ofPattern("d.M.y");
DateTimeFormatter formatT = DateTimeFormatter.ofPattern("H:m:s");
try {
LocalDateTime localDateTime = LocalDateTime.parse(date, formatDT);
LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();
printOnlyDate(localDate);
printOnlyTime(localTime);
} catch (DateTimeParseException e) {
try {
LocalDate localDate = LocalDate.parse(date, formatD);
printOnlyDate(localDate);
} catch (DateTimeParseException q) {
try {
LocalTime localTime = LocalTime.parse(date, formatT);
printOnlyTime(localTime);
} catch (DateTimeParseException w) {
System.out.println("Wrong date");
}
}
}
}
public static void printOnlyDate(LocalDate localDate) {
System.out.println("День: " + localDate.getDayOfMonth());
System.out.println("День недели: " + localDate.getDayOfWeek().getValue());
System.out.println("День месяца: " + localDate.getDayOfMonth());
System.out.println("День года: " + localDate.getDayOfYear());
System.out.println("Неделя месяца: " + localDate.get(WeekFields.of(Locale.getDefault()).weekOfMonth()));
System.out.println("Неделя года: " + localDate.get(WeekFields.of(Locale.getDefault()).weekOfYear()));
System.out.println("Месяц: " + localDate.getMonthValue());
System.out.println("Год: " + localDate.getYear());
}
public static void printOnlyTime(LocalTime localTime) {
System.out.println("AM или PM: " + (localTime.get(ChronoField.AMPM_OF_DAY) == 0 ? "AM" : "PM"));
System.out.println("Часы: " + localTime.getHour() % 12);
System.out.println("Часы дня: " + localTime.getHour());
System.out.println("Минуты: " + localTime.getMinute());
System.out.println("Секунды: " + localTime.getSecond());
}
}