java获取时间全面
//当前时间
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
//当前时间
String ldate=dateFormat.format(date);
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);//设置为上个月
Date lastMdate = calendar.getTime();
//上个月时间
String lastdate=dateFormat.format(lastMdate);
Calendar calendar1=Calendar.getInstance();
calendar1.setTime(date);
calendar1.set(Calendar.MONTH,calendar1.get(Calendar.MONTH)-3);//设置为3个月前时间
Date lastTMdate = calendar1.getTime();
//3个月前时间
String lastTdate=dateFormat.format(lastTMdate);
Calendar calendar8=Calendar.getInstance();
calendar8.setTime(date);
calendar8.set(Calendar.MONTH,calendar1.get(Calendar.MONTH)-4);//设置为4个月前时间
Date lastFMdate = calendar8.getTime();
//4个月前时间
String lastFdate=dateFormat.format(lastTMdate);
Calendar calendar2=Calendar.getInstance();
calendar2.setTime(date);
calendar2.set(Calendar.MONTH,calendar2.get(Calendar.MONTH)-2);//设置为2个月前时间
Date lastTwMdate = calendar2.getTime();
//2个月前时间
String lastTwdate=dateFormat.format(lastTwMdate);
Calendar calendar3=Calendar.getInstance();
calendar3.setTime(date);
calendar3.set(Calendar.MONTH,calendar3.get(Calendar.MONTH)-6);//设置为6个月前时间
Date lastSMdate = calendar3.getTime();
//6个月前时间
String lastSdate=dateFormat.format(lastSMdate);
Calendar calendar9=Calendar.getInstance();
calendar9.setTime(date);
calendar9.set(Calendar.MONTH,calendar3.get(Calendar.MONTH)-7);//设置为7个月前时间
//7个月前时间
String lastSevenSdate=dateFormat.format(calendar9.getTime());
//获取本月1号的时间
Calendar calendar4=Calendar.getInstance();
calendar4.setTime(date);
calendar4.set(Calendar.DAY_OF_MONTH, 1);
String localMfirst=dateFormat.format(calendar4.getTime());
//获取上上个月1号
Calendar calendar5=Calendar.getInstance();
calendar5.setTime(lastTwMdate);
calendar5.set(Calendar.DAY_OF_MONTH, 1);//设置为2个月前1号
String lastMfirst=dateFormat.format(calendar5.getTime());
//获取上个月1号
Calendar calendar7=Calendar.getInstance();
calendar7.setTime(lastMdate);
calendar7.set(Calendar.DAY_OF_MONTH, 1);
String lastfirst=dateFormat.format(calendar7.getTime());
//获取3个月前1号
Calendar calendar10=Calendar.getInstance();
calendar10.setTime(lastTMdate);
calendar10.set(Calendar.DAY_OF_MONTH, 1);
String lastThfirst=dateFormat.format(calendar10.getTime());
//获取6个月前的1号
Calendar calendar6=Calendar.getInstance();
calendar6.setTime(lastSMdate);
calendar6.set(Calendar.DAY_OF_MONTH, 1);//设置为6个月前1号
String lastSixfirst=dateFormat.format(calendar6.getTime());
//获取7个月前的1号
Calendar calendar11=Calendar.getInstance();
calendar11.setTime(calendar9.getTime());
calendar11.set(Calendar.DAY_OF_MONTH, 1);//设置为7个月前1号
String lastSevenfirst=dateFormat.format(calendar11.getTime());
浙公网安备 33010602011771号