Java获取当前时间的下一天以及30天前的时间

有这样的一个需求。需要得到当前时间的下一天以及30前的时间。在计算30天前的时间时出现了一点问题,时间出错,但是公式没有出错,后来才发现是运算超出了Integer的范围。
( 24 * 60 * 60*1000) 这种计算表达式在 Java中是默认以 Integer类型来的,若不超过 Integer的最大值则没有问题,若超过则必须加上L (  24 * 60 * 60*1000L) 
Date date1=new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
System.out.println(date1);
 
// 获取当前时间的30天前的时间
// 如果运算中不加L的话会出错
 
Date date2=new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000 * 30L);
System.out.println(date2);

 

posted @ 2025-11-04 22:18  培轩  阅读(3)  评论(0)    收藏  举报