gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:35:31 27 4
gpt4 key购买 nike

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

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

最佳答案

您可以简单地使用 LocalDate 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()

此外,使用一些日志记录框架(如 log4j)删除您的 System.out.println()应用程序接口(interface)。

关于我的程序出现 java.time.LocalDateTime 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48495734/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com