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函数
这个代码非常正确,还解决了2月份等天数不对的问题。
你可以试试看,手工代码,记得采纳,非常感谢。
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;
public class test{
// 存储输入的年份
private static int year;
// 存储输入的月份
private static int mouth;
// 存储输入的日,也就是那个月的多少号
private static int day;
//统计天数
private static int sum = 0;
// 判断输入的年份是平年还是闰年,如果返回的值是true,表示该年份是闰年,如果返回值是false,表示该年份是平年
private static boolean JudgeYear(int input) {
if ((input % 4 == 0 && input % 100 != 0) || input % 400 == 0) {
return true;
}
return false;
}
// 对输入的月份进行判断,也就是算出该月有多少天
private static int Inputday(int input,int inputMonth) {
if (inputMonth == 1 || inputMonth == 3 || inputMonth == 5
|| inputMonth == 7 || inputMonth == 8 || inputMonth == 10
|| inputMonth == 12) {
return 31;
} else if (inputMonth == 2) {
if (JudgeYear(input) == true) {
return 29;
} else {
return 28;
}
} else {
return 30;
}
}
/**以下输入年份、月份和日的三个方法在输入的时候如果不是整数,
就会出现异常,所以使用try..catch语句进行异常处理*/
//输入年份的方法
private static void inputYear() {
System.out.print("请输入年份:");
try {
year = new Scanner(System.in).nextInt();
} catch (Exception e) {
System.out.println("你输入的年份有误!");
System.out.println("");
inputYear();
}
if (year < 1900) {
System.out.println("年份必须大于1900年!");
System.out.println("");
inputYear();
}
}
//输入月份的方法
private static void inputMonth() {
System.out.print("请输入月份:");
try {
mouth = new Scanner(System.in).nextInt();
} catch (Exception e) {
System.out.println("你输入的月份有误!");
System.out.println("");
inputMonth();
}
if (mouth > 12 || mouth < 1) {
System.out.println("你输入的月份有误!");
System.out.println("");
inputMonth();
}
}
//输入日的方法
private static void inputMonthDay() {
System.out.print("请输入日:");
try {
day = new Scanner(System.in).nextInt();
} catch (Exception e) {
System.out.println("你输入的日有误!");
System.out.println("");
inputMonthDay();
}
if (Inputday(year,mouth) < day || day < 1) {
System.out.println("你输入的日有误!");
System.out.println("");
inputMonthDay();
}
}
//输出有多少天
private static void toatalDay(){
inputYear();
inputMonth();
inputMonthDay();
for (int i = 1; i < mouth; i++) {
sum += Inputday(year,i);
}
sum+=day - 1;
System.out.println(String.format("%d年%d月%d日是%d年的第%d天!", year,mouth,day,year,sum));
}
public static void main(String[] args) {
toatalDay();
}
}
java.util.Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_YEAR)); //这是获取当前是当年的第几天