What is the problem?
package com.codegym.task.task40.task4007;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/*
Working with Java 8's DateTime API
*/
public class Solution {
public static void main(String[] args) {
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) {
//write your code here
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
date = date.trim();
String[] split = date.split("\\s+");
if (split.length == 2) {
getDate(split[0], calendar);
getTime(split[1], calendar);
} else if (date.matches("\\d+\\.\\d+\\.\\d+")) {
getDate(date, calendar);
} else if (date.matches("\\d+:\\d+:\\d+")) {
getTime(date, calendar);
}
}
private static void getDate(String date, Calendar calendar) {
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
try {
calendar.setTime(dateFormat.parse(date));
System.out.println("Day: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("Day of the week: " + (calendar.get(Calendar.DAY_OF_WEEK) - 1));
System.out.println("Day of the month: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("Day of the year: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("Week of the month: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("Week of the year: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("Month: " + (calendar.get(Calendar.MONTH) + 1));
System.out.println("Year: " + calendar.get(Calendar.YEAR));
} catch (ParseException e) {}
}
private static void getTime(String time, Calendar calendar) {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
try {
calendar.setTime(dateFormat.parse(time));
System.out.println("AM or PM: " + (calendar.get(Calendar.AM) == 0 ? "AM" : "PM"));
System.out.println("Hour: " + calendar.get(Calendar.HOUR));
System.out.println("Hour of the day: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("Minutes: " + calendar.get(Calendar.MINUTE));
System.out.println("Seconds: " + calendar.get(Calendar.SECOND));
} catch (ParseException e) {}
}
}
/* CONSOLE OUTPUT:
Day: 21
Day of the week: 1
Day of the month: 21
Day of the year: 111
Week of the month: 4
Week of the year: 17
Month: 4
Year: 2014
AM or PM: PM
Hour: 3
Hour of the day: 15
Minutes: 56
Seconds: 45
Day: 21
Day of the week: 1
Day of the month: 21
Day of the year: 111
Week of the month: 4
Week of the year: 17
Month: 4
Year: 2014
AM or PM: PM
Hour: 5
Hour of the day: 17
Minutes: 33
Seconds: 40
////////////////////////////////////////
Working with Java 8's DateTime API
Complete this task using Java 8's DateTime API.
Implement the printDate(String date) method.
It must accept a date as an argument (in one of 3 formats) and display it as follows:
1) For "21.4.2014 15:56:45", the output must be:
Day: 21
Day of the week: 1
Day of the month: 21
Day of the year: 111
Week of the month: 4
Week of the year: 17
Month: 4
Year: 2014
AM or PM: PM
Hour: 3
Hour of the day: 15
Minutes: 56
Seconds: 45
2) For "21.4.2014":
Day: 21
Day of the week: 1
Day of the month: 21
Day of the year: 111
Week of the month: 4
Week of the year: 17
Month: 4
Year: 2014
3) For "17:33:40":
AM or PM: PM
Hour: 5
Hour of the day: 17
Minutes: 33
Seconds: 40
Use the Calendar class.
Requirements:
1. If a date is passed to the printDate method in the format "date time", the output should be: Day, Day of the week, Day of the month, Day of the year, Week of the month, Week of the year, Month, Year, AM or PM, Hour, Hour of the day, Minutes, and Seconds.
2. If a date is passed to the printDate method in the format "date", the output should be: Day, Day of the week, Day of the month, Day of the year, Week of the month, Week of the year, Month, and Year.
3. If a date is passed to the printDate method in the format "time", the output should be: AM or PM, Hour, Hour of the day, Minutes, and Seconds.
4. Use the Calendar class's static getInstance method to get the calendar.
*/