如何打印给定日期月份的日期?

时间:2014-01-06 04:46:09

标签: java date

在这里,我得到了飞跃&普通的一年和一个月,但我不知道如何获得给定日期月份的日期。还有一个问题:1800和1900是普通的一年,但我得到这些年是闰年。你能解决这个问题吗?

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");
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

这是如何识别公历的闰年

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

请参阅http://en.wikipedia.org/wiki/Leap_year

答案 1 :(得分:0)

为什么不使用java.util.Datejava.util.GregorianCalendar类? 然后,您只需从控制台读取输入,并将其解析为Calendar对象。

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;
}

闰年

闰年可以除以4,除了也可以除以100,除了(!)也可以除以400.因此,1800不是闰年,因为它可以除以4和100,但不是400. 2000年是闰年,因为它可以被400整除。

答案 2 :(得分:0)

根据以前的信息:

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, ...
    }
}