如何在不使用SimpleDataFormat或Calendar的情况下获取比当前日期早一年的java.util.Date
我有当前日期,我只想使用如何在不使用SimpleDataFormat或Calendar的情况下获取比当前日期早一年的java.util.Date,java,date,Java,Date,我有当前日期,我只想使用java.util.date 我在gwt工作,因此无法使用SimpleDataFormat或日历 Date currentDate = new Date(); Date oneYearBefore = new Date( - (365 * 24 * 60 * 60 * 1000)); 上面提到的代码不起作用(从某个论坛获得)使用日历类 Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.YE
java.util.date
我在gwt工作,因此无法使用SimpleDataFormat
或日历
Date currentDate = new Date();
Date oneYearBefore = new Date( - (365 * 24 * 60 * 60 * 1000));
上面提到的代码不起作用(从某个论坛获得)使用日历类
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
System.out.println(calendar.getTime());
使用日历类
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
System.out.println(calendar.getTime());
为什么不能使用日历?我想你可能误解了什么 无论如何:
Date oneYearBefore = new Date( System.currentTimeMillis() - (365 * 24 * 60 * 60 * 1000));
或者使用从论坛粘贴的代码:
Date currentDate = new Date();
Date oneYearBefore = new Date( currentDate.getTime() - (365 * 24 * 60 * 60 * 1000));
为什么不能使用日历?我想你可能误解了什么 无论如何:
Date oneYearBefore = new Date( System.currentTimeMillis() - (365 * 24 * 60 * 60 * 1000));
或者使用从论坛粘贴的代码:
Date currentDate = new Date();
Date oneYearBefore = new Date( currentDate.getTime() - (365 * 24 * 60 * 60 * 1000));
仅使用
java.util.Date
,您可以使用:
Date oneYearBefore = new Date(currentDate.getTime() - (365L * 24L * 60L * 60L * 1000L));
但请记住,这并不能解释潜在的闰年。仅使用
java.util.Date
,您可以使用:
Date oneYearBefore = new Date(currentDate.getTime() - (365L * 24L * 60L * 60L * 1000L));
但请记住,这并不能解释潜在的闰年。您使用的是所有
int的
,当您将它们相乘时,您将得到一个int。您将该int转换为long,但只有在int相乘已经导致错误答案之后。
实际上,它溢出了int类型
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println(currentDate);
long milliseconds = (long) 365 * 24 * 60 * 60 * 1000;
Date oneYearBefore = new Date(currentDate.getTime() - milliseconds);
System.out.println(oneYearBefore);
}
Mon Nov 17 13:11:10 IST 2014
Sun Nov 17 13:11:10 IST 2013
您正在使用所有的
int
,当您将它们相乘时,您将得到一个int。您正在将该int转换为long,但只有在int相乘已经导致错误答案之后。
实际上,它溢出了int类型
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println(currentDate);
long milliseconds = (long) 365 * 24 * 60 * 60 * 1000;
Date oneYearBefore = new Date(currentDate.getTime() - milliseconds);
System.out.println(oneYearBefore);
}
Mon Nov 17 13:11:10 IST 2014
Sun Nov 17 13:11:10 IST 2013
该表达式将使int数据类型溢出。尝试使用长常量。(例如365L*24L*60L…)。此外,您还没有考虑夏时制或闰秒。它们对您的用例重要吗?这会给您带来很多麻烦,因为您还必须检查闰年等。该表达式的可能重复项将溢出int数据类型。尝试使用长常量。(例如365L*24L*60L…)。此外,您还没有考虑夏时制或闰秒。它们对您的用例重要吗?这会给您带来很大的痛苦,因为您还必须检查闰年等。一年的可能重复并不总是365次24小时。你愿意给我们一个很好的实施公历吗?在大多数应用程序中,这已经足够了。我想在gwt中,我不允许使用日历。它说在运行时找不到java.util.Calendar的源代码。这个util怎么样,也许它会有用(那么它只有-365天)?一年并不总是365乘以24小时。你愿意给我们一个很好的实施公历吗?在大多数应用程序中,这已经足够了。我想在gwt中,我不允许使用日历。它说在运行时找不到java.util.Calendar的源代码。这个util怎么样,也许它会有用(那么它只有-365天)?这里有一个bug,应该是:长毫秒=(长)365*24*60*60*1000;Date oneYearBefore=新日期(currentDate.getTime()-毫秒);这里有一个bug,应该是:长毫秒=(长)365*24*60*60*1000;Date oneYearBefore=新日期(currentDate.getTime()-毫秒);