日期實用工具類
日期實用工具,我們在做統計功能的時候,經常要統計本週,本月,本季度,本年的資料,這就需要以某一天為基準,找出這些日期的範圍;另外也有些方便的Date與String的轉換函式。
import java.util.Date;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
/**
* @author iori.ogami
* @version 1.0 / 2005-06-30
*
*/
public class DateUtil {
public static final int HOUR = 1129 + 1;
public static final int DATE = 1129 + 2;
public static final int WEEK = 1129 + 3;
public static final int MONTH = 1129 + 4;
public static final int SEASON = 1129 + 5;
public static final int YEAR = 1129 + 6;
public static final String BEGIN = "begin";
public static final String END = "end";
/**
* get the specified date range based on ref.
* the range may be today, this week, this month,
* this season or this year.
*
* @param ref:
* the benchmark of the range.
* @param range:
* the range rounding the ref.
* DAY, WEEK, MONTH, SEASON or YEAR.
*
* @return
* a map with two entries, keyed BEGIN and END,
* whose values are of Date type and indicate
* the begin date and the end date of the range.
*/
public static Map getDateRange(Date ref, int range) {
Map m = new HashMap();
Calendar cal = Calendar.getInstance();
cal.setTime(ref);
// this week
if ( range == WEEK ) {
int cnt = 0;
for (; cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY; cnt++) {
cal.add(Calendar.DATE, -1);
}
m.put( BEGIN, cal.getTime() );
cal.add(Calendar.DATE, cnt);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
cal.add(Calendar.DATE, 1);
}
m.put( END, cal.getTime() );
// this month
} else if ( range == MONTH ) {
cal.set(Calendar.DATE, 1);
m.put(BEGIN, cal.getTime());
int month = cal.get(Calendar.MONTH)+1;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
cal.set(Calendar.DATE, 31);
break;
case 4:
case 6:
case 9:
case 11:
cal.set(Calendar.DATE, 30);
break;
case 2:
int year = cal.get(Calendar.YEAR);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
cal.set(Calendar.DATE, 29);
} else {
cal.set(Calendar.DATE, 28);
}
break;
}
m.put(END, cal.getTime());
//this season
} else if ( range == SEASON ) {
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
switch (month / 3) {
case 0: //01-01 to 03-31
cal.set(Calendar.MONTH,0);
cal.set(Calendar.DATE,1);
m.put(BEGIN, cal.getTime());
cal.set(Calendar.MONTH,2);
cal.set(Calendar.DATE,31);
m.put(END, cal.getTime());
break;
case 1: //04-01 to 06-30
cal.set(Calendar.MONTH,3);
cal.set(Calendar.DATE,1);
m.put(BEGIN, cal.getTime());
cal.set(Calendar.MONTH,5);
cal.set(Calendar.DATE,30);
m.put(END, cal.getTime());
break;
case 2: //07-01 to 09-30
cal.set(Calendar.MONTH,6);
cal.set(Calendar.DATE,1);
m.put(BEGIN, cal.getTime());
cal.set(Calendar.MONTH,8);
cal.set(Calendar.DATE,30);
m.put(END, cal.getTime());
break;
case 3: // 10-01 to 12-31
cal.set(Calendar.MONTH,9);
cal.set(Calendar.DATE,1);
m.put(BEGIN, cal.getTime());
cal.set(Calendar.MONTH,11);
cal.set(Calendar.DATE,31);
m.put(END, cal.getTime());
break;
}
// this year
} else if ( range == YEAR ) {
cal.set(Calendar.MONTH,0);
cal.set(Calendar.DATE,1);
m.put(BEGIN, cal.getTime());
cal.set(Calendar.MONTH,11);
cal.set(Calendar.DATE,31);
m.put(END, cal.getTime());
// default is DAY
} else {
m.put(BEGIN,cal.getTime());
m.put(END,cal.getTime());
}
setTime(m); //optional
return m;
}
/**
* get the specified date range based on ref.
* the range may be this week, this month,
* this season or this year.
*
* @param ref:
* the benchmark of the range. a String of "yyyy-mm-dd" format.
* @param range:
* the range rounding the ref.
* WEEK, MONTH, SEASON or YEAR.
*
* @return
* a map with two entries, keyed BEGIN and END,
* whose values are of Date type and indicate
* the begin date and the end date of the range.
* if the input string dosen't match "yyyy-M[M]-d[d]" format,
* null will be returned.
*/
public static Map getDateRange(String ref, int range){
if( !ref.matches("}") )
return null;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt( ref.split("-")[0]) );
cal.set(Calendar.MONTH, Integer.parseInt( ref.split("-")[1])-1 );
cal.set(Calendar.DATE, Integer.parseInt( ref.split("-")[2]) );
Date d = cal.getTime();
return getDateRange(d, range);
}
/**
* convert a Date type variable to String type,
* applying the specified format.
*
* @param d
* @param format
* @return
*/
public static String dateToStr(Date d, String format){
SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat
.getDateInstance();
df.applyPattern(format);
return df.format(d);
}
/**
* convert a Date type variable to String,
* applying the "yyyy-MM-dd" format.
*
* @param d
* @return
*/
public static String dateToStr(Date d){
return dateToStr(d, "yyyy-MM-dd");
}
/**
* convert a String type variable to Date type.
* this method only support the "yyyy-MM-dd" format,
* if dosen't match, null will be returned.
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate){
if( !strDate.matches("}") )
return null;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(strDate.split("-")[0]) );
cal.set(Calendar.MONTH, Integer.parseInt(strDate.split("-")[1])-1 );
cal.set(Calendar.DATE, Integer.parseInt(strDate.split("-")[2]) );
return cal.getTime();
}
/**
* set the begin time of the date range to 00:00:00,
* and the end time of the date range to 23:59:59.
* @param m:
* the date range map.
*/
private static void setTime(Map m){
Calendar cal = Calendar.getInstance();
cal.setTime( (Date)m.get(BEGIN) );
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
m.put(BEGIN,cal.getTime());
cal.setTime( (Date)m.get(END) );
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
m.put(END,cal.getTime());
}
public static void main(String args[]){
System.out.println( getDateRange(new Date(), WEEK) );
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7199667/viewspace-915315/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 當前日期+隨機數+檔名(採用兩種方法實現的工具類)隨機
- 支付寶即時交易,日期工具類
- 實用性工具類——Post請求
- 日期類
- 字串與日期型別轉換的工具類字串型別
- java Date日期類和SimpleDateFormat日期類格式JavaORM
- 前端簡潔並實用的工具類前端
- Java的日期類都是怎麼用的Java
- 簡單易用且全面的JavaScript日期工具類庫JavaScript
- LocalDate,LocalDateTime和處理時間、日期工具類LDA
- Jdk8 新日期工具類 Api 學習筆記JDKAPI筆記
- 日期工具SimpleDateFormatORM
- Android實用的SQLite資料庫工具類AndroidSQLite資料庫
- Java日期類分析Java
- 日期時間類
- 一款實用的.NET Core加密解密工具類庫加密解密
- 前端簡潔並實用的工具類函式封裝前端函式封裝
- js日期轉換工具類(仿oracle to_char,to_date等語法)JSOracle
- 資料庫工具類實現資料庫
- 第 9 篇:實現分類、標籤、歸檔日期介面
- 簡易實用的JavaScript日期時間操作!JavaScript
- Calendar類在Java中的應用與日期時間處理Java
- DreamJudge-1437-日期類
- 直播電商平臺開發,日期與時間戳轉換封裝工具類時間戳封裝
- 【實驗】【總結】Oracle日期類操作(格式 加減乘 取毫秒)Oracle
- javascript 日期時間函式(經典+完善+實用)JavaScript函式
- Facebook暫停中國工具類應用廣告
- grep 工具實用頁
- 自定義超實用Redis工具類(滿足物件,list,map等型別)Redis物件型別
- Java 8 的日期時間工具Java
- Java日期和時間類簡介Java
- lua外掛之----【luaDate 日期類】
- Java 日期和時間 API:實用技巧與示例 - 輕鬆處理日期和時間JavaAPI
- 工具類
- 值得收藏的 ViewHolder 工具類實現View
- 前端實用小工具前端
- joda jar日期處理類的學習JAR
- java工具類之編碼轉換工具類Java