package com.topstar.volclient.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期操作工具類
* <p/>
* <pre>
* 日期類型顯示格式
* ------------------------------
* 字母 日期或時間元素 表示 示例
* G Era 標誌符 Text AD
* y 年 Year 1996; 96
* M 年中的月份 Month July; Jul; 07
* w 年中的週數 Number 27
* W 月份中的週數 Number 2
* D 年中的天數 Number 189
* d 月份中的天數 Number 10
* F 月份中的星期 Number 2
* E 星期中的天數 Text Tuesday; Tue
* a Am/pm 標記 Text PM
* H 一天中的小時數(0-23) Number 0
* k 一天中的小時數(1-24) Number 24
* K am/pm 中的小時數(0-11) Number 0
* h am/pm 中的小時數(1-12) Number 12
* m 小時中的分鐘數 Number 30
* s 分鐘中的秒數 Number 55
* S 毫秒數 Number 978
* z 時區 General time zone Pacific Standard Time; PST; GMT-08:00
* Z 時區 RFC 822 time zone -0800
* <p/>
* 常見的格式如下:
* "yyyy","年份,如:2009"
* "M","月份,如:8"
* "d","日,如:15"
* "yyyyMMdd","年月日,如:20091001"
* "yyyy-MM-dd","年月日,如:2009-10-01"
* "yyyy/MM/dd","年月日,如:2009/10/01"
* "yyyy.MM.dd","年月日,如:2009.10.01"
* "yyyy年MM月dd日","年月日,如:2009年10月01日"
* "yyyy-MM-dd HH:mm","年月日時分,如:2009-10-01 10:01"
* "yyyy/MM/dd HH:mm","年月日時分,如:2009/10/01 10:01"
* "yyyy.MM.dd HH:mm","年月日時分,如:2009.10.01 10:01"
* "yyyy年MM月dd日 HH時mm分","年月日時分,如:2009年10月01日 10時01分"
* "yyyy-MM-dd HH:mm:ss","年月日時分秒,如:2009-10-01 10:01:01"
* "yyyy/MM/dd HH:mm:ss","年月日時分秒,如:2009/10/01 10:01:01"
* "yyyy.MM.dd HH:mm:ss","年月日時分秒,如:2009.10.01 10:01:01"
* "yyyy年MM月dd日 HH時mm分ss秒","年月日時分秒,如:2009年10月01日 10時01分01秒"
* "yyyy-MM-dd a","年月日上午/下午,如:2009-10-01 上午"
* "yyyy/MM/dd a","年月日上午/下午,如:2009/10/01 下午"
* "yyyy.MM.dd a","年月日上午/下午,如:2009.10.01 上午"
* </pre>
*/
public class DateUtil {
/**
* 日期類型格式:yyyy-MM-dd
*/
public static String YYYY_MM_DD = "yyyy-MM-dd";
/**
* 日期類型格式:yyyy-MM-dd HH:mm
*/
public static String YYYY_MM_DD_HHMM = "yyyy-MM-dd HH:mm";
/**
* 日期類型格式:yyyy-MM-dd HH:mm:ss
*/
public static String YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
/**
* 時間戳類型格式:yyyy-MM-dd HH:mm:ss.SSS
*/
public static String YYYY_MM_DD_HHMMSS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
/**
* 判斷字符串是否是有效的日期, 以下格式被認爲是有效的(字符串長度大於或等於8):
* <p>yyyy-MM-dd</p>
* <p>yyyy-MM-d</p>
* <p>yyyy-M-dd</p>
* <p>yyyy-M-d</p>
* <p>yyyy/MM/dd</p>
* <p>yyyy/MM/d</p>
* <p>yyyy/M/dd</p>
* <p>yyyy/M/d</p>
* <p>yyyyMMdd</p>
*
* @param date 日期字符串
* @return 如果是有效的日期則返回true,否則返回false
*/
public static boolean isValidDate(String date) {
if ((date == null) || (date.length() < 8)) {
return false;
}
try {
boolean result = false;
SimpleDateFormat formatter;
char dateSpace = date.charAt(4);
String format[];
if ((dateSpace == '-') || (dateSpace == '/')) {
format = new String[4];
String strDateSpace = Character.toString(dateSpace);
format[0] = "yyyy" + strDateSpace + "MM" + strDateSpace + "dd";
format[1] = "yyyy" + strDateSpace + "MM" + strDateSpace + "d";
format[2] = "yyyy" + strDateSpace + "M" + strDateSpace + "dd";
format[3] = "yyyy" + strDateSpace + "M" + strDateSpace + "d";
} else {
format = new String[1];
format[0] = "yyyyMMdd";
}
for (String aFormat : format) {
formatter = new SimpleDateFormat(aFormat);
formatter.setLenient(false);
String tmp = formatter.format(formatter.parse(date));
if (date.equals(tmp)) {
result = true;
break;
}
}
return result;
} catch (ParseException e) {
return false;
}
}
/**
* 判斷字符串是否是有效的日期
*
* @param date 日期字符串
* @param pattern 日期格式,如:yyyy-MM-dd
* @return 是則返回true,否則返回false
*/
public static boolean isValidTime(String date, String pattern) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
formatter.setLenient(false);
formatter.parse(date);
return true;
} catch (ParseException e) {
return false;
}
}
/**
* 解析字符串爲日期類型
* @param date_str 字符串類型日期值,要求日期數據格式爲:
* yyyy-M-d,yyyy/M/d,yyyyMMdd, yyyy-MM-dd,yyyy/MM/dd
* yyyy-MM-dd HH:mm 或 yyyy-MM-dd HH:mm:ss
* yyyy-MM-dd HH:mm:ss
* yyyyMMdd 或 yyyy-MM-dd 或 yyyy-MM-dd HH:mm 或 yyyy-MM-dd HH:mm:ss
* @return
* @throws Exception
*/
public static Date parseDate(String date_str) throws Exception {
if (date_str == null || date_str.equals("")) {
return null;
}
Date date = null;
SimpleDateFormat format;
try {
if (date_str.length() == 8) {
if (date_str.contains("-")) {
format = new SimpleDateFormat("yyyy-M-d");
} else if (date_str.contains("/")) {
format = new SimpleDateFormat("yyyy/M/d");
} else {
format = new SimpleDateFormat("yyyyMMdd");
}
} else if (date_str.length() == 10) {
if (date_str.contains("-")) {
format = new SimpleDateFormat("yyyy-MM-dd");
} else if (date_str.contains("/")) {
format = new SimpleDateFormat("yyyy/MM/dd");
} else {
format = new SimpleDateFormat("yyyy-MM-dd");
}
} else if (date_str.length() == 16) {
if (date_str.contains("-")) {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
} else if (date_str.contains("/")) {
format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
} else {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
} else if (date_str.length() == 19) {
if (date_str.contains("-")) {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else if (date_str.contains("/")) {
format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
} else {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
} else {
format = new SimpleDateFormat("yyyy-MM-dd");
}
format.setLenient(false);
date = format.parse(date_str);
return date;
} catch (ParseException e) {
String msg = "日期數據[" + date_str + "]類型轉換異常!";
throw new Exception(msg,e);
}
}
/**
* 用默認的格式將日期字符串轉換成日期對象<br/>
* 默認格式:yyyy-MM-dd
* @param date 日期字符串
* @return 返回格式化的日期
*/
public static Date parse(String date){
return parseDate(date,YYYY_MM_DD);
}
/**
* 將指定格式的日期字符串轉換成日期對象
*
* @param date 日期字符串
* @param pattern 日期格式,如:yyyy-MM-dd
* @return 返回格式化的日期
*/
public static Date parseDate(String date, String pattern){
try {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
formatter.setLenient(false);
return formatter.parse(date);
} catch (Exception e) {
return null;
}
}
/**
* 將指定格式的日期字符串轉換成日期對象
*
* @param date 日期字符串
* @param pattern 日期格式,如:yyyy-MM-dd
* @return 返回格式化的日期
* @throws ParseException 解析異常
*/
public static Date parseDate(String date, String pattern, Date dft) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
formatter.setLenient(false);
return formatter.parse(date);
} catch (Exception e) {
return dft;
}
}
/**
* 將日期對象轉換成指定格式的字符串
*
* @param date 日期對象
* @param pattern 日期格式,如:yyyy-MM-dd
* @return 返回格式化的日期字符串
*/
public static String format(Date date, String pattern) {
if (date == null) {
return null;
}
try {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
} catch (Exception e) {
return null;
}
}
/**
* 將當前日期轉換成指定格式的字符串
*
* @param pattern 日期格式
* @return 返回格式化的日期字符串
*/
public static String getNow(String pattern) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date now = new Date();
return formatter.format(now);
} catch (Exception e) {
return null;
}
}
/**
* 取得年份,格式"yyyy"
*
* @return 返回當前年份
*/
public static int getYear() {
Date now = new Date();
return getYear(now);
}
/**
* 取得日期的年份,格式"yyyy"
*
* @param date 日期
* @return 日期的年份
*/
public static int getYear(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
return Integer.parseInt(formatter.format(date));
}
/**
* 取得月份
*
* @return 返回當前月份
*/
public static int getMonth() {
Date now = new Date();
return getMonth(now);
}
/**
* 取得日期的月份
*
* @param date 日期
* @return 日期的月份
*/
public static int getMonth(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("M");
return Integer.parseInt(formatter.format(date));
}
/**
* 取得今天的日期數
*
* @return 返回今天的日期數
*/
public static int getDay() {
Date now = new Date();
return getDay(now);
}
/**
* 取得日期的天數
*
* @param date 日期
* @return 日期的天數
*/
public static int getDay(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("d");
return Integer.parseInt(formatter.format(date));
}
/**
* 獲得與某日期相隔幾天的日期
*
* @param date 指定的日期
* @param addCount 相隔的天數,可以是負數,表示日期前幾天
* @return 返回的日期
*/
public static Date addDay(Date date, int addCount) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, addCount);
return calendar.getTime();
}
/**
* 獲得與某日期相隔幾月的日期
*
* @param date 指定的日期
* @param addCount 相隔的月數,可以是負數,表示日期前幾月
* @return 返回的日期
*/
public static Date addMonth(Date date, int addCount){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, addCount);
return calendar.getTime();
}
/**
* 得到某天是周幾
*
* @param strDay 2010-01-02
* @return 周幾
*/
public static int getWeekDay(String strDay){
Date day = DateUtil.addDay(parseDate(strDay, "yyyy-MM-dd"), -1);
Calendar strDate = Calendar.getInstance();
strDate.setTime(day);
return strDate.get(Calendar.DAY_OF_WEEK);
}
/**
* 得到某天是周幾
* @param date 日期類型
* @return 周幾
* @throws ParseException 解析異常
*/
public static int getWeekDay(Date date){
Date day = DateUtil.addDay(date, -1);
Calendar strDate = Calendar.getInstance();
strDate.setTime(day);
return strDate.get(Calendar.DAY_OF_WEEK);
}
/**
* 取得兩個日期段的間隔天數
*
* @param t1 時間1
* @param t2 時間2
* @return t2 與t1的間隔天數
* @throws ParseException 如果輸入的日期格式不是0000-00-00 格式拋出異常
*/
public static int getBetweenDays(String t1, String t2) throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
int betweenDays;
Date d1 = format.parse(t1);
Date d2 = format.parse(t2);
betweenDays = getBetweenDays(d1, d2);
return betweenDays;
}
/**
* 取得兩個日期段的日期間隔
*
* @param d1 日期1
* @param d2 日期2
* @return t2 與t1的間隔天數
*/
public static int getBetweenDays(Date d1, Date d2) {
if (d1 == null || d2 == null) {
return -1;
}
int betweenDays;
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
// 保證第二個時間一定大於第一個時間
if (c1.after(c2)) {
c2.setTime(d1);
c1.setTime(d2);
}
int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
for (int i = 0; i < betweenYears
;
i++) {
c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
}
return betweenDays;
}
public static String getWeek(Date date){
SimpleDateFormat format = new SimpleDateFormat("EEEE");
String week = format.format(date);
return week;
}
/**
* 判斷今天是否爲星期六
* @return
*/
public static boolean isSaturday(){
int week = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
return week == Calendar.SATURDAY;
}
/**
* 判斷是否爲星期六
* @param date:年月日,格式:yyyy-MM-dd
* @return
*/
public static boolean isSaturday(String date){
try {
Date d = DateUtil.parseDate(date,YYYY_MM_DD);
Calendar calendar =Calendar.getInstance();
calendar.setTime(d);
int week = calendar.get(Calendar.DAY_OF_WEEK);
return week == Calendar.SATURDAY;
} catch (Exception e) {
return false;
}
}
}
實用乾貨——>日期工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.