public class Solution {
public static void main(String[] args) throws ParseException {
System.out.println(isDateOdd("November 9 2020"));
}
public static boolean isDateOdd(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd yyyy");
Date introducedDate = sdf.parse(date);
//i have obtained date format from the string introduced by user
Calendar calendar = new GregorianCalendar(2020, Calendar.JANUARY, 0);
//i have set the date from the beginning of the year but under calendar format
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(introducedDate);
//i have converted date introduced by user from date to calendar
long difference = ChronoUnit.DAYS.between(calendar.toInstant(), calendar2.toInstant());
//this is the difference between the two calendars in days.
//System.out.println(difference);
return (difference % 2 == 0) ? false : true;
}
}
package com.codegym.task.task08.task0827;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/*
Working with dates
*/
public class Solution {
public static void main(String[] args) throws ParseException {
System.out.println(isDateOdd("November 9 2020"));
}
public static boolean isDateOdd(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd yyyy");
Date introducedDate = sdf.parse(date);
//am obtinut date din string-ul introdus de utilizator
Calendar calendar = new GregorianCalendar(2020, Calendar.JANUARY, 0);
//am setat data de la inceputul anului dar sub forma de calendar
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(introducedDate);
//am trecut din date in calendar
//cum fac diferenta dintre cele doua calendare
long difference = ChronoUnit.DAYS.between(calendar.toInstant(), calendar2.toInstant());
//System.out.println(difference);
return (difference % 2 == 0) ? false : true;
}
}