时间工具类,主要用于sql的时间段查询
2011-08-23 22:55
253 查看
该类主要服务于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、工具类以附件形式提供。
Java代码
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;
/**
* 该类主要服务于sql中基于时间的统计查询,在写sql的过程中建议不要使用to_char或者to_date等oracle函数
* 这样不利用索引(除非你对to_char进行了类似索引的操作
* ),比如:在表的logintime字段上建立了索引,但是在sql中使用to_char(logintime,'yyyy-MM-dd')
* 作为检索条件的时候,数据库在logintime上建立的索引就没用了。在数据量很大的时候会影响检索的速度。
* 提供如下方法:
* 1、获取当前时间(按天截取时间)
* 2、根据指定时间提供天、周、旬、月、季度、年的开始时间,结束时间(时间格式采java.util.Date)
* 3、给定字符串类型的startTime和endTime,工具类负责类型的转换(String转换成Date)
* 注意:
* 1、在sql中使用开始时间和最后时间的时候,为了保证统计数据的正确性,
* sql按给出的例子组织:t.logintime >= startTime and t.loginTime <= entTime
* 2、时间的字符串格式采用 yyyy-MM-dd
*
*/
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$"); // 2010-12-22
/**
* 获取当前系统时间按天截取的时间
* @return
*/
public static Date getSystemTranceDay(){
return DateUtils.truncate(new Date(), Calendar.DATE);
}
/**
* 功能:根据指定时间获取当前天的开始和结束时间,以date数组返回
* 逻辑:
* 1、appointDate is null ,set default value sysdate
* 2、get date[]
* @param appointDate
* @return
*/
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;
}
/**
* 功能:根据指定时间获取当前星期的开始和结束时间,以date数组返回
* @param appointDate
* @return
*/
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;
}
/**
* 功能:根据指定的时间和上中下旬的其中一个,获取开始时间和结束时间
* @param appointDate
* @param appointIndex
* @return
*/
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();
}
//init 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;
}
/**
* 功能:根据指定时间获取相应月份的开始时间和结束时间
* @param appointDate
* @return
*/
public static Date[] getDateArrByMonth(Date appointDate){
Date stime = null;
Date etime = null;
Date[] date = new Date[2];
if(appointDate == null){
appointDate = new Date();
}
//init 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;
}
/**
* 功能:根据指定时间所在的当前年,获取指定季度的开始时间和结束时间
* @param appointDate 指定当前年
* @param appointIndex
* @return
* @throws IllegalArgumentException
*/
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;
}
/**
* 功能:根据指定时间,获取年的开始时间和结束时间
* @param appointDate
* @return
*/
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;
}
/**
* 逻辑: 1、检查startTime,endTime的有效性(是否为空,数据格式), 异常处理: 1、两个参数都为空,抛出空指针异常
* 2、数据格式不对,直接抛出 3、一个参数为空,另一个参数格式正确的情况下,为空的参数采用系统时间,为了保证startTime <=
* endTime,工具类会做适当的调整 2、转换 3、返回值是个Date[2]数组,date[0] 保存startTime值,date[1]
* 保存startTime值,其中startTime <= endTime
*
* @param startTime
* @param endTime
* @return
*/
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)) {
// 先判断endTime格式是否正确
Matcher matcher = pattern.matcher(endTime);
if (matcher.matches()) {
stime = DateUtils.truncate(new Date(), Calendar.DATE); // 当天的开始时间,比如:当前时间为2010-12-27 11:31:30 这里stime的时间是2010-12-27 0:0:0
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); // 当天的开始时间,比如:当前时间为2010-12-27 11:31:30 这里stime的时间是2010-12-27 0:0:0
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;
}
}
commons-dateutil-1.0.1.jar (12.5 KB)
下载次数: 171
该类提供如下方法:
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、工具类以附件形式提供。
Java代码
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;
/**
* 该类主要服务于sql中基于时间的统计查询,在写sql的过程中建议不要使用to_char或者to_date等oracle函数
* 这样不利用索引(除非你对to_char进行了类似索引的操作
* ),比如:在表的logintime字段上建立了索引,但是在sql中使用to_char(logintime,'yyyy-MM-dd')
* 作为检索条件的时候,数据库在logintime上建立的索引就没用了。在数据量很大的时候会影响检索的速度。
* 提供如下方法:
* 1、获取当前时间(按天截取时间)
* 2、根据指定时间提供天、周、旬、月、季度、年的开始时间,结束时间(时间格式采java.util.Date)
* 3、给定字符串类型的startTime和endTime,工具类负责类型的转换(String转换成Date)
* 注意:
* 1、在sql中使用开始时间和最后时间的时候,为了保证统计数据的正确性,
* sql按给出的例子组织:t.logintime >= startTime and t.loginTime <= entTime
* 2、时间的字符串格式采用 yyyy-MM-dd
*
*/
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$"); // 2010-12-22
/**
* 获取当前系统时间按天截取的时间
* @return
*/
public static Date getSystemTranceDay(){
return DateUtils.truncate(new Date(), Calendar.DATE);
}
/**
* 功能:根据指定时间获取当前天的开始和结束时间,以date数组返回
* 逻辑:
* 1、appointDate is null ,set default value sysdate
* 2、get date[]
* @param appointDate
* @return
*/
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;
}
/**
* 功能:根据指定时间获取当前星期的开始和结束时间,以date数组返回
* @param appointDate
* @return
*/
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;
}
/**
* 功能:根据指定的时间和上中下旬的其中一个,获取开始时间和结束时间
* @param appointDate
* @param appointIndex
* @return
*/
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();
}
//init 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;
}
/**
* 功能:根据指定时间获取相应月份的开始时间和结束时间
* @param appointDate
* @return
*/
public static Date[] getDateArrByMonth(Date appointDate){
Date stime = null;
Date etime = null;
Date[] date = new Date[2];
if(appointDate == null){
appointDate = new Date();
}
//init 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;
}
/**
* 功能:根据指定时间所在的当前年,获取指定季度的开始时间和结束时间
* @param appointDate 指定当前年
* @param appointIndex
* @return
* @throws IllegalArgumentException
*/
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;
}
/**
* 功能:根据指定时间,获取年的开始时间和结束时间
* @param appointDate
* @return
*/
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;
}
/**
* 逻辑: 1、检查startTime,endTime的有效性(是否为空,数据格式), 异常处理: 1、两个参数都为空,抛出空指针异常
* 2、数据格式不对,直接抛出 3、一个参数为空,另一个参数格式正确的情况下,为空的参数采用系统时间,为了保证startTime <=
* endTime,工具类会做适当的调整 2、转换 3、返回值是个Date[2]数组,date[0] 保存startTime值,date[1]
* 保存startTime值,其中startTime <= endTime
*
* @param startTime
* @param endTime
* @return
*/
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)) {
// 先判断endTime格式是否正确
Matcher matcher = pattern.matcher(endTime);
if (matcher.matches()) {
stime = DateUtils.truncate(new Date(), Calendar.DATE); // 当天的开始时间,比如:当前时间为2010-12-27 11:31:30 这里stime的时间是2010-12-27 0:0:0
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); // 当天的开始时间,比如:当前时间为2010-12-27 11:31:30 这里stime的时间是2010-12-27 0:0:0
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;
}
}
commons-dateutil-1.0.1.jar (12.5 KB)
下载次数: 171
相关文章推荐
- C#主要用于查询sql的web项目:查询以及页面显示数据非常缓慢的改进方案
- oracel sql语句:查询时间段中开始时间与结束时间相同的数据
- C#主要用于查询sql的web项目:gridview在页面上显示数据量大时跳转十分缓慢的解决方案
- SQL查询出当前服务器时间是否在表T1时间段内,如果有则选择出
- sql语句,多表关联查询,主要是T1表的日期在T2、T3时间段的统计查询
- sql查询时间段内所有周六日
- 查询SQL Server Index上次Rebuild时间
- sql时间查询问题
- 计算 sql查询语句所花时间
- sql查询某个时间段语句
- SQL判断一个事件段 是否在数据库中与其他时间段有重叠 判断时间重叠
- sql 时间类型 like 查询
- SQL语句实现日期时间模糊查询
- SQL 根据时间戳分组查询
- SQL SERVER查询优化工具:统计SQL语句执行时间
- sql查询技巧,按时间分段进行分组,每半小时一组统计组内记录数量
- mybatis查询时间段sql语句
- 项目中SQL语句实现时间联动查询
- SQL时间段的查询的两种执行方法
- 关于SQL时间类型的模糊查询 .