[英]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. 还有一个问题:1800和1900是普通的一年,但我得到这些年是闰年。 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 见http://en.wikipedia.org/wiki/Leap_year
Why don't you use the java.util.Date and java.util.GregorianCalendar classes? 为什么不使用java.util.Date和java.util.GregorianCalendar类? Then you can simply read the input from the console, and parse it into a Calendar object. 然后,您只需从控制台读取输入,并将其解析为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;
}
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. 闰年可以除以4,除了也可以除以100,除了(!)也可以除以400.因此,1800不是闰年,因为它可以除以4和100,但不是400. 2000年是闰年,因为它可以被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, ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.