求日期的前一天

给定一个年月日,返回前一天的日期,年的取值在1812-2017,月的取值为1-12,日的取值为1-31

package Nextdate;

import java.util.Scanner;

public class NextDate 
{
	public static int Leapyear(int year)
	{
		 if((year%4==0&&year%100!=0)||(year%400==0))
              return 1;
		 else
			 return 0;
	}
    public static void main(String args[])
    {
    	int flag;
    	int preyear=0,premonth=0,preday=0;
    	System.out.println("请输入此格式日期:年 月 日");
    	Scanner s=new Scanner(System.in);
    	int year=s.nextInt();
    	int month=s.nextInt();
    	int day=s.nextInt();
    	flag=Leapyear(year);
    	if(year<1812||year>2017)
    	{
    		System.out.println("年超出了范围");
    		System.exit(0);
    	}
    	if(year>=1812&&year<=2017)
    	{
    		if(year==1812&&month==1&&day==1)
    		{
    		   System.out.println("此为底线日期");
    		   System.exit(0);
    		}
    		if(month>12)
    		{
    			System.out.println("月份超出了正常值");
    			System.exit(0);
    		}
    		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
    		{
    			if(day<=0||day>31)
    			{
    				System.out.println("该月不存在这一天");
    				System.exit(0);
    			}
    		}
    		if(month==4||month==6||month==9||month==11)
    		{
    			if(day<=0||day>30)
    			{
    				System.out.println("该月不存在这一天");
    				System.exit(0);
    			}
    		}
    		if(month==2)
    		{
    			if(flag==1)
    			{
    				if(day<=0||day>29)
        			{
        				System.out.println("该年二月不存在这一天");
        				System.exit(0);
        			}
    			}
    			else
    			{
    				if(day<=0||day>28)
        			{
        				System.out.println("该年二月不存在这一天");
        				System.exit(0);
        			}
    			}
    		}
    		if(month==1)
    		{
    			if(day>1)
    			{
    				preday=day-1;
    				premonth=month;
    				preyear=year;
    			}
    			else
    			{
    				preday=31;
    				premonth=12;
    				preyear=year-1;
    			}
    		}
    		if(month==3)
    		{
    		    if(day==1)
    			{
    				   if(flag==1)
    					   preday=29;
    				   else
    					   preday=28;
    				   premonth=month-1;
    				   preyear=year;
    			}
    		    else
    		    {
    		    	preday=day-1;
    		    	premonth=month;
    		    	preyear=year;
    		    }
            }
    		if(month==2||month==4||month==6||month==8||month==9||month==11)
    		{
    			if(day==1)
    			{
    				preday=31;
    				preyear=year;
    				premonth=month-1;
    			}
    			else
    			{
    				preday=day-1;
    		    	premonth=month;
    		    	preyear=year;
    			}
    		}
    		if(month==5||month==7||month==10||month==12)
    		{
    			if(day==1)
    			{
    				preday=30;
    				preyear=year;
    				premonth=month-1;
    			}
    			else
    			{
    				preday=day-1;
    		    	premonth=month;
    		    	preyear=year;
    			}
    		}
    		System.out.println("前一天为:"+preyear+" "+premonth+" "+preday);
    	}
    }
}


你可能感兴趣的:(求日期的前一天)