日期计划和每天递增

时间:2014-11-17 22:59:52

标签: java date

我完全迷失在这个问题上。我们要创建一个程序,允许用户输入日期(月,日,年),程序应打印出该日期,并在每天后增加1。如果日期输入在该月的最后一天下降。月份需要加1,第二天应该重新开始1.如果是12月31日,则输入xxx1。那么月,日和年应该改为1月1日,xxx2。此外,如果日期是闰年。比如说输入是2009年2月15日。如果不是2月28日打印出来的闰年,日期应该增加到2月29日。

我完全不知道如何解决这个问题。如果有人能指出我正确的方向,我将非常感激。

我在这里:

package date;

public class Date 
{
  private static final int February = 2;
  private static final int December = 12;
  private int month;
  private int day;
  private int year;

  public Date()
  {
    month = 1;
    day = 1;
    year = 2000;
  }

  public Date(int month, int day, int year)
  {
    this.month = month;
    this.day = day;
    this.year = year;       
  }

  public void setMonth(int month)
  {
    this.month = month;
  }

  public void setDay(int day)
  {
    this.day = day;
  }

  public void setYear(int year)
  {
    this.year = year;
  }

  public int getMonth(int month)
  {
    return month;
  }

  public int getDay(int day)
  {
    return day;
  }

  public static int getYear(int year)
  {
    return year;
  }

  public boolean nextDay()
  { 
    boolean leapYear = false;
    int currentMonthMaxDays;
    int [] daysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    currentMonthMaxDays = daysInMonths[month];
    if(day < currentMonthMaxDays)
    {
        day = day + 1;
        day++;
    }

    else if(month == February && leapYear && day == 28)
    {
        day = 29;
        day++;
    }
        else day = 1;

            if(month != December)

                month +=1;

                else 
                    year+=1;
                    month = 1;

    while(!leapYear)
    {
        if(year % 400 ==0)
            leapYear = true;
        else if(year % 4 == 0 && year % 100 != 0)
            leapYear = true;

    }
        return leapYear;
  } 

}

我认为我正确地完成了Date.java类,我的问题在于主类。我们必须创建一个数组,将日期增加1.我试图每月查看30-31天。

这是DateTest主页:

 package date;
 import java.text.SimpleDateFormat;
 import java.util.Date;


 import javax.swing.JOptionPane;
 public class DateTest {


 public static void main(String[] args) 
 {
    Date [] date = new Date[2];

    for(int i = 1; i < date.length; i++)
    {
        JOptionPane.showInputDialog("Enter Date (e.g. 19991231): ");
        date[i] = new Date();
        i++;
    }
    new SimpleDateFormat("EEEE, MMMM d, yyyy");
    JOptionPane.showMessageDialog(null, "Date: " + date, "Results", JOptionPane.PLAIN_MESSAGE);
 }
}

我确信我的主要是非常高兴。而且我真的迷失在任务上。

1 个答案:

答案 0 :(得分:0)

我评论说,将日期内部存储为基准日期之后的天数将更容易编码。

这是Date类的一个版本,它在内部将日期存储为基准日期之后的天数:

package com.ggl.testing;

import java.security.InvalidParameterException;

public class Date {

    private static final boolean DEBUG = false;

    private static final int BASE_MONTH = 1;
    private static final int BASE_YEAR = 1700;

    private int days; 

    public Date(int month, int day, int year) {
        setDate(month, day, year);
    }

    public void setDate(int month, int day, int year) {
        if ((month >= 1) && (month <= 12)) {            
        } else {
            String s = "Month not between 1 and 12";
            throw new InvalidParameterException(s); 
        }

        if (year >= BASE_YEAR) {
            int temp = calculateMonthDays(month, year);
            if ((day >= 1) && (day <= temp)) {              
            } else {
                String s = "Day not between 1 and " + temp;
                throw new InvalidParameterException(s); 
            }

            int days = calculateYearDays(year); 
            if (DEBUG)  {
                System.out.println(days);
                System.out.println(temp);
            }

            days += temp;
            this.days = days + day;
            if (DEBUG) {
                System.out.println(day);
                System.out.println(this.days);
            }
        } else {
            String s = "Year before " + BASE_YEAR;
            throw new InvalidParameterException(s); 
        }
    }

