这里使用了Java8对日期处理的新特性。列出了三种实现方式。
public class TheDayInYear_One {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("请输入你要判断的日期(xxxx年xx月xx日):");
String dateIn = scan.nextLine();
if(dateIn.length()==11){
int year = Integer.parseInt(dateIn.substring(0, 4));//获取输入的年份
int month = Integer.parseInt(dateIn.substring(5, 7));//获取输入的月份
int day = Integer.parseInt(dateIn.substring(8, 10));//获取输入的日数
System.out.println("day="+dateIn.substring(8, 10));
LocalDate date = LocalDate.of(year, month, day);//将其转换为Java8中的LocalDate类型
System.out.println("转换结果:"+year+"-"+month+"-"+day);
int[] arr = new int[]{31,60,91,121,152,182,213,244,274,305,335,366};//定义数组,存放截至每个月为止当年的天数
int count = 0;//声明变量,存放计算结果(天数)
if(month == 1){// 如果是一月,天数就是当前的日数
count = day;
}
if(date.isLeapYear()){// 判断输入的年份是否是瑞年(Java8中新特性)
System.out.println("是瑞年");
if(month>1 && month<13){
count = arr[month-2]+day;
}
}else{
System.out.println("是平年");
if(month>1 && month<13){
count = arr[month-2]+day-1;
}
}
System.out.println(count);
}else{
System.out.println("输入的日期格式与要求格式不符!!");
}
}
}
public class TheDayInYear_Two {
public static void main(String[] args) {
System.out.println("请输入你要判断的日期(xxxx年xx月xx日):");
Scanner scan = new Scanner(System.in);
String dateIn = scan.nextLine();
int count = 0;
if(dateIn.length() == 11){
int year = Integer.parseInt(dateIn.substring(0, 4));
int month = Integer.parseInt(dateIn.substring(5, 7));//获取输入的月份
int day = Integer.parseInt(dateIn.substring(8, 10));//获取输入的日数
LocalDate date = LocalDate.of(year, month, day);
System.out.println("转换结果:"+year+"-"+month+"-"+day);
if(month == 1){
count = day;
}else if(month == 2){
count = 31 + day;
}else{
if(date.isLeapYear()){
System.out.println("是瑞年");
if(month == 3){
count = 31 + 29 + day;
}
if(month == 4){
count = 31 + 29 + 31 + day;
}
if(month == 5){
count = 31 + 29 + 31 + 30 + day;
}
if(month == 6){
count = 31 + 29 + 31 + 30 + 31 + day;
}
if(month == 7){
count = 31 + 29 + 31 + 30 + 31 + 30 + day;
}
if(month == 8){
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
}
if(month == 9){
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day;
}
if(month == 10){
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
}
if(month == 11){
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
}
if(month == 12){
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
}
}else{
System.out.println("是平年");
if(month == 3){
count = 31 + 28 + day;
}
if(month == 4){
count = 31 + 28 + 31 + day;
}
if(month == 5){
count = 31 + 28 + 31 + 30 + day;
}
if(month == 6){
count = 31 + 28 + 31 + 30 + 31 + day;
}
if(month == 7){
count = 31 + 28 + 31 + 30 + 31 + 30 + day;
}
if(month == 8){
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
}
if(month == 9){
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
}
if(month == 10){
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
}
if(month == 11){
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
}
if(month == 12){
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
}
}
}
}else{
System.out.println("输入的日期格式与要求格式不符!!");
}
System.out.println(count);
}
}
public class TheDayInYear_Three {
public static void main(String[] args) {
System.out.println("请输入你要判断的日期(xxxx年xx月xx日):");
Scanner scan = new Scanner(System.in);
String dateIn = scan.nextLine();
int count = 0;
if (dateIn.length() == 11) {
int year = Integer.parseInt(dateIn.substring(0, 4));
int month = Integer.parseInt(dateIn.substring(5, 7));
int day = Integer.parseInt(dateIn.substring(8, 10));
LocalDate date = LocalDate.of(year, month, day);
System.out.println("转换结果:" + year + "-" + month + "-" + day);
if (month == 1) {
count = day;
} else if (count == 2) {
count = 31 + day;
} else {
if (date.isLeapYear()) {
switch (month) {
case 3:
count = 31 + 29 + day;
break;
case 4:
count = 31 + 29 + 31 + day;
break;
case 5:
count = 31 + 29 + 31 + 30 + day;
break;
case 6:
count = 31 + 29 + 31 + 30 + 31 + day;
break;
case 7:
count = 31 + 29 + 31 + 30 + 31 + 30 + day;
break;
case 8:
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
break;
case 9:
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day;
break;
case 10:
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
break;
case 11:
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
break;
case 12:
count = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
break;
}
} else {
switch (month) {
case 3:
count = 31 + 28 + day;
break;
case 4:
count = 31 + 28 + 31 + day;
break;
case 5:
count = 31 + 28 + 31 + 30 + day;
break;
case 6:
count = 31 + 28 + 31 + 30 + 31 + day;
break;
case 7:
count = 31 + 28 + 31 + 30 + 31 + 30 + day;
break;
case 8:
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
break;
case 9:
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
break;
case 10:
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
break;
case 11:
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
break;
case 12:
count = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
break;
}
}
}
} else {
System.out.println("输入的日期格式与要求格式不符!!");
}
System.out.println(count);
}
}