根据具体的天数返回日期

package com.wang.struts.util;

import java.util.Date;
import java.util.Scanner;
import java.text.SimpleDateFormat;

public class DateUtil {

  
    public String getBackDate(int backDay) {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String date = "";
        int year = Integer.parseInt((today.split("-"))[0]);
        int month = Integer.parseInt((today.split("-"))[1]);
        int day = Integer.parseInt((today.split("-"))[2]);
        if (day - (backDay - 1) > 0) {
            return date + year + "-" + month + "-" + (day - (backDay - 1));
        } else {
            return date
                    + getBackYear(year, month)
                    + "-"
                    + getLastMonth(month)
                    + "-"
                    + (getMonthDays(year, getLastMonth(month)) - (backDay - 1 - day));
        }
    }

  
    public String getForwardDate(int forwardDay) {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        int year = Integer.parseInt((today.split("-"))[0]);
        int month = Integer.parseInt((today.split("-"))[1]);
        int day = Integer.parseInt((today.split("-"))[2]);
        if (day + forwardDay <= 28) {
            return "" + year + "-" + month + "-" + (day + forwardDay);
        } else {
            switch (month) {
            case 2:
                return "" + getForwardYear(year, month) + "-"
                        + getNextMonth(month) + "-"
                        + (forwardDay - (getMonthDays(year, month) - day));

            case 4:
                if (day + forwardDay <= 30) {
                    return "" + year + "-" + month + "-" + (day + forwardDay);
                } else {
                    return "" + getForwardYear(year, month) + "-"
                            + getNextMonth(month) + "-"
                            + (forwardDay - (getMonthDays(year, month) - day));
                }
            case 6:
                if (day + forwardDay <= 30) {
                    return "" + year + "-" + month + "-" + (day + forwardDay);
                } else {
                    return "" + getForwardYear(year, month) + "-"
                            + getNextMonth(month) + "-"
                            + (forwardDay - (getMonthDays(year, month) - day));
                }

            case 9:
                if (day + forwardDay <= 30) {
                    return "" + year + "-" + month + "-" + (day + forwardDay);
                } else {
                    return "" + getForwardYear(year, month) + "-"
                            + getNextMonth(month) + "-"
                            + (forwardDay - (getMonthDays(year, month) - day));
                }

            case 11:
                if (day + forwardDay <= 30) {
                    return "" + year + "-" + month + "-" + (day + forwardDay);
                } else {
                    return "" + getForwardYear(year, month) + "-"
                            + getNextMonth(month) + "-"
                            + (forwardDay - (getMonthDays(year, month) - day));
                }

            default:
                if (day + forwardDay <= 31) {
                    return "" + year + "-" + month + "-" + (day + forwardDay);
                } else {
                    return "" + getForwardYear(year, month) + "-"
                            + getNextMonth(month) + "-"
                            + (forwardDay - (getMonthDays(year, month) - day));
                }
            }
        }
    }

  
    public boolean isRn(int year) {
        if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
            return true;
        return false;
    }

  
    public int getLastMonth(int month) {
        if (month != 1)
            return month - 1;
        else
            return 12;
    }

  
    public int getNextMonth(int month) {
        if (month != 12)
            return month + 1;
        else
            return 1;
    }

  
    public int getBackYear(int year, int month) {
        if (getLastMonth(month) == 12)
            return year - 1;
        else
            return year;
    }

  
    public int getForwardYear(int year, int month) {
        if (getNextMonth(month) == 1)
            return year + 1;
        else
            return year;
    }

  
    public int getMonthDays(int year, int month) {
        int lastMonthDays = 0;
        switch (month) {
        case 2:
            if (isRn(year))
                lastMonthDays = 28;
            else
                lastMonthDays = 29;
            break;
        case 4:
            lastMonthDays = 30;
            break;
        case 6:
            lastMonthDays = 30;
            break;
        case 9:
            lastMonthDays = 30;
            break;
        case 11:
            lastMonthDays = 30;
            break;
        default:
            lastMonthDays = 31;
        }
        return lastMonthDays;
    }

    // private int d = 0 ;
    // public int geDay(int year,int month,int backDay){
    // if (getMonthDays(year,month) - (backDay - 1) > 0) {
    // d = getMonthDays(year,month) - (backDay - 1);
    // return d;
    // }else{
    // geDay(year,month,backDay);
    //         
    // }
    // return d;
    // }

    public static void main(String[] args) {
        DateUtil du = new DateUtil();
        // for (int m = 1; m <= 12; m++) {
        // System.out.println("1900年" + m + "月的天数" + du.getMonthDays(1900, m));
        // }
        int sign = 1;
        Scanner sc = new Scanner(System.in);
        while (sign != 0) {
            System.out.print("输入一个天数:");
            sign = sc.nextInt();
            System.out.println(du.getForwardDate(sign));
        }
        System.out.println("程序退出");
    }
}


你可能感兴趣的:(struts)