java制定年月日第几天_Java基础试题:输入年月日,判断这一天是这一年的第几天...

这里使用了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);

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值