0

Im trying to get my program to print out the day of any given date using functions that I have to write and declare, although for the early dates of the month the program doesnt seem to print the write day. The equation in the dayOfTheWeek function, w, was given to us to calculate for the day, although for the 'floor' code to be used i had to create another 'private static' function for reasons im not quite sure of, any reason as to why would be great as well as any reason for why my program isnt returning the right day for certain dates.

here's my code, any help would be greatly appreciated :)

import java.util.Scanner;
import javax.swing.JOptionPane;

public class DayOfTheWeek {

    public static final int SEPTEMBER_APRIL_JUNE_NOVEMBER_DAYS = 30;
    public static final int REST_OF_YEAR_DAYS = 31;
    public static final int LEAP_YEAR_FEB = 29;
    public static final int NORMAL_FEB = 28;
    public static final int MONTHS = 12;

    public static void main(String[] args) {
        try 
        {
            String input = JOptionPane.showInputDialog("Enter date (day/month/year):");
            Scanner scanner = new Scanner( input );
            scanner.useDelimiter("/");
            int day = scanner.nextInt();
            int month = scanner.nextInt();
            int year = scanner.nextInt();
            scanner.close();

            String numberEnding = numberEnding (day);
            String dayEnding = day + numberEnding;
            String monthName = monthName (month);
            String dayName = dayOfTheWeek (day, month, year);

            if (validDate(day, month, year))
            {
                JOptionPane.showMessageDialog(null, dayName + " " + dayEnding + " " + monthName 
                        + " " + year + " is a valid date.");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "" + dayEnding + " " + monthName 
                        + " " + year + " is not a valid date.",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        catch (NullPointerException exception)
        {
        }
        catch (java.util.NoSuchElementException exception)
        {
            JOptionPane.showMessageDialog(null, "No number entered. \nPlease restart and try again.",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }       
    }
    public static boolean validDate( int day, int month, int year ) {
        return ((year >= 0) && (month >= 1) && (month <= MONTHS) &&
                (day >= 1) && (day <= daysInMonth( month, year )));
    }
    public static int daysInMonth( int month, int year ) {
        int monthDays;
        switch (month)
        {
        case 2:
            monthDays = isLeapYear(year) ? LEAP_YEAR_FEB : NORMAL_FEB;                                              
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            monthDays = SEPTEMBER_APRIL_JUNE_NOVEMBER_DAYS;
            break; 
        default:
            monthDays = REST_OF_YEAR_DAYS;
        }
        return monthDays;
    }
    public static boolean isLeapYear( int year ) {
        return (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0));
    }
    public static String numberEnding( int day ) {
        String dayEnding = "";
        int remainder = day%10;
        if (day >= 10 && day <= 20)
        {
            dayEnding = "th";
        }
        else
        {
            switch (remainder)
            {
            case 1:
                dayEnding = "st";
                break;
            case 2:
                dayEnding = "nd";
                break;
            case 3:
                dayEnding = "rd";
                break;
            default:
                dayEnding = "th";
                break;
            }
        }
        return dayEnding;
    }
    public static String monthName( int month ) {
        String monthName = "";
        switch (month)
        {
        case 1:
            monthName = "January";
            break;
        case 2:
            monthName = "February";
            break;
        case 3:
            monthName = "March";
            break;
        case 4:
            monthName = "April";
            break;
        case 5:
            monthName = "May";
            break;
        case 6:
            monthName = "June";
            break;
        case 7:
            monthName = "July";
            break;
        case 8:
            monthName = "August";
            break;
        case 9:
            monthName = "September";
            break;
        case 10:
            monthName = "October";
            break;
        case 11:
            monthName = "November";
            break;
        case 12:
            monthName = "December";
            break;
        default:
        }
        return monthName;
    }
    public static String dayOfTheWeek (int day, int month, int year){
        String dayName = "";
        int Y;
        if (month == 1 || month == 2)
        {
            Y = (year-1);
        }
        else
        {
            Y = (year);
        }
        int y = Y%100;
        int c = Y/100;
        int w = (day + floor(2.6 * (((month+9) % 12)+ 1) -0.2) 
                + y + floor(y/4) + floor(c/4) - (2*c));
        w = (w%7);
        if (w < 0)
        {
            w += 7;
        }
        switch (w)
        {
        case 0:
            dayName = "Sunday";
            break;
        case 1:
            dayName = "Monday";
            break;
        case 2:
            dayName = "Tuesday";
            break;
        case 3:
            dayName = "Wednesday";
            break;
        case 4:
            dayName = "Thursday";
            break;
        case 5:
            dayName = "Friday";
            break;
        case 6:
            dayName = "Saturday";
            break;
        }
        return dayName;
    }
    private static int floor(double d) {
        return 0;
    }
}
roughosing
  • 71
  • 11
  • If you catch an exception you should do something with it (at the very least, log it/inform the user). – SJuan76 Nov 22 '14 at 20:06

2 Answers2

1

I believe you need to use the Math.floor() method. Simply call this in place of your floor method:

(day + Math.floor(2.6 * (((month+9) % 12)+ 1) -0.2) 
                + y + Math.floor(y/4) + Math.floor(c/4) - (2*c));

You can also cast the equation directly using (int):

int w = (int) (day + 2.6 * ((month+9) % 12 + 1) - 0.2 + y + (y/4) + (c/4) - (2*c));

However, in your case I think that the values will be rounded improperly using casting, so you should probably use the floor method.

If you'd like some additional information on the differences between floor and casting here's a stackoverflow question that addresses it: Cast to int vs floor

Community
  • 1
  • 1
dogwin
  • 183
  • 2
  • 14
  • Im not too sure either, and yeah thats what i was wondering seeing as the return on that last function is just 0, although putting that in was the only for me to be able to get the 'w' equation to work... our lecturer gave us this equation "w = (day + floor(2.6 * (((month + 9) % 12) + 1) - 0.2) + y + floor(y/4) + floor(c/4) - 2c) mod 7" to use, and im not sure if he'd appreciate changing it too much or what? – roughosing Nov 24 '14 at 00:02
  • thanks ive done what you said and its printing more accurate dates, but for some reason some of the dates are a day behind, or ahead, for some weird reason.... – roughosing Nov 24 '14 at 00:09
  • @GregPenrose I think I know what the problem is. I believe your lecturer wanted you to use the Math.floor() method. This has different behavior from casting to an int directly. I'll edit my answer for what I believe is correct. – dogwin Nov 24 '14 at 01:08
  • awwwww... I feel like such an idiot, I did that before but it kept coming up as an error, I didnt realise that setting 'w' as an int, didnt cast the equation as an int, thanks for the help, the program works perfectly now! – roughosing Nov 24 '14 at 12:13
0

I would use the Joda-Time library.

import org.joda.time.DateTime

final DateTime date = new DateTime();
final int dayOfWeek = date.getDayOfWeek();

See the Joda-Time User Guide for more info and examples..

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Dan King
  • 1,080
  • 1
  • 11
  • 28