您不应该发送Date
像String
(Tue Dec 13 00:00:00 CST 2022)这样的对象,而是获取一个Calendar
实例,在那里设置日期并Date
从那里发送。
小例子:
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2022);
c.set(Calendar.MONTH, Calendar.DECEMBER);
c.set(Calendar.DATE, 13);
Event h1 = new Event(c.getTime(), "New Year's Day", "New Year's Day", "Holiday");
vEvents.add(h1);
为了不重写所有Calendar
代码,您可以创建一个实用方法来获取Date
实例:
public class Utils {
public static Date getDate(int day, int month, int year) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
c.set(Calendar.DATE, day);
return c.getTime();
}
}
//now calling this utility method in your code
Event h1 = new Event(Utils.getDate(13, 12, 2022), "New Year's Day", "New Year's Day", "Holiday");
vEvents.add(h1);
作为更高级(和现实世界的应用程序)的解决方案,您应该按照 Makoto 的回答中所述使用 Joda Time。