Java 当前时间的上一个月时间、上一年时间
- 获取当前时间
/** * 获取当前时间 * @return Timestamp * @author zhangyao * date: 2018/8/22 11:36 */ public static Timestamp now() { return new Timestamp(Calendar.getInstance().getTimeInMillis()); }
- 上个月、上一年;time可以用获取当前时间的值
/** * 上一个月 */ private Timestamp lastMonthTime(Timestamp time){ Calendar c = Calendar.getInstance(); c.setTime(time); c.add(Calendar.MONTH, -1); return new Timestamp(c.getTimeInMillis()); } private Timestamp lastYearTime(Timestamp time){ //过去一年 Calendar c = Calendar.getInstance(); c.setTime(time); c.add(Calendar.YEAR, -1); return new Timestamp(c.getTimeInMillis()); }