如何使用spring boot在json对象中表示日历日期

问题描述 投票:0回答:1

我有以下json对象:

{
"username":"test",
"customerName":"test",
"accessReasons":"tes",
"leaseHours":"8"
}

这表示某个用户必须执行某项任务的时间(即sysdate + leaseHours)

在我的控制器上,我创建了一个对象,其中包含我在json中收到的信息:

thatObject obj=new thatObject (jsonObjectFromPostBody)

通过这样做,以这种方式创建了该对象的实例:

public thatObject(thatObject aci) {
    this.username=aci.getUsername();
    this.customerName=aci.getCustomerName();
    this.accessReasons=aci.getAccessReasons();
    this.beginDate= new GregorianCalendar();
    long sum=beginDate.getTimeInMillis()+aci.getLeaseHours().getTimeInMillis();
    this.endDate=(Calendar)beginDate.clone();
    this.endDate.setTimeInMillis(sum);
}

如您所见,我的endDate是初始日期+用户有权激活的时间(在这种情况下是8小时)

但我的问题是我正在使用日历而且我无法在我的json对象上代表那些8小时,所以当我计算endTime时它将为0,我如何在我的json上代表那些8小时或者将这8小时加到我的beginDate上变量?

java spring spring-boot calendar
1个回答
0
投票

为什么不直接在该对象中将leaseHours属性的类型更改为Integer?在这种情况下,您可以执行以下操作:

long sum=beginDate.getTimeInMillis() + aci * 60 * 60 * 1000;
© www.soinside.com 2019 - 2024. All rights reserved.