根据当前日期,获取某个日期区间
/**
* 根据当前日期获得最近n周的日期区间(包含本周)
* @param n
* @param sdf
* @return
*/
public static String getNWeekTimeInterval(int n, SimpleDateFormat sdf,SimpleDateFormat sdf1) {
String beginDate = getFromToDate(sdf, new Date(), n, 0, 0);
String endDate = getFromToDate(sdf1, new Date(), n, 1, 0);
return beginDate + "," + endDate;
}
/**
* 根据当前日期获得最近n周的日期区间(不包含本周)
* @param n
* @param sdf
* @return
*/
public static String getNWeekTimeIntervalTwo(int n, SimpleDateFormat sdf,SimpleDateFormat sdf1) {
String beginDate = getFromToDate(sdf, new Date(), n, 0, 1);
String endDate = getFromToDate(sdf1, new Date(), n, 1, 1);
return beginDate + "," + endDate;
}
/**
* 根据当前日期获得本周的日期区间(本周周一和周日日期)
* @param sdf
* @return
*/
public static String getThisWeekTimeInterval(SimpleDateFormat sdf,SimpleDateFormat sdf1) {
return getNWeekTimeInterval(1, sdf,sdf1);
}
/**
* 根据当前日期获得上周的日期区间(上周周一和周日日期)
* @param sdf
* @return
*/
public static String getLastWeekTimeInterval(SimpleDateFormat sdf,SimpleDateFormat sdf1) {
return getNWeekTimeIntervalTwo(1, sdf,sdf1);
}
/**
* 获取本月的第一天
*/
public static String getThisMonthStart(SimpleDateFormat sdf){
Calendar calendar = Calendar.getInstance();//日历对象
calendar.set(Calendar.DAY_OF_MONTH,1);
String s = sdf.format(calendar.getTime());
return s;
}
/**
* 获取本月的最后一天
*/
public static String getThisMonthEnd(SimpleDateFormat sdf){
Calendar calendar1 = Calendar.getInstance();//日历对象
calendar1.set(Calendar.DAY_OF_MONTH,calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
String s = sdf.format(calendar1.getTime());
return s;
}
/**
* 获取上月的第一天
*/
public static String getLastMonthStart(SimpleDateFormat sdf){
Calendar calendar2 = Calendar.getInstance();
calendar2.add(Calendar.MONTH, -1);
calendar2.set(Calendar.DAY_OF_MONTH,1);
String s = sdf.format(calendar2.getTime());
return s;
}
/**
* 获取上月的最后一天
*/
public static String getLastMonthEnd(SimpleDateFormat sdf){
Calendar calendar3 = Calendar.getInstance();
calendar3.set(Calendar.DAY_OF_MONTH, 0);
String s = sdf.format(calendar3.getTime());
return s;
}
/**
* 获取当前日期前/后n天
* @param i 前7天为-7,后1天为1
* @param simpleDateFormat
* @return
*/
public static String getDate1(int i,SimpleDateFormat simpleDateFormat) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
//i=-1获取的是前一天 1=1获取的是后一天 1=0获取的是当天
calendar.add(Calendar.DATE,i);
return simpleDateFormat.format(calendar.getTime());
}
/**
* 获取某年第一天日期开始时刻
* @param year 年份
* @return Date
*/
public static String getYearFirstDay(int year,SimpleDateFormat simpleDateFormat){
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, year);
return simpleDateFormat.format(cal.getTime());
}
/**
* 获取某年最后一天日期最后时刻
* @param year 年份
* @return Date
*/
public static String getYearLastDay(int year,SimpleDateFormat simpleDateFormat){
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, year);
cal.roll(Calendar.DAY_OF_YEAR, -1);
return simpleDateFormat.format(cal.getTime());
}
测试:
public static void main(String[] args) {
//System.out.println(DateUtils.getDate()+"%");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();//日历对象
calendar.set(Calendar.DAY_OF_WEEK,1);
System.out.println(sdf.format(calendar.getTime()));
Calendar calendar1 = Calendar.getInstance();//日历对象
calendar1.set(Calendar.DAY_OF_WEEK,calendar1.getActualMaximum(Calendar.DAY_OF_WEEK));
System.out.println(sdf1.format(calendar1.getTime()));
//获取前一个月第一天
Calendar calendar2 = Calendar.getInstance();
calendar2.add(Calendar.MONTH, -1);
calendar2.set(Calendar.DAY_OF_MONTH,1);
String firstDay = sdf.format(calendar2.getTime());
System.out.println ("获取前一个月第一天:"+firstDay);
//获取前一个月最后一天
Calendar calendar3 = Calendar.getInstance();
calendar3.set(Calendar.DAY_OF_MONTH, 0);
String lastDay = sdf1.format(calendar3.getTime());
System.out.println ("获取前一个月最后一天:"+lastDay);
String s2 = getNWeekTimeInterval(1,new SimpleDateFormat("yyyy-MM-dd 00:00:00"),new SimpleDateFormat("yyyy-MM-dd 23:59:59"));
String a = getThisWeekTimeInterval(new SimpleDateFormat("yyyy-MM-dd 00:00:00"),new SimpleDateFormat("yyyy-MM-dd 23:59:59"));
String[] s = a.split(",");
System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(getThisWeekTimeInterval(new SimpleDateFormat("yyyy-MM-dd"),new SimpleDateFormat("yyyy-MM-dd 23:59:59"))); // 获得本周的日期区间
System.out.println(getLastWeekTimeInterval(new SimpleDateFormat("yyyy-MM-dd"),new SimpleDateFormat("yyyy-MM-dd 23:59:59"))); // 获得上周的日期区间
String s3 = getNWeekTimeInterval(1,new SimpleDateFormat("yyyy-MM-dd"),new SimpleDateFormat("yyyy-MM-dd 23:59:59"));
System.out.println(s3);
System.out.println(getDate1(-6,new SimpleDateFormat("yyyy-MM-dd")));
System.out.println(getDate1(0,new SimpleDateFormat("yyyy-MM-dd")));
SimpleDateFormat sd1f = new SimpleDateFormat("yyyy");
System.out.println(Integer.parseInt(sd1f.format(new Date()))-1);
System.out.println(getDate1(-13,new SimpleDateFormat("yyyy-MM-dd")));
System.out.println(getDate1(-7,new SimpleDateFormat("yyyy-MM-dd")));
NumberFormat numberFormat = NumberFormat.getInstance();
System.out.println(100 * (9 / 3) );
System.out.println(numberFormat.format(100 * (9 / 3)));
}
结果: