Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的程序出现java.time.LocalDateTime错误_Java_Datetime_Java Time_Datetime Comparison - Fatal编程技术网

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

我的程序出现java.time.LocalDateTime错误,java,datetime,java-time,datetime-comparison,Java,Datetime,Java Time,Datetime Comparison,我的代码没有显示正确或所需的输出,因此出现问题。这是我写的代码 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,

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

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);
    }
}
在我的isExpired()方法中,应该检查当前日期是否晚于ID的过期日期。如果ID过期,则应打印为true,否则应打印为false。出于某种原因,当程序检查的三个ID应该为false-true-false时,我得到了所有false。下面是我的测试文件

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();
   }
}
这里还有我目前得到的输出

Name: John Smith
ID: 192891
Expiration: 6/21/2018
Expired? false

Name: Jennifer Brown
ID: 728828
Expiration: 5/31/2017
Expired? false

Name: Britney Wilson
ID: 592031
Expiration: 7/15/2019
Expired? false
您只需为
DriversLicense
类使用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()

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

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

为了说明在比较哪些字段时,以下是一个表:

+==================================================+================+
|执行的测试|结论|
+================+=================================+================+
|第一年<第二年|
+----------------+------------------+              |                |
|| month1第2天||
|                +------------------+--------------+                |
|| month1>month2 | Date1>Date2|
+----------------+------------------+              |                |
|第一年>第二年|
+----------------+---------------------------------+----------------+
因此,正如您所看到的,您的测试非常不完整:

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

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

LocalDate
LocalDateTime
中已经有一些方法可以为您执行此操作。为什么你觉得有必要在这里重新发明轮子?另外,请看看这个网站是如何运作的,这里有哪些问题,以及你相应的问题。另请参见:仔细查看您的逻辑条件。您需要在iExpired方法中使用几个嵌套的if语句读取
if(year>this.expYear&&month>this.expMonth&&day>this.expDay)
line我知道,您的许可证只有在年、月、日(全部三个)到期时才有效晚于
LocalDateTime.now()
和部分时间,但许可证不仅可以在超过日期一年、一个月和一天时过期,还可以在短于一年、一个月和一天的时间跨度内过期。因此,您的
isExpired
应该得到比此检查更多的检查。正如其他人所建议的,您应该使用
LocalDate
LocalDateTime
提供的方法来检查一个日期是否晚于其他日期。如果
expiryDate
在过去,即
expiryDate
在今天之前,又称今天在
expiryDate
之后,又称
currentDate.isAfter,是否会过期(expiryDate)
?回答正确。现在我们更多地使用标准类,我们还可以使用
日期时间格式设置日期格式,而不是自己连接字符串。