import java.util.Scanner;
/**
* Created by xpf on 2018/6/22 :)
* GitHub:xinpengfei520
* Function:
*/
public class CalculateUtils {
/*平年二月28天*/
private static final int DAYS_28 = 28;
/*闰年二月29天*/
private static final int DAYS_29 = 29;
/*除了31天的月份其他均为30天*/
private static final int DAYS_30 = 30;
/*1、3、5、7、8、10、12月份31天*/
private static final int DAYS_31 = 31;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please input year:");
int year = input.nextInt();
System.out.println("Please input month:");
int month = input.nextInt();
System.out.println("Please input day:");
int day = input.nextInt();
int daysInYear = getDaysInYear(year, month, day);
System.out.println("daysInYear:" + daysInYear);
}
/**
* get days in this year
*
* @param year
* @param month
* @param day
* @return
*/
public static int getDaysInYear(int year, int month, int day) {
int totalDays = 0;
switch (month) {
// 12 月份加的是11月份的天数,依次类推
case 12:
totalDays += DAYS_30;
case 11:
totalDays += DAYS_31;
case 10:
totalDays += DAYS_30;
case 9:
totalDays += DAYS_31;
case 8:
totalDays += DAYS_31;
case 7:
totalDays += DAYS_30;
case 6:
totalDays += DAYS_31;
case 5:
totalDays += DAYS_30;
case 4:
totalDays += DAYS_31;
case 3:
// 判断是否是闰年
if (((year / 4 == 0) && (year / 100 != 0)) || (year / 400 == 0)) {
totalDays += DAYS_29;
} else {
totalDays += DAYS_28;
}
case 2:
totalDays += DAYS_31;
case 1: // 如果是1月份就加上输入的天数
totalDays += day;
}
return totalDays;
}
}
【解题思路】
1、通过年份区分是闰年还是平年,平年 2 月 28 年,闰年 2 月 29 天。
2、1、3、5、7、8、10、12 月份为 31 天,其余月份为 30 天。
3、将每个月的天数相加即可,如果输入的是 12 月,则从 11 月往前累加到1月。
扩展资料
其他java计算日期的方式
package study01;
import java.util.Scanner;
public class TestDay {
/*
* 输入2017年的月和日:month=?,day=? 输出输入的日期是2017年的第几天,使用switch完成
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("month=");
int month = sc.nextInt();
System.out.print("day=");
int day = sc.nextInt();
int days = 0;
switch (month) {
case 12:
days += 30;
case 11:
days += 31;
case 10:
days += 30;
case 9:
days += 31;
case 8:
days += 31;
case 7:
days += 30;
case 6:
days += 31;
case 5:
days += 30;
case 4:
days += 31;
case 3:
days += 28;
case 2:
days += 31;
case 1:
days += day;
}
if(days>365){
System.out.println("你输入的已经超过了365天了");
}else{
System.out.println("第" + days + "天");
}
}
}
输出的结果如下:
month=12
day=31
第365天
参考资料:百度百科-Scanner
参考资料:百度百科-java
import java.util.Scanner;
public class practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入年份:");
int year = sc.nextInt();
System.out.print("请输入月份:");
int month = sc.nextInt();
System.out.print("请输入日:");
int day = sc.nextInt();
int count = 0;
int days = 0;
if (year > 0 && month > 0 && month < 13 && day > 0 && day < 32) {
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2: {
if ((year % 4 == 0 && year % 1 != 0) || (year % 400 == 0)) {
days = 29;
} else {
days = 28;
}
break;
}
}
count = count + days;
}
count = count + day;
System.out.println(year + "年" + month + "月" + day + "日是" + year + "年的第" + count + "天");
} else
System.out.println("数据输入错误。");
}
}
扩展资料
import java.util.Scanner;
/**
* Created by admin on 2016/11/25.
*/
public class Day {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int year = sc.nextInt();//年份
int month = sc.nextInt();//月份
int day = sc.nextInt();//日期
//判断是平年还是闰年
boolean isleapYear = ((year%4==0&&year%100!=0)||(year%400)==0?true:false);
int days = 0;
switch (month-1){
case 12:days += 31;
case 11:days += 30;
case 10:days += 31;
case 9:days += 30;
case 8:days += 31;
case 7:days += 31;
case 6:days += 30;
case 5:days += 31;
case 4:days += 30;
case 3:days += 31;
case 2: if(isleapYear)
days += 29;
else
days += 28;
case 1:days += 31;
}
System.out.println(day+days);
}
}
}
参考资料:百度百科 time函数
本回答被网友采纳