/**
* 获取今日开始时间和结束时间
* @param condition
* @return
*/
private void setDayRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.HOUR_OF_DAY,0);
condition.put("startTime",calendar.getTime());
calendar.set(Calendar.HOUR_OF_DAY,24);
condition.put("stopTime",calendar.getTime());
}
/**
* 获取昨天开始时间和结束时间
* @param condition
* @return
*/
private void setYesterdayRange(PageData condition) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.HOUR_OF_DAY,0);
condition.put("stopTime",calendar.getTime());
calendar.set(Calendar.HOUR_OF_DAY,-24);
condition.put("startTime",calendar.getTime());
}
/**
* 获得最近三天的开始时间和结束时间
* @param condition
* @return
*/
private void setThreeRange(PageData condition) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.HOUR_OF_DAY,24);
condition.put("stopTime",calendar.getTime());
calendar.set(Calendar.HOUR_OF_DAY,-72);
condition.put("startTime",calendar.getTime());
}
/**
* 获得近一周的开始时间和结束时间
* @param condition
* @return
*/
private void setDaySevenRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.HOUR_OF_DAY,24);
condition.put("stopTime",calendar.getTime());
calendar.set(Calendar.HOUR_OF_DAY,-168);
condition.put("startTime",calendar.getTime());
}
/**
* 获得近一月的开始时间和结束时间
* @param condition
* @return
*/
private void setDayTRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.HOUR_OF_DAY,24);
condition.put("stopTime",calendar.getTime());
calendar.set(Calendar.HOUR_OF_DAY,-720);
condition.put("startTime",calendar.getTime());
}
/**
* 获取本周开始时间和结束时间
* @param condition
* @return
*/
private void setWeekRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
condition.put("startTime",calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 7);
condition.put("stopTime",calendar.getTime());
或者
int week = calendar.get(Calendar.DAY_OF_WEEK)-2;
calendar.add(Calendar.DATE, -week);
condition.put("startTime",dateToString(calendar.getTime()));
calendar.add(Calendar.DATE, 7-week);
condition.put("stopTime",dateToString(calendar.getTime()));
}
/**
* 获取上周开始时间和结束时间
* @param condition
* @return
*/
private void setLastWeekRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
condition.put("stopTime",calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, -7);
condition.put("startTime",calendar.getTime());
}
/**
* 获取本月开始时间和结束时间
* @param condition
* @return
*/
private void setMonthRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
condition.put("startTime",calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 7);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 24);
condition.put("stopTime",calendar.getTime());
}
/**
* 获取上月开始时间和结束时间
* @param condition
* @return
*/
private void setLastMonthRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
condition.put("startTime",calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 7);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 24);
condition.put("stopTime",calendar.getTime());
}
/**
* 获取本季度开始时间和结束时间
* @param condition
* @return
*/
private void setQuarterRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3)
calendar.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
calendar.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
calendar.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DATE, 1);
} catch (Exception e) {
e.printStackTrace();
}
condition.put("startTime",calendar.getTime());
calendar.add(Calendar.MONTH, 3);
condition.put("stopTime",calendar.getTime());
}
/**
* 获取上季度开始时间和结束时间
* @param condition
* @return
*/
private void setLastQuarterRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3)
calendar.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
calendar.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
calendar.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DATE, 1);
} catch (Exception e) {
e.printStackTrace();
}
calendar.add(Calendar.MONTH, -3);
condition.put("startTime",calendar.getTime());
calendar.add(Calendar.MONTH, 3);
condition.put("stopTime",calendar.getTime());
}
/**
* 获取本年开始时间和结束时间
* @param condition
* @return
*/
private void setYearRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
condition.put("startTime",calendar.getTime());
calendar.add(Calendar.YEAR, 1);
condition.put("stopTime",calendar.getTime());
}
/**
* 获取上一年开始时间和结束时间
* @param condition
* @return
*/
private void setLastYearRange(PageData condition){
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.YEAR, -1);
condition.put("startTime",calendar.getTime());
calendar.add(Calendar.YEAR, 1);
condition.put("stopTime",calendar.getTime());
}
/**
* ("startTime" -> "Fri Jun 08 00:00:00 CST 2018")String的Date转为String类型的date
*/
private String strToformatDate(String date){
String str="";
Date date1=new Date();
try {
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
date1 = df.parse(date);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
str = df.format(date1);
}catch (Exception e){
e.printStackTrace();
}
return str;
}
/**
* ("startTime" -> "Fri Jun 08 00:00:00 CST 2018")String的Date转为时间戳
*/
public long toformatDate(String date){
String str="";
Date date1=new Date();
try {
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
date1 = df.parse(date);
System.err.println(date1.getTime()/1000);
}catch (Exception e){
e.printStackTrace();
}
return date1.getTime()/1000;
}
/**
* 测试
*/
public static void main(String[] args) {
PageData condition = new PageData();
setLastQuarterRange(condition);
String startTime=String.valueOf(strToformatDate(condition.get("startTime").toString()));
String stopTime=String.valueOf(strToformatDate(condition.get("stopTime").toString()));
System.out.println(startTime+"--->"+stopTime);
}
/**
* 得到n天之后是周几
* @param days
* @return
*/
public static String getAfterDayWeek(String days) {
int daysInt = Integer.parseInt(days);
Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
Date date = canlendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("E");
String dateStr = sdf.format(date);
return dateStr;
}
/**
* 得到n天之后的当前时间
* @param days
* @return
*/
public static String getAfterDayDate(String days) {
int daysInt = Integer.parseInt(days);
Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
Date date = canlendar.getTime();
SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdfd.format(date);
return dateStr;
}
/**
* 得到几分钟后的时间
* @param date minute(可为正负)
* @return
*/
public static Date getAfterTimeDate(Date date,int minute) {
Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.setTime(date);
canlendar.add(Calendar.MINUTE, minute); // 日期减 如果不够减会将月变动
return canlendar.getTime();
}
/**
* 时间相减得到天数(开始时间和结束时间可任意大小)
* @param beginDateStr(格式为"yyyy-MM-dd",也可为"yyyy-MM-dd hh:mm:ss")
* @param endDateStr
* @return long
*/
public static long getDaySub(String beginDateStr,String endDateStr){
long day=0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date beginDate = null;
Date endDate = null;
try {
beginDate = format.parse(beginDateStr);
endDate= format.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
//System.out.println("相隔的天数="+day);
return day;
}
private String dateToString(Date date){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
Calendar获取今天、昨天、近三天、近一周、近一月、本周、上周、本月、上月、本季度、上季度、本年、上一年开始时间和结束时间
最新推荐文章于 2024-07-15 03:07:13 发布