My code compiles and runs perfectly, but when I any dates that are Saturdays the output is 'null' instead of 'SATURDAY'. Examples will be provided below to further explain this problem.
I have tried to change my if statement on my "getDayOfWeek" method but I seem to have no solution, I have also tried to obtain help from an experienced coder but they seem to be struggling due to java not being their main language...
The code:
class MyDate {
// properties of date object
private int day, month, year;
// constructor with arguments
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public boolean isDateValid() {
if (month > 12 || month < 1 || day < 1) { // if values exceed 12 or are negative: return false
return false;
} else if (year <= 1582 && month <= 10 && day <= 15) { // starting date
// checking
return false;
} // for 31 day months: January, March, May, July, August, October, December
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day > 31) {
return false;
}
} // for 30 day months: April, June, September, November
else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
return false;
}
} // February check
else if (month == 2) {
// leap year check for February
// 29 days in a leap year
// 28 days in a common year
if (isLeapYear()) {
if (day > 29) {
return false;
}
} else {
if (day > 28) {
return false;
}
}
}
return true;
}
// checks if input year is leap year
private boolean isLeapYear() {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}
// method returns the day of MyDate object
public int getDay() {
return day;
}
// parameter for day to set
public void setDay(int day) {
this.day = day;
}
// method returns the month of MyDate object
public int getMonth() {
return month;
}
// parameter for month
public void setMonth(int month) {
this.month = month;
}
// method returns the year of MyDate object
public int getYear() {
return year;
}
// parameter for year
public void setYear(int year) {
this.year = year;
}
// method returns all variables: day/month/year of MyDate object
public String toString() {
return day + "/" + month + "/" + year;
}
}
public class MyCalendar {
//enums for days of week
public static enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
};
//enums for month of year
public static enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
};
//enums for week number of month
public static enum Week {
FIRST, SECOND, THIRD, FOURTH, FIFTH;
};
//to store Date object
private MyDate date;
//constructor taking mydate object
public MyCalendar(MyDate enteredDate) {
this.date = enteredDate;
}
//main method
public static void main(String[] args) {
boolean dateValid = false; //valid date false
Scanner input = new Scanner(System.in); //scanner for input
MyDate enteredDate = null;
//till valid date found
while (!dateValid) {
System.out.print("Enter the date as day month year : ");
//taking input and creating date output
enteredDate = new MyDate(input.nextInt(), input.nextInt(), input.nextInt());
//validating date input
if (enteredDate.isDateValid()) { //if valid
MyCalendar myCalendar = new MyCalendar(enteredDate);
//creating calendar table
myCalendar.printDateInfo(); //printing date info
myCalendar.printCalendar(); //printing calendar
dateValid = true; //setting validate to true
} else {
System.out.println(enteredDate + " is not a valid date, please re-input a valid date: ");
}
}
input.close();
}
// returns number of days in current month
private int getNumberOfDays() {
int days = 31;
int month = date.getMonth();
if (month == 4 || month == 6 || month == 9 || month == 11)
days = 30;
return days;
}
//print calendar of input month
public void printCalendar() {
System.out.println("\n\nThe Calendar of "+Month.values()[date.getMonth()-1]+" "+date.getYear()+" is :");
int numberOfMonthDays = getNumberOfDays();
Day firstWeekdayOfMonth = getDayOfWeek(1, date.getMonth(), date.getYear());
int weekdayIndex = 0;
System.out.println("Su Mo Tu We Th Fr Sa");
// The order of days depends on the input of date
// to display output of calendar
for (int day = 0; Day.values()[day] != firstWeekdayOfMonth; day++) {
System.out.print(" "); // this loop to print the first day in the
// correct place
weekdayIndex++;
}
for (int day = 1; day <= numberOfMonthDays; day++) {
if (day < 10)
System.out.print(day + " ");
else
System.out.print(day);
weekdayIndex++;
if (weekdayIndex == 7) {
weekdayIndex = 0;
System.out.println();
} else {
System.out.print(" ");
}
}
System.out.println();
}
//method to print about date information in literal form
public void printDateInfo() {
System.out.println(date + " is a " + getDayOfWeek(date.getDay(), date.getMonth(), date.getYear())
+ " located in the " + Week.values()[getWeekOfMonth() - 1] + " week of "
+ Month.values()[date.getMonth() - 1] + " " + date.getYear());
}
/*
* gets day of the week, returns enum type Day
*
* Zellar's congruence to calculate the day of week
* for any given date after October 15 1582
* (h) = (q+(13*(m+1)/5)+K+(K/4)+(J/4)+5J)%7 ,q- day of month,
* m- month, k = year of century (year%100), J = (year/100)
*/
public Day getDayOfWeek(int day, int month, int year) {
int q = day;
int m = month;
if (m < 3) {
m = 12 + date.getMonth();
year = year - 1;
}
int K = year % 100;
int J = year / 100;
//calculating h value
int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + 5 * J) % 7;
Day output = null;
if (h < Day.values().length && h > 0) {
output = Day.values()[h - 1]; //getting respective enum value
}
return output; //returning enum value
}
// get week number of current date
public int getWeekOfMonth() {
int days = date.getDay();
int weeks = days / 7;
days = days % 7;
if (days > 0)
weeks++;
return weeks;
}
}
Expected results:
java MyCalendar 29/02/2019
29/02/2019 in not a valid date, please re-input a valid date: 25/05/2019
25/05/2019 is a Saturday and located in the fourth week of May 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Actual results:
25/05/2019 is a null located in the FOURTH week of MAY 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Results of another date (that is not a Saturday):
24/05/2019 is a FRIDAY located in the FOURTH week of MAY 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Specified problem:
The output prints null on all dates that are on the day of Saturday, whereas other dates that are not a Saturday will achieve the correct output needed.
if (h < Day.values().length && h > 0) {
should change to
if (h < (Day.values().length + 1) && h > 0) {
If this is for production code, the comment by Basil Bourque is correct: you should not develop your own MyDate class but rely on the built in LocalDate class.
If on the other hand, as I would assume, this is a programming exercise, it’s a fine one, and there’s no reason (that I can see) why you shouldn’t struggle your way through it.
Your formula for calculating the day of the week is:
//calculating h value
int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + 5 * J) % 7;
(As an aside, please find better variable names and also respect the Java naming convention: a variable name cannot be an uppercase letter.) I don’t understand the formula, but assuming that it is correct, it calculates the day of week as 0 = Saturday, 1 = Sunday up to 6 = Friday. To use this number as a lookup into your Day enum use
output = Day.values()[(h + 6) % 7]; //getting respective enum value
Since h will always be non-negative and less than 7, you don’t need the enclosing if statement. Just assign to output unconditionally. With these changes I get
Enter the date as day month year : 25 5 2019
25/5/2019 is a SATURDAY located in the FOURTH week of MAY 2019
The Calendar of MAY 2019 is :
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Related
Can anyone suggest an example on how to handle invalid inputs, I have got the logic wrong for input from the keyboard. So, I need !myScanner.hasNextInt() or to put that loop in a method which I can reuse for ever.
package dayoftheyear;
import java.util.Scanner;
/**
*
* #author abdal
*/
public class DayOfTheYear {
/**
* Ask the user to enter a month, day, and year as integers
* separated by spaces then display the number of the days since the
* beginning of the year.
* Don’t forget about leap year.
* Sample: user inputs ‘3 1 2000’, output is ‘61’.
* #param args Unused.
*/
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String enter = "A-z";
int a=31;
int b=1;
boolean dateCheck;
int month;
int day;
int year;
do {
System.out.print("Enter a valid month day year separated by spaces");
if (s.hasNextInt()) {
month= s.nextInt();
day=s.nextInt();
year=s.nextInt();
if (month >= b && month <= a || day>=b && day<=a || year>=b) {
int numberOfDay = countDays(month, day, year);
System.out.println(+ month + "/" + day + "/" + year + " is a day number "
+ numberOfDay + " of that year");
dateCheck = true;
} else {
System.out.println("Enter a valid month day year separated by spaces");
dateCheck = false;
}
} else {
System.out.println("Not a date");
month = 0;
day=0;
year=0;
s.next();
dateCheck = false;
}
} while (!dateCheck);
/**
* Get the number of days since the start of the year.
* Declare a 12 element array and initialize it with the number of
* days in each month.
* #param month Month to count from.
* #param day Day of the month to count to.
* #param year The year to count from.
* #return Days since the start of the given year.
*/
} public static int countDays(int month, int day, int year) {
int monthLength[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int days = 0;
if (isLeapYear(year) && month > 2)
days += 1;
for (int i = 0; i < month - 1; i++)
days += monthLength[i];
return days += day;
}
/**
* Check if a year is a leap year.
* #param year Year to check.
* #return True if the year is a leap year.
*/
public static boolean isLeapYear(int year) {
if (year % 4 != 0) return false;
else if (year % 100 != 0) return true;
else return year % 400 == 0;
}
}
Replace
if (month >= b && month <= a || day>=b && day<=a || year>=b)
with
if (month >= 1 && month <= 12 && day >= 1 && day <= 31 && year >= 1)
A sample run after this change:
Enter a valid month day year separated by spaces: 13 1 2000
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 3 32 2000
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 3 1 2
3/1/2 is a day number 60 of that year
Update1: the following condition will hold good also for February
if ((month == 2 && day >= 1 && day <= 28 && year >= 1 && !isLeapYear(year))
|| (month == 2 && day >= 1 && day <= 29 && year >= 1 && isLeapYear(year))
|| (month != 2 && month >= 1 && month <= 12 && day >= 1 && day <= 31 && year >= 1))
A sample run:
Enter a valid month day year separated by spaces: 2 29 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 13 2 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 1 32 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 1 2 3
1/2/3 is a day number 2 of that year
Update2: the following code addresses all the issues raised in the comments
int monthLength[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((month == 2 && day >= 1 && day <= 28 && year >= 1 && !isLeapYear(year))
|| (month == 2 && day >= 1 && day <= 29 && year >= 1 && isLeapYear(year))
|| (month != 2 && month >= 1 && month <= 12 && day >= 1 && day <= monthLength[month-1] && year >= 1))
A sample run:
Enter a valid month day year separated by spaces: 2 29 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 13 2 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 1 32 2001
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 6 31 2020
Enter a valid month day year separated by spaces
Enter a valid month day year separated by spaces: 7 31 2020
7/31/2020 is a day number 213 of that year
Since date format is not subject to change in real world, it should be appropriate to use some hard coding. Dividing the problem into several methods is always recommended.
You could add following methods to your class:
private static boolean validDate(int month, int day, int year) {
if (year < 1) {
return false; // no B.C.
}
if (month > 1 && month < 13) {
if (month == 2) { // handle February
return validFeb(day, year);
} else if (month % 2 == 1 && month < 8
|| month % 2 == 0 && month >= 8) { // 31-day months
return valid31(day);
} else { // 30 day months
return valid30(day);
}
}
return false;
}
Validate 30-day months:
private static boolean valid30(int day) {
if (day > 1 && day < 31) {
return true;
}
return false;
}
Validate 31-day months
private static boolean valid31(int day) {
if (day > 1 && day < 32) {
return true;
}
return false;
}
Validate February
private static boolean validFeb(int day, int year) {
if (isLeapYear(year)) {
if (day > 1 && day < 30) {
return true;
}
} else {
if (day > 1 && day < 29) {
return true;
}
}
return false;
}
Then your do-while loop should look something like so:
do {
System.out.print("Enter a valid month day year separated by spaces\n");
if (s.hasNextInt()) month = s.nextInt();
else s.next();
if (s.hasNextInt()) day = s.nextInt();
else s.next();
if (s.hasNextInt()) year = s.nextInt();
else s.next();
int numberOfDaysSinceStart = 0;
if (validDate(month, day, year)) {
dateCheck = true;
numberOfDaysSinceStart = countDays(month, day, year);
System.out.println(month + "/" + day + "/" + year + " is a day number "
+ numberOfDaysSinceStart + " of that year");
} else {
dateCheck = false;
}
} while (!dateCheck);
You can add edge cases forever. There is a reason why time-related calculations are nightmare outsourced to libraries written by some poor souls that are paid to try to cover them all. Java has such built in, take a look at java.util.Calendar (Gregorian implementation).
You set it to year/month/day, and it will puke out an exception if anything is wrong while trying to get the result.
Calendar c = Calendar.getInstance();
c.set(year, month, day);
try {
c.getTime();
} catch (Exception e) {
// wrong date format
}
I think the code below can help you, comment if you have any questions.
while(true) {
try{
System.out.print("Enter a valid month day year separated by spaces");
month= s.nextInt();
day=s.nextInt();
year=s.nextInt();
if (month >= 1 && month <= 12 || day>=1 && day<=31 || year>=1) {
System.out.println(+ month + "/" + day + "/" + year + " is a day number "+ " of that year");
break;
} else {
System.out.println("Enter a valid month day year separated by spaces");
}
} catch(InputMismatchException e) {
System.out.println("Enter a valid month day year separated by spaces");
}
s.next();
}
thanks I updated my program and it is working;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;
boolean dateCheck;
do {
System.out.print("Enter a valid month day year separated by spaces\n");
if (s.hasNextInt()) month = s.nextInt();
else s.next();
if (s.hasNextInt()) day = s.nextInt();
else s.next();
if (s.hasNextInt()) year = s.nextInt();
else s.next();
int numberOfDaysSinceStart = 0;
if (month >= 1 && month <= 12 && day >= 1 && day <= 31 && year >= 1) {
dateCheck = true;
numberOfDaysSinceStart = countDays(month, day, year);
System.out.println(month + "/" + day + "/" + year + " is a day number "
+ numberOfDaysSinceStart + " of that year");
} else {
dateCheck = false;
}
} while (!dateCheck);
/**
* Get the number of days since the start of the year.
* Declare a 12 element array and initialize it with the number of
* days in each month.
* #param month Month to count from.
* #param day Day of the month to count to.
* #param year The year to count from.
* #return Days since the start of the given year.
*/
} public static int countDays(int month, int day, int year) {
int monthLength[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int days = 0;
if (isLeapYear(year) && month > 2)
days += 1;
for (int i = 0; i < month - 1; i++)
days += monthLength[i];
return days += day;
}
/**
* Check if a year is a leap year.
* #param year Year to check.
* #return True if the year is a leap year.
*/
public static boolean isLeapYear(int year) {
if (year % 4 != 0) return false;
else if (year % 100 != 0) return true;
else return year % 400 == 0;
}
}
I am trying to write a program in Java (this is a school assignment that tells you what day of the week a certain date is. (The date should be written on the form yyyy-mm-dd.) I thought I had come up with a solution with the code below, but then I found an error.
When you run the code, and type in 1999-12-31 in the dialog, the program tells you that the entered date (1999-12-31) is a Friday. But when you type in the date 2000-01-01 (which is one day after 1999-12-31), the program tells you that the day is a Sunday! What happened with Saturday? A similar problem happens when you type in 2000-02-29 and 2000-03-01, they both give Wednesday as an answer!
What I have yet noticed, this error appears only when you enter a date between 2000-01-01 and 2000-02-29. I would be very grateful if someone could please help me to find the cause of the error and to solve the problem!
import static javax.swing.JOptionPane.*;
import static java.lang.Math.*;
public class DateCalc {
// Here, between the DateCalc class declaration and the main method, several methods used in the program are
// constructed.
// The method isLeapYear tests whether the entered year is a leap year or not.
private static boolean isALeapYear(int year) {
// If the year is a multiple of 4 and not a multiple of 100, or a multiple of 400, then it is a leap year.
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
}
else {
return false;
}
}
// A method that tests whether a given string is written as a valid date.
private static boolean isAValidDate(int year, int month, int day) {
int maxValidYear = 9999;
int minValidYear = 1754;
if (year > maxValidYear || year < minValidYear) {
return false;
}
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > 31) {
return false;
}
// Handle the February Month
if (month == 2) {
if (isALeapYear(year)) {
return (day <= 29); // This statement is true if the value of the day is less than or equal to 29 if the month is February within a leap year.
// Otherwise the statement is false and the method will return the boolean value false.
}
else {
return (day <= 28); // This statement is true if the value of the day is less than or equal to 28 if the month is February within a non-leap year.
// Otherwise the statement is false and the method will return the boolean value false.
}
}
// Month of April, June, September and November must have number of days less than or equal to 30.
if (month == 4 || month == 6 || month == 9 || month == 11) {
return (day <= 30);
}
return true;
}
// A method that calculates the day number within the year.
private static int dayNumberWithinYear(int year, int month, int day) {
// An array which stores the number of days in the different months (when the year is not a leap year).
// (Index 0 is the number of days in January, index 1 is the number of days in February, etc.)
int[] monthStructure = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// If the entered year is a leap year, then the monthStructure array will be initialized with an extra day in February, i.e the leap day.
if (isALeapYear(year)) {
monthStructure[1] = 29;
}
int sumDaysInPreviousMonths = 0;
int daysInTheCurrentMonth = day;
int dayNumber = 0;
// Loops through all the months (index 0 is January, index 1 is February, etc.).
for (int i = 0; i < month - 1; i++) {
sumDaysInPreviousMonths += monthStructure[i];
}
dayNumber = sumDaysInPreviousMonths + daysInTheCurrentMonth;
return dayNumber;
}
// A method that decides the day of the week of an entered date.
private static void weekDay(int year, int month, int day) {
// The number of days that have passed since January 1, 1754, excluding the days of the entered year and
// excluding the leap days.
int sumDaysInOrdinaryYears = (year - 1754) * 365;
int sumLeapDaysInLeapYears = 0;
// Suppose the entered year is n. The for-loop goes through all the years from year n-1 to year 1754, and
// checks if the current year in the loop is a leap year. The number of leap years between year 1754 and n-1
// is equal to the number of days that will get added (beside from the days in ordinary years) to the total
// days from January 1, 1754 to the entered date.
for (; year > 1754; year -= 1) {
if (isALeapYear(year)) {
sumLeapDaysInLeapYears += 1;
}
}
// The sum of all days from year 1754 to year n-1 (if the entered year is n), is equal to the sum of days in
// the ordinary years and the leap days in the years.
int sumDaysInEveryYearExcludingTheEntered = sumDaysInOrdinaryYears + sumLeapDaysInLeapYears;
int sumDaysInTotalYears = sumDaysInEveryYearExcludingTheEntered + dayNumberWithinYear(year, month, day);
int weekDay = sumDaysInTotalYears % 7;
if (weekDay == 0) {
showMessageDialog(null, "The date is a monday.");
}
else if (weekDay == 1) {
showMessageDialog(null, "The date is a tuesday.");
}
else if (weekDay == 2) {
showMessageDialog(null, "The date is a wednesday.");
}
else if (weekDay == 3) {
showMessageDialog(null, "The date is a thursday.");
}
else if (weekDay == 4) {
showMessageDialog(null, "The date is a friday.");
}
else if (weekDay == 5) {
showMessageDialog(null, "The date is a saturday.");
}
// If weekDay == 6
else {
showMessageDialog(null, "The date is a sunday.");
}
}
public static void main(String[] args) {
// This is step 3 in the laboratory instruction.
while (true) {
String date = showInputDialog("Please, enter a date on the form yyyy-mm-dd");
// If the user clicks 'Cancel' or clicks 'OK' when the dialog box is empty, the program will exit.
if (date == null || date.length() == 0) {
break;
}
int y = Integer.parseInt(date.substring(0,4));
int m = Integer.parseInt(date.substring(5,7));
int d = Integer.parseInt(date.substring(8));
if (!isAValidDate(y, m, d)) {
showMessageDialog(null, "Error! The entered date is invalid. " +
" Please enter a valid date on the form yyyy-mm-dd");
}
else {
weekDay(y, m, d);
}
}
}
}
Instead of asking us to debug through your entire code, perhaps consider LocalDate to get the desired result:
LocalDate ldt = LocalDate.parse("1999-12-31");
System.out.println(ldt.getDayOfWeek());
LocalDate ldt2 = LocalDate.parse("2000-01-01");
System.out.println(ldt2.getDayOfWeek());
Output:
FRIDAY
SATURDAY
The Problem is with finding the number of leap year. Your logic is counting the year 2000 also. The number of leap years should be same for 1999-12-31 and 2000-01-01. You need to consider year 2000 only if the month is greater than February. Increment the sumLeapDaysInLeapYears only if the input date is greater than Feb 28th
I was trying to build a beginner calendar exercise project and all I want is if last date of January ends on Sunday then start next month from Monday. Here is my code;
public static void main(String[] args)
{
String[] months= {"January","Febuary","March","April","May","June"
,"July","August","september","October","November"
,"December"
};
String[] weekdays= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
int x;
for(int k=0;k<12;k++)
{
if(months[k]=="April" ||months[k]=="june" ||months[k]=="September" ||months[k]=="November")
{x=30;}
else if(months[k]=="Febuary")
{x=28;}
else {x=31;}
System.out.print(months[k]+"\n");
for(int i=0;i<weekdays.length;i++)
{
System.out.print("\t"+weekdays[i]);
}System.out.println();
for(int m=1;m<=x;m++) {
if(((m-1) %7) == 0) //line break after 7 characters
{
System.out.println();
}
System.out.print("\t"+m);
}
System.out.println();
}
}
I think you use not correct approach. Working with dated relates to working with Locale, because all these names like name for the month, weekdays in different format are already in the JVM. You should use it:
public static void printCalendar(LocalDate date, Locale locale) {
DateFormatSymbols symbols = new DateFormatSymbols(locale);
WeekFields weekFields = WeekFields.of(locale);
printMonthName(symbols, date);
printWeekdayNames(symbols, weekFields);
printWeekdays(weekFields, date);
}
private static void printMonthName(DateFormatSymbols symbols, LocalDate date) {
System.out.println(symbols.getMonths()[date.getMonthValue() - 1]);
}
private static void printWeekdayNames(DateFormatSymbols symbols, WeekFields weekFields) {
String[] weekdays = symbols.getShortWeekdays();
DayOfWeek firstDayOfWeek = weekFields.getFirstDayOfWeek();
int offs = firstDayOfWeek == DayOfWeek.SUNDAY ? 1 : firstDayOfWeek.ordinal() + 2;
for (int i = 0; i < 7; i++)
System.out.print('\t' + (offs + i >= weekdays.length ? weekdays[(offs + i) % 7] : weekdays[offs + i]));
System.out.println();
}
private static void printWeekdays(WeekFields weekFields, LocalDate date) {
LocalDate cur = date.withDayOfMonth(1).with(weekFields.dayOfWeek(), 1);
boolean stop = false;
do {
if (cur.getMonthValue() == date.getMonthValue())
System.out.format("\t%2d", cur.getDayOfMonth());
else
System.out.format("\t ");
cur = cur.plusDays(1);
if (cur.getDayOfWeek() == weekFields.getFirstDayOfWeek()) {
System.out.println();
stop = cur.getMonthValue() != date.getMonthValue();
}
} while (!stop);
}
Test1:
printCalendar(LocalDate.of(2018, Month.DECEMBER, 17), Locale.US);
December
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Test2:
printCalendar(LocalDate.of(2018, Month.DECEMBER, 17), Locale.ITALIAN);
dicembre
lun mar mer gio ven sab dom
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Your program still needs some adjustments:
1. The first day of the year can be sunday or other day of the week.
2. If the last day of the of the month is sunday, you should continue the order and not starting with sunday from the beginning.
I will propose you to adjust you current code, or you can use an existing library https://www.javatpoint.com/java-util-calendar.
So basicaly, everything works like it should. But the main thing that should be correct, which is the date, is wrong, so far for october 2015 it is correct but for some reason i cannot detect, the other months i have tried so far are incorrect when compared to the calendar in my computer.
Here is my program, maybe you have some valid input to help me with these issues.
public static boolean isLeapYear(int year) {
int month = 0;
int s = getDaysIn(month, year);
return year % 4 == 0 && (year % 100 != 0) || (year % 400 == 0);
}
public static int getDaysIn(int month, int year) {
switch (month) {
case 1:
return 31;
case 2:
if (isLeapYear(month)) {
return 29;
} else {
return 28;
}
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
default:
return -1;
}
}
public static String getMonthName(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "Invalid month.";
}
}
public static int getStartDay(int month, int year) {
int days = 0;
for (int i = 1900; i < year; i++) {
days = days + 365;
if (isLeapYear(i)) {
days = days + 1;
}
}
for (int i = 1; i < month; i++) {
days = days + getDaysIn(month, year);
}
int startday = (days + 1) % 7 + 2;
return startday;
}
public static void displayCalendar(int month, int year) {
String monthName = getMonthName(month);
int startDay = getStartDay(month, year);
int monthDays = getDaysIn(month, year);
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
int weekDay = startDay - 1;
for (int i = 1; i < startDay; i = i + 1) {
System.out.print(" ");
}
for (int x = 1; x <= monthDays; x++) {
weekDay = weekDay + 1;
if (weekDay > 7) {
System.out.println();
weekDay = 1;
}
System.out.format(" %3d", x);
}
if (weekDay > 7) {
System.out.println();
}
}
Your getStartDay is actually where your error lays :
for (int i = 1; i < month; i++) {
days = days + getDaysIn(month, year);
}
int startday = (days + 1) % 7 + 2;
return startday;
Should be replaced with :
for (int i = 1; i < month; i++) {
days = days + getDaysIn(i, year);
}
int startday = (days % 7) + 2;
return startday;
There is a minor problem with your loop as you see.
You should have had used your incrementor i in your getDaysIn calls to count the days from the begin of the year until the month you want to display.
Second error is here :
int startday = (days + 1) % 7 + 2;
The logic behind this expression is still a mystery for me.
Let's break this off with a concrete example :
Let's say we want to display FEB 1900. Easy to do because we start at the begin of JAN 1900 so we can count the days easily (31 days in january so days = 31)
Which result will the first expression return?
= (31 + 1) % 7 + 2
= 32 % 7 + 2
= 4 + 2
= 6
Because day 1 in your logic is SUNDAY, day 6 is FRIDAY.
Let's check it out :
Not really the expected result right?
I see one more problem with this method.
Let's imagine days = 33 (this will never happens but we need this example here).
= (33 + 1) % 7 + 2
= 34 % 7 + 2
= 6 + 2
= 8
What would the 8th day of the week be?
The explanation of the correct answer is the following :
(days % 7) + 2
days % 7 gives us the value of the last day of the following month when weeks begin with MONDAY.
To this value, we add 2 :
1 to have the beginning day of the wanted month.
1 because our calendar begins with sunday and we'll have to slide our days on the calendar view.
Here is the full code (that I also modified a bit to make it more compact) :
public class Calendar{
public static void main(String[] args) {
// INSERT YOUR TESTS HERE
}
public static boolean isLeapYear(int year) {
return year % 4 == 0 && (year % 100 != 0) || (year % 400 == 0);
}
public static int getDaysIn(int month, int year) {
switch (month) {
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
return 31;
case 4 : case 6 : case 9 : case 11 :
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return -1;
}
}
public static String getMonthName(int month) {
String[] months = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
return month <= 12 && month > 0 ? months[month - 1] : "Invalid month";
}
public static int getStartDay(int month, int year) {
int days = 0;
for (int i = 1900; i < year; i++) {
days += 365;
if (isLeapYear(i)) {
days = days + 1;
}
}
for (int i = 1; i < month; i++) {
days = days + getDaysIn(i, year);
}
int startday = (days % 7) + 2;
return startday;
}
public static void displayCalendar(int month, int year) {
String monthName = getMonthName(month);
int startDay = getStartDay(month, year);
int monthDays = getDaysIn(month, year);
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
int weekDay = startDay - 1;
for (int i = 1; i < startDay; i = i + 1) {
System.out.print(" ");
}
for (int x = 1; x <= monthDays; x++) {
weekDay = weekDay + 1;
if (weekDay > 7) {
System.out.println();
weekDay = 1;
}
System.out.format(" %3d", x);
}
if (weekDay > 7) {
System.out.println();
}
}
}
EDIT
You should try to write something in your main method if nothing displays.
public static void main(String[] args) {
for (int i = 0 ; i < 5 ; i++){
System.out.println(getMonthName(i+1) + " " + (2010 + i));
displayCalendar(i+1, 2010 + i);
System.out.println();
}
}
Gives the following output :
January 2010
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
February 2011
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28
March 2012
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
April 2013
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
May 2014
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Spoiler alert!! for project euler problem 19.
The problem is how many sundays fall on the first of the month from 1901 to 2000. Given information: Jan 1st 1900 was a monday, Jan & march etc have 31 days, April, June etc 30 days and a leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. which in this case applies to year 2000.
I got the correct answer, but when I checked my results with a real calendar, it turns out my code is counting saturdays. I don't see why it is doing that, can someone help?!
public class Sunday {
public static void main(String[] args) {
int sundayCount = 0;
int currentday = 0; // monday is 0, sunday will be 6
int dayLimit = 0;
for (int year = 1900; year < 2001; year++) {
for (int month = 1; month < 13; month++) {
// 30 days April, June, September, November
if (month == 4 || month == 6 || month == 9 || month == 11)
dayLimit = 30;
// leap year
else if (month == 2 && year % 4 == 0)
dayLimit = 29;
// February not leap year
else if (month == 2 && year % 4 != 0)
dayLimit = 28;
// Jan, March, May, July, August, October, December
else
dayLimit = 31;
for (int day = 1; day <= dayLimit; day++) {
if (day == 1 && currentday == 6 && year > 1900) {
System.out.println("year: " + year);
System.out.println("month: " + month);
sundayCount++;
}
if (currentday < 6)
currentday++;
else
currentday = 0;
}
}
}
System.out.println(sundayCount);
}
}
You forget to add into the code to check for if febuary 1900 should have 29 or 28 days. As 1900 wasn't a leap year it shouldn't have 29 days in 1900.
else if (month == 2 && year % 4 == 0 && year != 1900)
dayLimit = 29;
// February not leap year
else if (month == 2)
dayLimit = 28;
This should fix it.