funny in first date with time resul for day of the week is "" and for second date Week of the month: b, maybe it's small error but my taks don't work
package com.codegym.task.task40.task4008;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.util.Calendar;
/*
Working with Java 8's DateTime API
*/
public class Solution {
public static void main(String[] args) throws ParseException {
printDate("9.10.2017 5:56:45");
System.out.println();
printDate("21.4.2014");
System.out.println();
printDate("17:33:40");
}
public static void printDate(String date) {
switch (getFormatDate(date)){
case ("date time"):
String[] split = date.split(" ");
getDateResult(split[0]);
getTimeResult(split[1]);
break;
case ("date"):
getDateResult(date);
break;
case ("time"):
getTimeResult(date);
break;
default:
System.out.println("Invalid date time data ");
}
}
private static String getFormatDate(String date){
if (date.contains(".") && date.contains(":")) return "date time";
if (date.contains(".")) return "date";
if (date.contains(":")) return "time";
return "INVALID";
}
private static void getDateResult(String s) throws DateTimeParseException {
LocalDate date = LocalDate.parse(s, DateTimeFormatter.ofPattern("d.M.yyyy"));
System.out.println("Day: " + date.getDayOfMonth());
System.out.println("Day of the week: " + date.getDayOfWeek().getValue());
System.out.println("Day of the month: " + date.getDayOfMonth());
System.out.println("Day of the year: " + date.getDayOfYear());
System.out.println("Week of the month: " + (date.get(ChronoField.ALIGNED_WEEK_OF_MONTH) + 1));
System.out.println("Week of the year: " + (date.get(ChronoField.ALIGNED_WEEK_OF_YEAR) + 1));
System.out.println("Month: " + date.getMonthValue());
System.out.println("Year: " + date.getYear());
}
private static void getTimeResult(String s) throws DateTimeParseException {
LocalTime time = LocalTime.parse(s, DateTimeFormatter.ofPattern("H:mm:ss"));
System.out.println("AM or PM: " + (time.get(ChronoField.AMPM_OF_DAY) == 0 ? "AM" : "PM"));
System.out.println("Hour: " + time.get(ChronoField.CLOCK_HOUR_OF_AMPM));
System.out.println("Hour of the day: " + time.getHour());
System.out.println("Minutes: " + time.getMinute());
System.out.println("Seconds: " + time.getSecond());
}
}