输入日期,得到明天的日期

来源:互联网 发布:淘宝智能版退回专业版 编辑:程序博客网 时间:2024/05/21 17:11

package dateEntity;
public class Date1 {
    private String date;//日期字符串
    /**
     * 无参构造函数
     */
    public Date1(){}
    /**
     * 有参构造函数
     */
    public Date1(String date){
        setDate(date);
    }
    /**
     * 设置日期
     */
    public void setDate(String date) {
        String [] arr=date.split("-");
        if(arr.length!=3){
            System.out.println("程序输出:日期非法");
            return;
        }
        int yyyy,mm,dd;
        try{
            yyyy=Integer.parseInt(arr[0]);
            mm=Integer.parseInt(arr[1]);
            dd=Integer.parseInt(arr[2]);
        }catch (Exception e) {
            System.out.println("程序输出:日期非法");
            return;
        }
        //如果年或月或日的范围不正确,则示错并返回
        if((yyyy<1900 || yyyy>2050) || (mm<1 || mm>12) || (dd<1 || dd>getMaxDate(yyyy, mm)) ){
            System.out.println("程序输出:日期非法");
            return;
        }
        this.date = date;
    }   
    /**
     * 取得日期
     */
    public String getDate() {
        return date;
    }
    /**
     * 取得当前日期的后一天
     */
    public String getNextDate(){
        String [] arr=date.split("-");
        int yyyy,mm,dd;
        yyyy=Integer.parseInt(arr[0]);
        mm=Integer.parseInt(arr[1]);
        dd=Integer.parseInt(arr[2]);
        if(dd
            return yyyy+"-"+mm+"-"+(dd+1);
        }       
        if(mm==12){
            return (yyyy+1)+"-1-1";
        }
        return yyyy+"-"+(mm+1)+"-1";
    }
    /**
     * 判断是否是闰年
     */
    public boolean isLeapYear(int yyyy){
        if( (yyyy%4==0 && yyyy%100!=0) || yyyy%400==0 ){
            return true;
        }
        return false;
    }
    /**
     * 根据年份和月份, 返回该年该月的最大日期数
     */
    public int getMaxDate(int yyyy,int mm){
        int maxDate;
        switch (mm) {
            case 2:
                maxDate=28;
                if(isLeapYear(yyyy)){
                    maxDate=29;
                }
                break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                maxDate=31;
                break;
            default:
                maxDate=30;
                break;
        }
        return maxDate;
    }
}

//*************************************************************************//

package dateTest;
import java.util.Scanner;
import dateEntity.Date1;

public class TestDate1 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("请按'yyyy-mm-dd'格式输入一个日期(输入'no'则退出),程序将输出下一天的日期:");
        do{
            System.out.print("用户输入:");
            String strDate=input.next();
            if(strDate.equals("no")){
                System.out.println("程序退出!");
                break;
            }
            Date1 date=new Date1(strDate);
            if(date.getDate()!=null){
                System.out.println("程序输出:"+date.getNextDate());
            }
            System.out.println();
        }while(true);
    }
}

//*************************************************************************//

上面是学JavaOOP时老师要求用面向对象的方法, 自己写一个类来完成的。

另:最近想法:

package com.yenange.test2;
import java.util.Date;

public class testDateAdd {
    public static void main(String[] args) {
        Date date=new Date();
        Long addDate=date.getTime()+24*60*60*1000;
        date=new Date(addDate);
        System.out.println(date);
    }
}

原创粉丝点击