    public int[] getDate() {
        int days = this.days;
        if (DEBUG) System.out.println(days);

        int year = BASE_YEAR; 
        int decrement = daysInYear(year);
        while (days > decrement) {
            days -= decrement;
            decrement = daysInYear(++year);
        }
        if (DEBUG) System.out.println(days);

        int month = BASE_MONTH;
        decrement = daysInMonth(month, year);
        while (days > decrement) {
            days -= decrement;
            decrement = daysInMonth(++month, year);
        }
        if (DEBUG) System.out.println(days);

        int day = days;

        int[] result = new int[3];
        result[0] = month;
        result[1] = day;
        result[2] = year;

        return result;
    }

    public String getFormattedDate() {
        String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        int[] fields = getDate();
        return String.format("%s %d, %d", months[fields[0] - 1], 
                fields[1], fields[2]); 
    }

    public void addDays(int increment) {
        this.days += increment;
    }

    private int calculateMonthDays(int month, int year) {
        int days = 0;

        for (int i = BASE_MONTH; i < month; i++) {
            days += daysInMonth(i, year);
        }

        return days;
    }

    private int calculateYearDays(int year) {
        int days = 0;

        for (int i = BASE_YEAR; i < year; i++) {
            days += daysInYear(i);
        }

        return days;
    }

    private int daysInMonth(int month, int year) {
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        if (month == 2) {
            if (daysInYear(year) > 365) {
                return days[month - 1] + 1;
            } 
        }

        return days[month - 1];
    }

    private int daysInYear(int year) {
        int days = 365;

        if ((year % 4) == 0) {
            if ((year % 100) == 0) {
                if ((year % 400) == 0) {
                    days++;
                }
            } else {
                days++;
            }
        }

        return days;
    }

    public static void main(String[] args) {
        Date date = new Date(6, 12, 2014);
        displayResult(date, 10);

        date = new Date(6, 12, 2014);
        displayResult(date, 20);

        date = new Date(12, 15, 2014);
        displayResult(date, 20);

        date = new Date(12, 15, 1955);
        displayResult(date, 30);

        date = new Date(12, 15, 1955);
        displayResult(date, -30);

        date = new Date(12, 15, 1955);
        displayResult(date, 16);

        date = new Date(12, 15, 1955);
        displayResult(date, 17);

        date = new Date(12, 15, 1955);
        displayResult(date, 80);

        date = new Date(12, 15, 1956);
        displayResult(date, 80);

        date = new Date(3, 5, 1957);
        displayResult(date, -80);

        date = new Date(3, 5, 1956);
        displayResult(date, -80);
    }

    private static void displayResult(Date date, int increment) {
        String sign = "";
        int display = 0;

        if (increment > 0) {
            sign = " + ";
            display = increment;
        } else {
            sign = " - ";
            display = -increment;
        }

        System.out.print(date.getFormattedDate() + sign +
                display + " days -> ");
        date.addDays(increment);
        System.out.println(date.getFormattedDate());
    }

}

以下是一些测试结果。

Jun 12, 2014 + 10 days -> Jun 22, 2014
Jun 12, 2014 + 20 days -> Jul 2, 2014
Dec 15, 2014 + 20 days -> Jan 4, 2015
Dec 15, 1955 + 30 days -> Jan 14, 1956
Dec 15, 1955 - 30 days -> Nov 15, 1955
Dec 15, 1955 + 16 days -> Dec 31, 1955
Dec 15, 1955 + 17 days -> Jan 1, 1956
Dec 15, 1955 + 80 days -> Mar 4, 1956
Dec 15, 1956 + 80 days -> Mar 5, 1957
Mar 5, 1957 - 80 days -> Dec 15, 1956
Mar 5, 1956 - 80 days -> Dec 16, 1955

使用相同的私有方法将日期转换为整数,并将整数转换为日期有助于一些。 DEBUG显示帮助我解决了其他错误。