0

我正在尝试确保我的 nextday 方法可以在用户输入的日期上添加 1 天,并确保它正确添加 40 天,同时考虑到每月正确的天数和闰年

public nextday() {
    for (int count = 1; count < 40; count++)
        ;
}

public String toDayDateString() // toDayDateString method {
    int countDays = 0;

    for (int i = 1; i < getMonth(); i++) {
        if (i == 2 && checkLeapYr(getYear()))
            countDays += 29;
        else
            countDays += daysPerMonth[i];
    }
    countDays += date;
    String message = String.format("\n", countDays, getYear());
    return message;
}

private Boolean checkLeapYr(int inYear) { // check leap year method.
    if (getYear() % 400 == 0 || (getYear() % 4 == 0 && getYear() % 100 != 0))
        return true;
    else
        return false;
}

下面是一个菜单,应该允许用户选择输入日期或退出,但日期未被正确接受。

{
 public static void main ( String [] args)
// User selects how they will enter the dates
 {      
   +"(1) Enter the date as MM/DD/YYYY\n"
   +"(Any Key) Quit\n";
  int input=0;

  Date newDate;

  do{
   String userInput = JOptionPane.showInputDialog(null, menu);
   input = Integer.parseInt( userInput);
   String outputMessage="";

   if ( input = 1)
   {
    userInput =JOptionPane.showInputDialog(null, "Please enter a date as 02/28/2011");

    switch ( input )  // here is where the menu choice will be evaluated
    {
     case 1 :
      token = userInput.split("/");
      if (token.length == 3 )

      {
       newDate = new Date( Integer.parseInt( token[0]),
        Integer.parseInt( token[1]), Integer.parseInt( token[2]) );
       outputMessage = newDate.toString();
       JOptionPane.showMessageDialog(null, outputMessage);
      }

      break;

     case 2:
  } while ( input <>1); // this will quit the program when user selects any key other than 1 at the menu.
4

3 回答 3

2
  • 您应该使用java.text.DateFormat来解析日期字符串
  • 编写自己的日期算术非常容易出错(闰年只是众多例外中的一个),最好使用java.util.Calendar实现。

至于日期算术,它是如何完成的Calendar

//TODO: parse input string with DateFormat
Date startDate = ... // your parsed date

Calendar cal = new GregorianCalendar();
cal.setTime(startDate);

for (int i = 0; i < 40; i++) {

    // add a day
    cal.add(Calendar.DATE, 1);

    // and get the new result as date
    Date date = cal.getTime();

    System.out.println(date);
}

如果您需要倒数,请添加时间量(天、小时等)。

于 2013-09-24T15:48:20.483 回答
0
    public static void main(String[] args) throws IOException, ParseException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter date(dd/mm/yyyy) : ");
    String input = br.readLine();
    DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
    Calendar cal = Calendar.getInstance();
    Date date = df.parse(input);
    cal.setTime(date);
    cal.add(Calendar.MONTH, 1); // to get actual month,as it parses 02 as Jan.
    for (int i = 1; i < 40; i++) {
        System.out.println(" day " + i + " : " + cal.getTime());
        cal.add(Calendar.DATE, 1);
    }
}
于 2013-09-27T04:19:56.450 回答
0
    public static void main(String[] args) throws IOException,ParseException{
                BufferedReader br = new BufferedReader(new   InputStreamReader(System.in));       
                System.out.print("Enter date(dd/mm/yyyy) : ");
                String input = br.readLine();
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                Calendar cal = Calendar.getInstance();
                Date date = df.parse(input);
                cal.setTime(date);
                for (int i = 1; i <=40; i++) {
                    cal.add(Calendar.DATE,1);
                    System.out.println(" day " + i + " : " + cal

.getTime());

            }
        }
于 2015-03-05T09:33:45.363 回答