I wanted to try to solve this without using the Date object. Is there something I am forgetting to account for in this code??
package com.codegym.task.task08.task0827;
import java.util.Date;
import java.util.HashMap;
/*
Working with dates
*/
public class Solution
{
public static void main(String[] args)
{
System.out.println(isDateOdd("MAY 1 2013"));
}
public static boolean isDateOdd(String date)
{
// split the string using split and store variables in month and day
String[] splitDate;
splitDate = date.split(" ");
// for (String d : splitDate)
// {
// System.out.println(d);
// }
String month = splitDate[0];
// System.out.println(month);
Integer day = Integer.parseInt(splitDate[1]);
// System.out.println(day);
Integer year = Integer.parseInt(splitDate[2]);
// System.out.println(year);
// initialize a hashMap with months of the year == key, and consecutive days == value
HashMap<String, Integer> monthMap = new HashMap<String, Integer>();
// add months to the dictionary
monthMap.put("JANUARY", 0);
monthMap.put("FEBRUARY", 31);
monthMap.put("MARCH", 59);
monthMap.put("APRIL", 90);
monthMap.put("MAY", 120);
monthMap.put("JUNE", 151);
monthMap.put("JULY", 181);
monthMap.put("AUGUST", 212);
monthMap.put("SEPTEMBER", 243);
monthMap.put("OCTOBER", 273);
monthMap.put("NOVEMBER", 304);
monthMap.put("DECEMBER", 334);
// initialize integer of daysSince since beginning of year
// use dictionary to get number and add days variable
Integer daysSince = monthMap.get(month) + day;
// add days if the year is a leap year
if (year % 100 == 0 && year % 400 != 0)
{
// System.out.println("Number of days in the year: 365");
}
else if (month.equals("FEBRUARY") && day == 29)
{
// leap day so don't have to add extra day
}
else if (year % 4 == 0 || year % 400 == 0 && !month.equals("JANUARY") && !month.equals("FEBRUARY"))
{
// System.out.println("Number of days in the year: 366");
daysSince += 1;
}
else
{
// System.out.println("Number of days in the year: 365");
}
// use conditional and modulo to determine if value is even or odd
return daysSince % 2 != 0;
}
}
package com.codegym.task.task08.task0827;
import java.util.Date;
import java.util.HashMap;
/*
Working with dates
*/
public class Solution
{
public static void main(String[] args)
{
System.out.println(isDateOdd("MAY 1 2013"));
}
public static boolean isDateOdd(String date)
{
// split the string using split and store variables in month and day
String[] splitDate;
splitDate = date.split(" ");
// for (String d : splitDate)
// {
// System.out.println(d);
// }
String month = splitDate[0];
// System.out.println(month);
Integer day = Integer.parseInt(splitDate[1]);
// System.out.println(day);
Integer year = Integer.parseInt(splitDate[2]);
// System.out.println(year);
// initialize a hashMap with months of the year == key, and consecutive days == value
HashMap<String, Integer> monthMap = new HashMap<String, Integer>();
// add months to the dictionary
monthMap.put("JANUARY", 0);
monthMap.put("FEBRUARY", 31);
monthMap.put("MARCH", 59);
monthMap.put("APRIL", 90);
monthMap.put("MAY", 120);
monthMap.put("JUNE", 151);
monthMap.put("JULY", 181);
monthMap.put("AUGUST", 212);
monthMap.put("SEPTEMBER", 243);
monthMap.put("OCTOBER", 273);
monthMap.put("NOVEMBER", 304);
monthMap.put("DECEMBER", 334);
// initialize integer of daysSince since beginning of year
// use dictionary to get number and add days variable
Integer daysSince = monthMap.get(month) + day;
// add days if the year is a leap year
if (year % 100 == 0 && year % 400 != 0)
{
// System.out.println("Number of days in the year: 365");
}
else if (month.equals("FEBRUARY") && day == 29)
{
// leap day so don't have to add extra day
}
else if (year % 4 == 0 || year % 400 == 0 && !month.equals("JANUARY") && !month.equals("FEBRUARY"))
{
// System.out.println("Number of days in the year: 366");
daysSince += 1;
}
else
{
// System.out.println("Number of days in the year: 365");
}
// use conditional and modulo to determine if value is even or odd
return daysSince % 2 != 0;
}
}