在开发中遇到这么一个问题就是,要查选今天的,昨天的,本周的,上周的,本月的以及上个月的数据,数据数据库中存的是时间戳,所以要查询的话就要的到一个时间戳的范围,并且要得到的是00点到下一天的00点的时间戳范围,下面是我写的点做一下记录
/**
* 更据时间戳获取当天的00的时间戳
*
* @param now 时间戳
* @return 得到的00点的时间戳
*/
private static long getStartTimeOfDay(long now) {
TimeZone curTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
Calendar calendar = Calendar.getInstance(curTimeZone);
calendar.setTimeInMillis(now);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
注意:这个要使用jdk1.8这里面使用了jdk1.8的Time包下的一些类
XZ_TIME_TYPE是自定义的枚举
/**
* <p> 时间类型 </p>
*
* @author Alemand
* @since 2017/12/29
*/
public enum XZ_TIME_TYPE {
/**
* 今天
*/
TODAY(0),
/**
* 昨天
*/
YESTERDAY(1),
/**
* 本周
*/
THISWEEK(2),
/**
* 上周
*/
LASTWEEK(3),
/**
* 这个月
*/
THISMONTH(4),
/**
* 上月
*/
LASTMONTH(5),
/**
* 自定义
*/
CUSTOMIZE(6);
private static Map map = new HashMap();
static {
for (XZ_TIME_TYPE pageType : XZ_TIME_TYPE.values()) {
map.put(pageType.value, pageType);
}
}
private int value;
private XZ_TIME_TYPE(int value) {
this.value = value;
}
public static XZ_TIME_TYPE valueOf(int value) {
return (XZ_TIME_TYPE) map.get(value);
}
public int getValue() {
return value;
}
}
/**
* 根据时间的类型获取时间戳范围
*
* @param timeType 要获取的时间类型
* @return 开始时间和结束范围
*/
public static Map<String, Long> getTimeFrame(XZ_TIME_TYPE timeType) {
//获取当前的时间戳
long nowTimeStamp = getNowTimeStamp();
HashMap<String, Long> result = new HashMap<>();
//开始的00点
long startTimeOfDay = 0;
//结束的00点
long endTimeOfDay = 0;
//今天的日期
LocalDate today;
//星期几的枚举
DayOfWeek dayOfWeek;
//星期所对的整数
int value;
//周一的时间戳
long monTime;
//当前日期
Calendar thisMonth;
switch (timeType) {
case TODAY:
//先获取当天00点的时间错
startTimeOfDay = getStartTimeOfDay(nowTimeStamp);
//获取明天的时间00点时间戳
endTimeOfDay = startTimeOfDay + 24 * 60 * 60 * 1000L;
break;
case YESTERDAY:
//获取昨天的一个时间戳
long last = nowTimeStamp - 24 * 60 * 60 * 1000L;
startTimeOfDay = getStartTimeOfDay(last);
endTimeOfDay = startTimeOfDay + 24 * 60 * 60 * 1000L;
break;
case THISWEEK:
//获取今天的日期
today = LocalDate.now();
//得到今天是星期几
dayOfWeek = today.getDayOfWeek();
value = dayOfWeek.getValue();
//获取周一的时间戳
monTime = nowTimeStamp - 24 * 60 * 60 * 1000L * (value - 1);
//获取本周周一的00点的时间戳
startTimeOfDay = getStartTimeOfDay(monTime);
//获取下周一的00点时间戳
endTimeOfDay = startTimeOfDay + 24 * 60 * 60 * 1000 * 7L;
break;
case LASTWEEK:
//获取今天的日期
today = LocalDate.now();
//得到今天是星期几
dayOfWeek = today.getDayOfWeek();
value = dayOfWeek.getValue();
//获取周一的时间戳
monTime = nowTimeStamp - 24 * 60 * 60 * 1000 * (value - 1);
//在获取上周周一的00点的是时间戳
monTime = monTime - 24 * 60 * 60 * 1000 * 7;
//获取上周周一的00点的时间戳
startTimeOfDay = getStartTimeOfDay(monTime);
//获取下周一的00点时间戳
endTimeOfDay = startTimeOfDay + 24 * 60 * 60 * 1000 * 7L;
break;
case THISMONTH:
//获取当前日期
thisMonth = Calendar.getInstance();
//设置为1号,当前日期既为本月第一天
thisMonth.set(Calendar.DAY_OF_MONTH, 1);
DateStringInfo dateStrByTimestamp = getDateStrByTimestamp(thisMonth.getTimeInMillis());
System.out.println(dateStrByTimestamp.getMonth());
//一号的00点
startTimeOfDay = getStartTimeOfDay(thisMonth.getTimeInMillis());
//获取下个月
thisMonth.add(Calendar.MONTH, 1);
//获取下个月的00点
endTimeOfDay = getStartTimeOfDay(thisMonth.getTimeInMillis());
break;
case LASTMONTH:
thisMonth = Calendar.getInstance();
//设置为1号,当前日期既为本月第一天
thisMonth.set(Calendar.DAY_OF_MONTH, 1);
endTimeOfDay = getStartTimeOfDay(thisMonth.getTimeInMillis());
//获取上个月
thisMonth.add(Calendar.MONTH, -1);
//获取下个月的00点
startTimeOfDay = getStartTimeOfDay(thisMonth.getTimeInMillis());
break;
case CUSTOMIZE://自定义
break;
default:
break;
}
result.put("startTime", startTimeOfDay);
result.put("endTime", endTimeOfDay);
return result;
}
获取任意时间戳的年月日时分秒字符串
/**
* 获取时间戳对应年-月-日 时分钟秒对应的字符串
*/
public static DateStringInfo getDateStrByTimestamp(long timestamp) {
Date date = new Date(timestamp);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = simpleDateFormat.format(date);
String year = dateStr.substring(0, 4);
String month = dateStr.substring(5, 7);
String day = dateStr.substring(8, 10);
String hour = dateStr.substring(11, 13);
String minute = dateStr.substring(14, 16);
String second = dateStr.substring(17, 19);
return new DateStringInfo.Builder()
.withYear(year)
.withMonth(month)
.withDay(day)
.withHour(hour)
.withMinute(minute)
.withSecond(second)
.build();
}