尝试比较日期时发生nullpointerexception。我注意到在调试时,expecteddate和arriveddate变量值正在获取当前日期时间,因为我正在使用set方法设置日期。请更正用于比较日期的代码。我的方法是用来确定货物是否会准时到达,是在预期的时间之前还是之后。
public class ShipmentBO {
public void displayStatusOfShipment(Shipment shipment) {
Date expectedDate = new Date();
Date arrivedDate = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Shipment s = new Shipment();
ShipmentStatus SStatus = new ShipmentStatus();
expectedDate = s.getexpectedDeliveryDate();
arrivedDate = SStatus.getarrivedDate();
String s1 = df.format(expectedDate);
String s2 = df.format(arrivedDate);
if (expectedDate.after(arrivedDate)) {
System.out.println("The shipment arrived after the expected date");
} else
if (expectedDate.before(arrivedDate)) {
System.out.println("The shipment arrived before the expected date");
}
}
我把日期定在下面的主课上
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the shipment details :");
String userDetail = sc.nextLine();
String userDetailParts[] = userDetail.split(",");
//System.out.println(Arrays.toString(userDetailParts));
Shipment shipment = new Shipment();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
shipment.setid(userDetailParts[0]);
shipment.setsourcePort(userDetailParts[1]);
shipment.setdestinationPort(userDetailParts[2]);
shipment.setexpectedDeliveryDate(sdf.parse(userDetailParts[3]));
shipment.setcustomerName(userDetailParts[4]);
}
我的输入是用逗号分隔的-staju01,香港,cochin,20-05-2017,karthick
装运类别:
import java.util.Date;
public class Shipment {
private String id;
private String sourcePort;
private String destinationPort;
private Date expectedDeliveryDate;
private String customerName;
private ShipmentStatus[] shipmentStatus;
public String getid() {
return id;
}
public void setid(String id) {
this.id = id;
}
public String getsourcePort() {
return sourcePort;
}
public void setsourcePort(String sourcePort) {
this.sourcePort = sourcePort;
}
public String getdestinationPort() {
return destinationPort;
}
public void setdestinationPort(String destinationPort) {
this.destinationPort = destinationPort;
}
public Date getexpectedDeliveryDate() {
return expectedDeliveryDate;
}
public void setexpectedDeliveryDate(Date expectedDeliveryDate) {
this.expectedDeliveryDate = expectedDeliveryDate;
}
public String getcustomerName() {
return customerName;
}
public void setcustomerName(String customerName) {
this.customerName = customerName;
}
public Shipment() {
}
public Shipment(String id, String sourcePort, String destinationPort, Date expectedDeliveryDate,
String customerName) {
this.id = id;
this.sourcePort = sourcePort;
this.destinationPort = destinationPort;
this.expectedDeliveryDate = expectedDeliveryDate;
this.customerName = customerName;
}
public ShipmentStatus[] getShipmentStatus() {
return shipmentStatus;
}
public void setShipmentStatus(ShipmentStatus[] shipmentStatus) {
this.shipmentStatus = shipmentStatus;
}
}
发货状态:
import java.util.Date;
public class ShipmentStatus {
private String arrivalPort;
private String departurePort;
private Date arrivedDate;
private String status;
private Shipment shipment;
public Shipment getshipment() {
return shipment;
}
public void setshipment(Shipment shipment) {
this.shipment = shipment;
}
public String getarrivalPort() {
return arrivalPort;
}
public void setarrivalPort(String arrivalPort) {
this.arrivalPort = arrivalPort;
}
public String getdeparturePort() {
return departurePort;
}
public void setdeparturePort(String departurePort) {
this.departurePort = departurePort;
}
public Date getarrivedDate() {
return arrivedDate;
}
public void setarrivedDate(Date arrivedDate) {
this.arrivedDate = arrivedDate;
}
public String getstatus() {
return status;
}
public void setstatus(String status) {
this.status = status;
}
public ShipmentStatus() {
}
public ShipmentStatus(String arrivalPort, String departurePort, Date arrivedDate, String status,
Shipment shipment) {
this.arrivalPort = arrivalPort;
this.departurePort = departurePort;
this.arrivedDate = arrivedDate;
this.status = status;
this.shipment = shipment;
}
}
3条答案
按热度按时间zengzsys1#
你在打电话给
getexpectedDeliveryDate()
在new Shipment()
,但我怀疑您希望在收到的装运时将其作为参数调用,否则将保持不变。基本上,试着改变expectedDate = s.getexpectedDeliveryDate();
至expectedDate = shipment.getexpectedDeliveryDate();
lh80um4z2#
参见代码中的注解:
在shipping中添加一个方法,该方法包含使用shipmentstatus数组获取装运的arriveddate的逻辑。你说这是最后三次发货状态,所以代码可以是:
我不确定
the 3rd last status
是确定正确装运状态的好方法。它认为最好是根据一些业务逻辑做出选择。例如:b1uwtaje3#
get方法返回空值,即使我已经设置了日期
因为你没有使用
Shipment
对象传递给displayStatusOfShipment(Shipment shipment)
; 而是创建一个新示例,如下所示:应将上述三行全部替换为以下行:
类似地,对于这个方法,您应该传递一个,
ShipmentStatus
对于到达日期,使用相同的方法获取到达日期,即需要以类似的方式替换以下三行,如上图所示:在比较日期时:
我建议你不要再使用设计拙劣的
java.util.Date
转向现代社会java.time
应用程序编程接口。这个场景是一个很好的例子,可以让您立即进行切换。让我们看看原因:现代日期时间api有一个类叫做,
LocalDate
它只表示日期,即只有三个字段:年、月和日,因此当您比较LocalDate
,只比较这些字段。LocalDate
有一套丰富的用于比较的api。LocalDate#isAfter
,LocalDate#isBefore
,LocalDate#isEqual
比较这三个字段的值得到结果。你会这么说的
java.util.Date
也有Date#before
,Date#after
但这就是这些方法与LocalDate
末端。这些方法java.util.Date
比较之后的毫秒数January 1, 1970, 00:00:00 GMT
i、 e.一个Date
可以在之前/之后,甚至相差一毫秒(不同于LocalDate
只有在一个或多个字段(日、月和年)中存在差异时,才可以是before/after。输出:
如果仍然坚持使用java.util.date:
如果你还坚持使用
java.util.Date
,则必须从Date
你自己和他们比较,以确定之前/之后的情况。选择权在你。