April 21, 2014 is a Monday, so it should be the second day of the week. However, the conditions say this should be an output of 1. Are they using 0 for Sunday? They don't appear to be using 0 as a start for the months, so that would be inconsistent, if so. In any event, I did try (Calender.get(CALENDAR.DAY_OF_WEEK) - 1) here and that still doesn't work.
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;
/*
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)
{
String formatString = "";
if(date.contains("."))
{
formatString = "dd.MM.yyyy";
}
if(date.contains(":"))
{
if(formatString.length() > 0)
{
formatString += " ";
}
formatString += "H:m:s";
}
DateFormat format = new SimpleDateFormat(formatString);
Calendar calendar = Calendar.getInstance();
try
{
calendar.setTime(format.parse(date));
if(date.contains("."))
{
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));
}
if(date.contains(":"))
{
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(Exception e)
{
}
}
}