(JAVA)输入年月日,计算日期是今年的第几天?

用Java判断一个日期,包括年,月,日三个数,计算这个日期是这一年的第几天,~

可以使用calendar累获取天数:
已知年月日,直接用年月日构造一个Date或者Calender对象,然后有现成的方法获得当前日期是所属年份的第几天,是当前周的第几天,当前月的第几天
比如Calender对象属性
DAY_OF_YEAR
public static final int DAY_OF_YEAR

get 和 set 的字段数字,指示当前年中的天数。一年中第一天的值为 1。

用Calendar类,里面有一个算某一天是一年的第几天的,然后你用那一年的总天数减一下就能得到是倒数第几天了.
自己输入传入time 的值
Calendar cal = Calendar.getInstance();
//通过输入传入年月日
cal.set(int year, int month, int date);
if(cal.get(Calendar.YEAR)%100 == 0) {
if(cal.get(Calendar.YEAR)%400 == 0) {
System.out.println("倒数第"+(366-cal.get(Calendar.DAY_OF_YEAR))+"天");
}
}else {
if(cal.get(Calendar.YEAR)%4 == 0) {
System.out.println("倒数第"+(366-cal.get(Calendar.DAY_OF_YEAR))+"天");
}else {
System.out.println("倒数第"+(365-cal.get(Calendar.DAY_OF_YEAR))+"天");
}
}

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)); //这是获取当前是当年的第几天

(JAVA)输入年月日,计算日期是今年的第几天?
答:1、通过年份区分是闰年还是平年,平年 2 月 28 年,闰年 2 月 29 天。2、1、3、5、7、8、10、12 月份为 31 天,其余月份为 30 天。3、将每个月的天数相加即可,如果输入的是 12 月,则从 11 月往前累加到1月。

编写程序,提示用户输入月份和年份,然后显示这个月份的天数.java语言编 ...
答:import java.util.Scanner;public class $ { private static int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("输入年份:"); int year =...

java中 输如年月日,然后输出是这年的第几天
答:import java.util.Date;public class testshuzu { public static void main(String[] args) { // 给定一个日期。算出是一年的第几天。int sum = 0;Date d = new Date(2009, 5, 10);// 年份 y 由整数 y - 1900 表示。 月份由从 0 至 11 // 的整数表示;0 是一月、1 是二月等等...

(用JAVA实现)请使用日期相关的api,控制台输入出生日期,计算出一个人...
答:这个问题主要涉及日期的解析及时间分量的计算。思路:使用SimpleDateFormat将输入的字符串表示的日期解析为Date,再将Data转为Calendar,获取日期分类年份,然后与当前年份做差运算即可。代码如下:代码实现

用Java做一个题:输出一个年月日,得出这个日子是这一年的多少天?请高...
答:import java.util.Scanner;public class Aasf { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in);System.out.println("请输入年");int Y=input.nextInt();System.out.println("请输入月");int M=input.nextInt();System....

Java编写程序完成输人年份和月份,计算指定年份中的天数并含代码注解...
答:void main(String[] args) {System.out.println(getDay("2018", "2020", "yyyy"));System.out.println(getDay("2018-01-01", "2018-12-31", "yyyy-MM-dd"));System.out.println(getDay("2019-02-07", "2019-02-11", "yyyy-MM-dd"));}年的月的都有,有什么疑问可以问我。

编写Java程序。当以年月日的格式输入一个日期时,输出该年是否是闰年,该...
答:public static void main(String[] args) throws ParseException {System.out.println("输入一个日期");Scanner input=new Scanner(System.in);String day=input.nextLine();SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd");Date date=a.parse(day);Calendar c=Calendar.getInstance();//设置...

这个用java输入年月日信息,输出这天是这年的第几天怎么写啊?_百度知 ...
答:public static void main(String[] args){ int year,month,day,a=0,b=0;Scanner reader=new Scanner(System.in);System.out.println("请输入年份");year=reader.nextInt();System.out.println("请输入月份");month=reader.nextInt();System.out.println("请输入日");day=reader.nextInt();f...

java编程:输入现在的月份,输入今天几号,计算今年一共过了多少天,这个编...
答:import java.util.Scanner;public class E { public static void main(String[] args) { int y,m,d,days,i,d1=0;System.out.printf("请输入年,月,日:\n");Scanner in=new Scanner(System.in);y=in.nextInt();Scanner in1=new Scanner(System.in);m=in1.nextInt();Scanner in2=...

用java输入一个日期,怎么算出该日期的下一天是哪年哪月哪日
答:import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class NextDay { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));System.out.println("请输入日期,...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网