该类主要服务于sql中基于时间的统计查询,在写sql的过程中建议不要使用to_char或者to_date等oracle函数这样不利用索引(除非你对to_char进行了类似索引的操作),比如:在表的logintime字段上建立了索引,但是在sql中使用to_char(logintime,'yyyy-MM-dd')作为检索条件的时候,数据库在logintime上建立的索引就没用了。在数据量很大的时候会影响检索的速度。
该类提供如下方法:
1、获取系统按天截取时间 getSystemTranceDay();
2、根据指定时间提供天、周、旬、月、季度、年的开始时间,结束时间(时间格式采java.util.Date),以Date数组的形式返回开始和结束时间。
3、给定字符串类型的startTime和endTime,工具类负责类型的转换(String转换成Date)
注意:
1、在sql中使用开始时间和最后时间的时候,为了保证统计数据的正确性,
sql按给出的例子组织:t.logintime >= startTime and t.loginTime <= entTime
2、时间的字符串格式采用 yyyy-MM-dd
3、使用该类的时候,注意返回结果的正确性,虽然单元测试通过,还是担心有数据不正确的问题。
4、工具类以附件形式提供。
-
import java.text.ParseException;
-
import java.text.SimpleDateFormat;
-
import java.util.Calendar;
-
import java.util.Date;
-
import java.util.regex.Matcher;
-
import java.util.regex.Pattern;
-
import java.util.zip.DataFormatException;
-
-
import org.apache.commons.lang.time.DateUtils;
-
import com.pengsy.commons.stringutil.StringUtil;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public final class DateUtil {
-
-
private static SimpleDateFormat sDateFormat = new SimpleDateFormat(
-
"yyyy-MM-dd");
-
-
public static final int FIRSTTEN = 1 ;
-
public static final int MIDTEN = 2;
-
public static final int LASTTEN = 3;
-
-
public static final int FIRSTQUARTER = 1;
-
public static final int SECONDQUARTER = 2;
-
public static final int THIRDQUARTER = 3;
-
public static final int FORTHQUARTER = 4;
-
-
private static Pattern pattern = Pattern
-
.compile("^[1-9]\\d{3}-[01]?\\d-[0|1|2|3]?\\d$");
-
-
-
-
-
-
public static Date getSystemTranceDay(){
-
return DateUtils.truncate(new Date(), Calendar.DATE);
-
}
-
-
-
-
-
-
-
-
-
-
public static Date[] getDateArrByDay(Date appointDate){
-
Date stime = null;
-
Date etime = null;
-
Date[] date = new Date[2];
-
-
if(appointDate == null){
-
appointDate = new Date();
-
}
-
stime = DateUtils.truncate(appointDate,Calendar.DATE);
-
etime = DateUtils.addSeconds(DateUtils.truncate(DateUtils.addDays(appointDate, 1), Calendar.DATE),-1);
-
-
date[0] = stime;
-
date[1] = etime;
-
return date;
-
}
-
-
-
-
-
-
-
public static Date[] getDateArrByWeek(Date appointDate){
-
Date stime = null;
-
Date etime = null;
-
Date[] date = new Date[2];
-
if(appointDate == null){
-
appointDate = new Date();
-
}
-
-
Calendar calendar = Calendar.getInstance();
-
calendar.setTime(appointDate);
-
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
-
System.out.println(dayOfWeek);
-
-
calendar.add(Calendar.DAY_OF_MONTH, -dayOfWeek+2);
-
-
stime = DateUtils.truncate(calendar.getTime(), Calendar.DATE);
-
calendar.add(Calendar.DAY_OF_MONTH, 7);
-
etime = DateUtils.addSeconds(DateUtils.truncate(calendar.getTime(), Calendar.DATE), -1);
-
-
date[0] = stime;
-
date[1] = etime;
-
-
return date;
-
}
-
-
-
-
-
-
-
-
public static Date[] getDateArrByTenDays(Date appointDate,int appointIndex ){
-
Date stime = null;
-
Date etime = null;
-
Date[] date = new Date[2];
-
if(appointDate == null){
-
appointDate = new Date();
-
}
-
-
Calendar calendar = Calendar.getInstance();
-
calendar.setTime(appointDate);
-
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
-
int maxDayOfMonth = calendar.getMaximum(Calendar.DAY_OF_MONTH);
-
-
Date tempDate = DateUtils.truncate(DateUtils.addDays(appointDate, -dayOfMonth + 1), Calendar.DATE);
-
-
if(appointIndex == FIRSTTEN){
-
stime = tempDate;
-
etime = DateUtils.addSeconds(DateUtils.addDays(stime, 10), -1);
-
}
-
-
if(appointIndex == MIDTEN){
-
stime = DateUtils.addDays(tempDate, 10);
-
etime = DateUtils.addSeconds(DateUtils.addDays(stime, 10), -1);
-
}
-
-
if(appointIndex == LASTTEN){
-
stime = DateUtils.addDays(tempDate, 20);
-
etime = DateUtils.addSeconds(DateUtils.addDays(tempDate, maxDayOfMonth), -1);
-
}
-
-
date[0] = stime;
-
date[1] = etime;
-
return date;
-
}
-
-
-
-
-
-
-
public static Date[] getDateArrByMonth(Date appointDate){
-
Date stime = null;
-
Date etime = null;
-
Date[] date = new Date[2];
-
if(appointDate == null){
-
appointDate = new Date();
-
}
-
-
-
Calendar calendar = Calendar.getInstance();
-
calendar.setTime(appointDate);
-
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
-
int maxDayOfMonth = calendar.getMaximum(Calendar.DAY_OF_MONTH);
-
-
appointDate = DateUtils.truncate(appointDate, Calendar.DATE);
-
-
stime = DateUtils.truncate(DateUtils.addDays(appointDate, -dayOfMonth+1), Calendar.DATE);
-
etime = DateUtils.addSeconds(DateUtils.addDays(stime, maxDayOfMonth), -1);
-
-
date[0] = stime;
-
date[1] = etime;
-
-
return date;
-
}
-
-
-
-
-
-
-
-
-
public static Date[] getDateArrByQuarter(Date appointDate,int appointIndex) throws IllegalArgumentException{
-
Date stime = null;
-
Date etime = null;
-
Date[] date = new Date[2];
-
if(appointDate == null){
-
appointDate = new Date();
-
}
-
int month = appointDate.getMonth();
-
Date tempDate = DateUtils.truncate(appointDate, Calendar.YEAR);
-
if(appointIndex == FIRSTQUARTER){
-
stime = tempDate;
-
}else if(appointIndex == SECONDQUARTER){
-
stime = DateUtils.addMonths(tempDate, 3);
-
}else if(appointIndex == THIRDQUARTER ){
-
stime = DateUtils.addMonths(tempDate, 6);
-
}else if(appointIndex == FORTHQUARTER){
-
stime = DateUtils.addMonths(tempDate, 9);
-
}
-
etime = DateUtils.addSeconds(DateUtils.addMonths(stime, 3), -1);
-
-
date[0] = stime;
-
date[1] = etime;
-
-
return date;
-
}
-
-
-
-
-
-
-
public static Date[] getDateArrByYear(Date appointDate){
-
Date stime = null;
-
Date etime = null;
-
Date[] date = new Date[2];
-
if(appointDate == null){
-
appointDate = new Date();
-
}
-
stime = DateUtils.truncate(appointDate, Calendar.YEAR);
-
etime = DateUtils.addSeconds(DateUtils.addYears(stime, 1), -1);
-
-
date[0] = stime;
-
date[1] = etime;
-
-
return date;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
public static Date[] convertDateClass(String startTime, String endTime)
-
throws NullPointerException, DataFormatException, ParseException {
-
Date stime = null;
-
Date etime = null;
-
Date[] date = new Date[2];
-
-
if (StringUtil.isEmpty(startTime) && StringUtil.isEmpty(endTime)) {
-
throw new NullPointerException("两个参数不能同时为空");
-
}
-
-
if (StringUtil.isEmpty(startTime) && !StringUtil.isEmpty(endTime)) {
-
-
Matcher matcher = pattern.matcher(endTime);
-
if (matcher.matches()) {
-
stime = DateUtils.truncate(new Date(), Calendar.DATE);
-
etime = DateUtils.truncate(sDateFormat.parse(endTime),Calendar.DATE);
-
} else {
-
throw new DataFormatException(
-
"参数endTime的格式不正确!正确的格式 yyyy-MM-dd 比如:2010-12-12!");
-
}
-
}
-
if (!StringUtil.isEmpty(startTime) && StringUtil.isEmpty(endTime)) {
-
Matcher matcher = pattern.matcher(startTime);
-
if (matcher.matches()) {
-
-
etime = DateUtils.truncate(new Date(), Calendar.DATE);
-
stime = DateUtils.truncate(sDateFormat.parse(startTime),Calendar.DATE);
-
} else {
-
throw new DataFormatException(
-
"参数startTime的格式不正确!正确的格式 yyyy-MM-dd 比如:2010-12-12!");
-
}
-
}
-
-
if (!StringUtil.isEmpty(startTime) && !StringUtil.isEmpty(endTime)) {
-
Matcher sMatcher = pattern.matcher(startTime);
-
Matcher eMatcher = pattern.matcher(endTime);
-
if (sMatcher.matches() && eMatcher.matches()) {
-
-
stime = DateUtils.truncate(sDateFormat.parse(startTime),
-
Calendar.DATE);
-
etime = DateUtils.truncate(sDateFormat.parse(endTime),
-
Calendar.DATE);
-
-
} else {
-
throw new DataFormatException(
-
"请检查参数startTime、endTime的格式是否正确!正确的格式 yyyy-MM-dd 比如:2010-12-12!");
-
}
-
-
}
-
-
if (!stime.before(etime)) {
-
Date temp = stime;
-
stime = etime;
-
etime = temp;
-
temp = null;
-
}
-
-
date[0] = stime;
-
date[1] = etime;
-
return date;
-
}
-
}
|
|