SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //获取格式化格式
Date nowtime = new Date(); //获取当前日期
Calendar c = Calendar.getInstance();
c.setTime(nowtime); //塞入当前日期
c.set(Calendar.HOUR_OF_DAY, 0); //将时分秒设置成0,便于格式获取,若格式的为yyyy-MM-dd则不需要
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
//获取今天0点以后的
Date time = c.getTime();
String needtime = format.format(time);
//获取三天前时间戳
c.add(Calendar.DATE, -3);
Date time = c.getTime();
String needtime = format.format(time);
//获取一周前时间戳
c.add(Calendar.DATE, -7);
Date time = c.getTime();
String needtime = format.format(time);
//获取一个月前
c.add(Calendar.MONTH, -1);
Date time = c.getTime();
String needtime = format.format(time);
//获取三个月前
c.add(Calendar.MONTH, -3);
Date time = c.getTime();
String needtime = format.format(time);
版权声明:本文为CSDN博主「江湖有个李半仙」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/LLnobug/article/details/83338544