SmartGwt:添加日期
SmartGwt: add days to date
如何在 SmartGwt 中为日期添加天数。我找到了this question,发现我可以使用CalendarUtil.addDaysToDate(dateToAddTo, daysToAddToDateAsInteger));
但是addDaysToDate()
是static void。如果一个方法没有 return 任何东西,它可以 "add days to a date" 的意义何在?
如何使用此方法?我想做这样的事情。
Date newDate = dateToAddTo + daysToAddToDate;
这是我的代码的简化版本。
if (listGridRecord.getAttribute("expirationDays") != null) {
CalendarUtil.addDaysToDate((Date) endDate.getValue(), Integer.parseInt(listGridRecord.getAttribute("expirationDays")));
listGridRecord.setAttribute("expirationDate", (Date) endDate.getValue());
} else {
listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}
这里是 link 到 javadocs
此方法更改作为参数传递的对象。
Date date = new Date();
long checkBeforeChange = date.getTime();
CalendarUtil.addDaysToDate(date, 1);
long checkAfterChange = date.getTime();
if(checkBeforeChange != checkAfterChange)
Window.alert("It works ;)");
你的代码应该是这样的:
if (listGridRecord.getAttribute("expirationDays") != null) {
Date tmpDate = endDate.getValue();
CalendarUtil.addDaysToDate(tmpDate, Integer.parseInt(listGridRecord.getAttribute("expirationDays")));
listGridRecord.setAttribute("expirationDate", tmpDate);
} else {
listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}
当执行 (Date) endDate.getValue()
时,您会得到 Date
对象的副本,因此您看不到任何变化。
在@Adam 的帮助下,我弄清楚了我做错了什么。我创建了一个名为 expireationDate 的新 Date 变量并将其设置为 (Date) endDate.getValue();之后我用它来做计算。
if (listGridRecord.getAttribute("expirationDays") != null) {
Date expirationDate = (Date) endDate.getValue();
CalendarUtil.addDaysToDate(expirationDate, Integer.parseInt(listGridRecord.getAttribute("expirationDays")));
listGridRecord.setAttribute("expirationDate", expirationDate);
} else {
listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}
首先,您可以将所有需要的实用方法包装在您自己的实用程序 class(私有构造函数)中,例如MyDateUtilsClassName.addDays(Date, int) 将 return 新实例,保留参数不变。
当涉及到日期操作时,在 Gwt 中你可以使用标准的 java.util.Date 方法,甚至是那些已弃用的方法,如 setMinutes、setHours 等。即使你看到 com.google.gwt.user.datepicker.client.CalendarUtil 方法,它们也在那里使用.
如果它不是服务器端,而是客户端,你不应该太在意这些方法上的@Deprecated。无论如何,Gwt 都会将它们编译为 javascript。你应该知道 java.util.Calendar。据我所知,完全不支持。
如何在 SmartGwt 中为日期添加天数。我找到了this question,发现我可以使用CalendarUtil.addDaysToDate(dateToAddTo, daysToAddToDateAsInteger));
但是addDaysToDate()
是static void。如果一个方法没有 return 任何东西,它可以 "add days to a date" 的意义何在?
如何使用此方法?我想做这样的事情。
Date newDate = dateToAddTo + daysToAddToDate;
这是我的代码的简化版本。
if (listGridRecord.getAttribute("expirationDays") != null) {
CalendarUtil.addDaysToDate((Date) endDate.getValue(), Integer.parseInt(listGridRecord.getAttribute("expirationDays")));
listGridRecord.setAttribute("expirationDate", (Date) endDate.getValue());
} else {
listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}
这里是 link 到 javadocs
此方法更改作为参数传递的对象。
Date date = new Date();
long checkBeforeChange = date.getTime();
CalendarUtil.addDaysToDate(date, 1);
long checkAfterChange = date.getTime();
if(checkBeforeChange != checkAfterChange)
Window.alert("It works ;)");
你的代码应该是这样的:
if (listGridRecord.getAttribute("expirationDays") != null) {
Date tmpDate = endDate.getValue();
CalendarUtil.addDaysToDate(tmpDate, Integer.parseInt(listGridRecord.getAttribute("expirationDays")));
listGridRecord.setAttribute("expirationDate", tmpDate);
} else {
listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}
当执行 (Date) endDate.getValue()
时,您会得到 Date
对象的副本,因此您看不到任何变化。
在@Adam 的帮助下,我弄清楚了我做错了什么。我创建了一个名为 expireationDate 的新 Date 变量并将其设置为 (Date) endDate.getValue();之后我用它来做计算。
if (listGridRecord.getAttribute("expirationDays") != null) {
Date expirationDate = (Date) endDate.getValue();
CalendarUtil.addDaysToDate(expirationDate, Integer.parseInt(listGridRecord.getAttribute("expirationDays")));
listGridRecord.setAttribute("expirationDate", expirationDate);
} else {
listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}
首先,您可以将所有需要的实用方法包装在您自己的实用程序 class(私有构造函数)中,例如MyDateUtilsClassName.addDays(Date, int) 将 return 新实例,保留参数不变。
当涉及到日期操作时,在 Gwt 中你可以使用标准的 java.util.Date 方法,甚至是那些已弃用的方法,如 setMinutes、setHours 等。即使你看到 com.google.gwt.user.datepicker.client.CalendarUtil 方法,它们也在那里使用.
如果它不是服务器端,而是客户端,你不应该太在意这些方法上的@Deprecated。无论如何,Gwt 都会将它们编译为 javascript。你应该知道 java.util.Calendar。据我所知,完全不支持。