我的程序出现java.time.LocalDateTime错误

时间:2018-01-29 06:44:19

标签: java datetime java-time datetime-comparison

我的代码没有显示正确或所需的输出问题。这是我写的代码。

address

在我的 isExpired()方法中,应该检查天气,当前日期晚于ID的到期日期。如果ID过期,则应打印出true,否则应打印出false。由于某些原因,我得到的都是假的,因为我的程序检查的三个id应该是假的。这是我下面的测试文件。

import java.time.LocalDateTime;
public class DriversLicense
{
   private String name;
   private int id;
   private int expYear;
   private int expMonth; 
   private int expDay;


    public DriversLicense(String name, int id, int expYear, int expMonth, int expDay)
    {
        this.name = name;
        this.id = id;
        this.expYear = expYear;
        this.expMonth = expMonth;
        this.expDay = expDay;
    }

    public boolean isExpired()
    {
        LocalDateTime date = LocalDateTime.now();

        boolean tORf = false;

        int year = date.getYear();
        int month = date.getMonthValue();
        int day = date.getDayOfMonth();

        if(year > this.expYear && month > this.expMonth && day > this.expDay)
        {
            return true;
        }
        return tORf;
    }

    public void displayInfo()
    {
        System.out.println("Name: " + this.name);
        System.out.println("ID: " + this.id);
        System.out.println("Expiration: " + this.expYear + "/" + this.expMonth + "/" + this.expDay);
    }
}

这也是我目前得到的输出。

public class TestDL
{
   public static void main(String[] args)
   {
      DriversLicense dr1 = new DriversLicense("John Smith", 192891, 6, 21, 2018);
      dr1.displayInfo();
      System.out.println("Expired? " + dr1.isExpired());
      System.out.println();


      DriversLicense dr2 = new DriversLicense("Jennifer Brown", 728828, 5, 31, 2017);
      dr2.displayInfo();
      System.out.println("Expired? " + dr2.isExpired());
      System.out.println();

      DriversLicense dr3 = new DriversLicense("Britney Wilson", 592031, 7, 15, 2019);
      dr3.displayInfo();
      System.out.println("Expired? " + dr3.isExpired());
      System.out.println();
   }
}

2 个答案:

答案 0 :(得分:5)

您只需为DriversLicense课程使用LocalDate API,如下所示:

public class DriversLicense {

   private String name;

   private int id;

   private LocalDate expiryDate;

   public DriversLicense(String name,int id,int expYear,int expMonth,int expDay) {
      this.name = name;
      this.id = id;
      this.expiryDate = LocalDate.of(expYear, expMonth, expDay);
   }

   public boolean isExpired() {
      LocalDate currentDate = LocalDate.now();
      return currentDate.isAfter(expiryDate);
   }

   public void displayInfo() {
      System.out.println("Name: " + this.name);
      System.out.println("ID: " + this.id);
      System.out.println("Expiration: " + 
         expiryDate.getYear() + "/" + expiryDate.getMonth() + "/" + 
          expiryDate.getDayOfMonth());
   }

   @Override
   public String toString() {
      return "name=" + name + ", id=" + id + ", expiryDate=" + expiryDate;
   }
}

作为旁注,请记住,如果您想查看对象内的值,可以使用上面显示的toString()

另外,请使用log4j api等一些日志框架删除System.out.println()

答案 1 :(得分:1)

当您比较多个“嵌套”值(如3个日期字段)时,您应该只比较年份值相等时的月份值,因为当年份不同时,月份比较没有意义。

为了说明何时比较哪些字段,这是一个表:

+==================================================+================+
|                 Tests to perform                 |   Conclusion   |
+================+=================================+================+
| year1 < year2  |                                 |                |
+----------------+------------------+              |                |
|                | month1 < month2  |              | Date1 < Date2  |
|                +------------------+--------------+                |
|                |                  | day1 < day2  |                |
|                |                  +--------------+----------------+
| year1 == year2 | month1 == month2 | day1 == day2 | Date1 == Date2 |
|                |                  +--------------+----------------+
|                |                  | day1 > day2  |                |
|                +------------------+--------------+                |
|                | month1 > month2  |              | Date1 > Date2  |
+----------------+------------------+              |                |
| year1 > year2  |                                 |                |
+----------------+---------------------------------+----------------+

正如您所看到的,您的测试非常不完整:

year > this.expYear && month > this.expMonth && day > this.expDay

我会让你自己编写正确的测试代码,虽然将3 expXxx个字段组合到构造函数中的LocalDate对象会更容易,因为LocalDate个对象知道如何进行这种比较。