該類主要服務于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;
- 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
- 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();
- }
- //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;
- }
- 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;
- }
- 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)) {
- // 先判斷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