简体   繁体   中英

How to print day of the given date month year?

In this I get leap & ordinary year and month, but I don't know how to get day of the given date month year. And also having one question: 1800 and 1900 are ordinary year but I get these years are leap year. Can you solve this?

import java.util.Scanner;

class day {

    public static void main(String arg[]) {
        int tm, sm, w;
        int y;
        int[] m = new int[12];
        m[0] = 31;
        m[1] = 28;
        m[2] = 31;
        m[3] = 30;
        m[4] = 31;
        m[5] = 30;
        m[6] = 31;
        m[7] = 31;
        m[8] = 30;
        m[9] = 31;
        m[10] = 30;
        m[11] = 31;
        //{31,28,31,30,31,30,31,31,30,31,30,31};
        String[] mo = new String[12];
        mo[0] = "January";
        mo[1] = "February";
        mo[2] = "March";
        mo[3] = "April";
        mo[4] = "May";
        mo[5] = "June";
        mo[6] = "July";
        mo[7] = "August";
        mo[8] = "September";
        mo[9] = "October";
        mo[10] = "November";
        mo[11] = "December";
        String[] we = new String[w];
        we[0] = "Sunday";
        we[1] = "Monday";
        we[2] = "Tuesday";
        we[3] = "Wednesday";
        we[4] = "Thursday";
        we[5] = "Friday";
        we[6] = "Saturday";
        Scanner ip = new Scanner(System.in);
        System.out.print("\nEnter year ");
        y = ip.nextInt();
        System.out.print("\nEnter month ");
        sm = ip.nextInt();
        if (y % 4 == 0) {
            m[1] = m[1] + 1;
            for (tm = 1; tm <= m.length; tm++) {
                if (tm == sm) {
                    System.out.print("\n" + y + " is a Leap Year\n"
                            + mo[sm - 1] + " month " + "has " + m[sm - 1] + " days\n");
                }
            }
        } else {
            for (tm = 1; tm <= 12; tm++) {
                if (tm == sm) {
                    System.out.print("\n" + y + " is an Ordinary year\n"
                            + mo[sm - 1] + " month " + "has " + m[sm - 1] + " days\n");
                }
            }
        }
    }
}

this is how to identify a leap year for Gregorian calendar

if ((y % 4 == 0  && y % 100 !=0) || y % 400 == 0) {
  ...

see http://en.wikipedia.org/wiki/Leap_year

Why don't you use the java.util.Date and java.util.GregorianCalendar classes? Then you can simply read the input from the console, and parse it into a Calendar object.

import java.io.Console;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
// ... other stuff
Console c = System.console();
Date date;
// Note: Console is not working from Netbeans, you have to run it through a real shell.
String dateinput = c.readLine("Phlease, enter the date (as year-month-day): ");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try
{
    date = simpleDateFormat.parse(dateinput);
}
catch (ParseException ex)
{
    System.out.println("Oops, problem! Exception: " + ex);
}

// Now, you have the input date as a Date object.
// Checking if it is a leap year:
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
// Now, the calendar object has the date. 
// From now on, we use this calendar to extract date/time informations:
if (gc.isLeapYear(gc.get(Calendar.YEAR))) {
    System.out.println("Yeah, this year is a leap year.");
} else {
    System.out.println("No, it is a normal year.");
}
// DOW returns an int, that is encoded in the following constants:
switch(gc.get(Calendar.DAY_OF_WEEK)) {
    case Calendar.MONDAY: System.out.println("Monday!"); break;
    case Calendar.TUESDAY: System.out.println("Thuesday!"); break;
    case Calendar.WEDNESDAY: System.out.println("Wednesday!"); break;
    case Calendar.THURSDAY: System.out.println("Thor's day!"); break;
    case Calendar.FRIDAY: System.out.println("Friday!"); break;

    case Calendar.SATURDAY: System.out.println("Saturday!"); break;
    case Calendar.SUNDAY: System.out.println("Sunday!"); break;
}

Leap years

Leap year is what can be divided by 4, except that also can be divided by 100, except(!) that also can be divided by 400. Hence, 1800 is not a leap year, as it can be divided by 4 and 100, but not with 400. 2000 was a leap year, as it is divisible by 400.

Based on previous info:

public class JavaApplication1 {

    public static void main(String[] args) {
        int y, sm, sd;
        int[] m = new int[12];
        m[0] = 31;
        m[1] = 28;
        m[2] = 31;
        m[3] = 30;
        m[4] = 31;
        m[5] = 30;
        m[6] = 31;
        m[7] = 31;
        m[8] = 30;
        m[9] = 31;
        m[10] = 30;
        m[11] = 31;
        //{31,28,31,30,31,30,31,31,30,31,30,31};

        String[] mo = new String[12];
        mo[0] = "January";
        mo[1] = "February";
        mo[2] = "March";
        mo[3] = "April";
        mo[4] = "May";
        mo[5] = "June";
        mo[6] = "July";
        mo[7] = "August";
        mo[8] = "September";
        mo[9] = "October";
        mo[10] = "November";
        mo[11] = "December";

        String[] we = new String[7];
        we[0] = "Sunday";
        we[1] = "Monday";
        we[2] = "Tuesday";
        we[3] = "Wednesday";
        we[4] = "Thursday";
        we[5] = "Friday";
        we[6] = "Saturday";

        Scanner ip = new Scanner(System.in);
        System.out.print("\nEnter year ");
        y = ip.nextInt();
        System.out.print("\nEnter month ");
        sm = ip.nextInt();
        System.out.print("\nEnter day ");
        sd = ip.nextInt();

        // Using the method Evgeniy Dorofeev kindly shared with us:
        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
            m[1] = m[1] + 1;
            System.out.print(y + " is a Leap Year. ");
        } else {
            System.out.print(y + " is an Ordinary year. ");
        }

        System.out.println(mo[sm - 1] + " month " + "has " + m[sm - 1] + " days");

        int dow = dayOfWeek(y, sm, sd);
        System.out.println("Day " + sd + " is a " + we[dow]);
    }

    //https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Implementation-dependent_methods_of_Sakamoto.2C_Lachman.2C_Keith_and_Craver
    public static int dayOfWeek(int y, int m, int d) {
        int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
        y -= (m < 3 ? 1 : 0);
        return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
        // 0 is Sunday, 1 is monday, ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM