1.获取某个时间一个月前的时间
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200411230632836.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lfeWFuZ2hhbw==,size_16,color_FFFFFF,t_70#pic_center)
1.1 代码如下,可以修改(302460601000)获取得到任何时间
import java.sql.Timestamp;
import java.util.Date;
public class Test3 {
public static void main(String[] args) {
Timestamp timestamp=new Timestamp(new Date().getTime());
System.out.println("现在的时间是:"+timestamp);
/*一个月前的时间*/
Timestamp timestamp1=new Timestamp(new Date().getTime()-30*24*60*60*1000);
System.out.println("一个月前的时间:"+timestamp1);
/*当初到这的时候一直出现错误,过了一段时间才想起来时数字太大溢出了*/
System.out.println("对比20天一下时间:"+20*24*60*60*1000);
System.out.println("对比30天一下时间:"+30*24*60*60*1000);
/*这是因为int(-2147483648---2147483647) 而30*24*60*60*1000=2592000000 数字溢出了*/
System.out.println("30天后面加了一个L:"+30*24*60*60*1000L);
/*现在再看一下30天前的时间*/
Timestamp timestamp2=new Timestamp(new Date().getTime()-30*24*60*60*1000L);
System.out.println("现在时间的30天前:"+timestamp2);
}
}
2.用Calendar 获取时间
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020041123162424.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lfeWFuZ2hhbw==,size_16,color_FFFFFF,t_70#pic_center)
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarTest {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd ");
Calendar c = Calendar.getInstance();
System.out.println("现在的时间:"+format.format(new Date()));
//过去七天
c.setTime(new Date());
c.add(Calendar.DATE, - 7);
Date d = c.getTime();
String day = format.format(d);
System.out.println("过去七天:"+day);
//截至本年
c.setTime(new Date());
c.set(Calendar.DAY_OF_MONTH,1); //将日期设置为1号
c.set(Calendar.MONTH,0); //将月份设置为1月
Date dD1 = c.getTime();
String day2 = format.format(dD1);
System.out.println("截至本年:"+day2);
/*截至本月*/
c.setTime(new Date());
c.set(Calendar.DAY_OF_MONTH,1);
c.set(Calendar.HOUR_OF_DAY,0);
Date dD = c.getTime();
String day1 = format.format(dD);
System.out.println("截至本月:"+day1);
//过去一月
c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date m = c.getTime();
String mon = format.format(m);
System.out.println("过去一个月:"+mon);
//过去三个月
c.setTime(new Date());
c.add(Calendar.MONTH, -3);
Date m3 = c.getTime();
String mon3 = format.format(m3);
System.out.println("过去三个月:"+mon3);
//过去一年
c.setTime(new Date());
c.add(Calendar.YEAR, -1);
Date y = c.getTime();
String year = format.format(y);
System.out.println("过去一年:"+year);
}
}
3.比如现在的时间是2020-04-11 23:20:00 ,那么有的需求是获取截止到目前为止本月的数据,那么开始时间就是2020-04-01 00:00:00,那么还有的要求比如说截止到目前每个月的开始与结束时间(客户要求你按每个月的话)。直接上代码了吧。先看一下效果图。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200411234007211.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lfeWFuZ2hhbw==,size_16,color_FFFFFF,t_70#pic_center)
3.1代码如下 其实原理很简单
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Test7 {
public static void main(String[] args) {
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time="2020";
List<Map> list=getMonths1(time);
for(Map l:list){
System.out.println(dateFormat2.format(l.get("first")));
System.out.println(dateFormat2.format(l.get("last")));
System.out.println(l.get("month"));
System.out.println("------------------------");
}
}
/*封装的一个方法*/
public static List<Map> getMonths1(String start1) {
List<Map> res=new ArrayList<Map>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
String endTime=dateFormat2.format(date);
String startTime=start1+"-01-01 00:00:00";
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.MONTH,1);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
String month=dateFormat.format(tempStart.getTime());
tempStart.set(Calendar.DAY_OF_MONTH, 1);
String first=dateFormat3.format(tempStart.getTime());
tempStart.set(Calendar.DAY_OF_MONTH, tempStart.getActualMaximum(Calendar.DAY_OF_MONTH));
String last=dateFormat3.format(tempStart.getTime());
Map<String,Object> map=new HashMap<String,Object>();
/*直接将month转换为月份*/
String s=month.substring(5,6);
String ss=month.substring(6);
String sss=month.substring(5);
if(s.equals("0")){
month=ss+"月";
}else{
month=sss+"月";
}
map.put("month", month);
map.put("first", dateFormat2.parse(first+" 00:00:00"));
map.put("last", dateFormat2.parse(last+" 23:59:59"));
res.add(map);
tempStart.add(Calendar.MONTH,1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return res;
}
}
本文链接
https://blog.csdn.net/y_yanghao/article/details/105461914