package util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.time.DateUtils;
/**
* @author wanglf
* @version 3.2.1
* @Package com.jfai.kg.kafkacomsu.util
* @Description: Java日期工具類 (注:本工具類默認異常全拋)
* @date 2018年6月30日14:43:25
* @Attention:
* 北京時間, China Standard Time,中國標準時間 縮寫CST
* 格林尼治標準時間,Greenwich Mean Time,縮寫 GMT,是指位於英國倫敦郊區的皇家格林尼治天文臺的標準時間.
* 由於地球在它的橢圓軌道里的運動速度不均勻,所以GMT可能與實際的太陽時有誤差,最大誤差達16分鐘。
* 現在的標準時間,是由原子鐘報時的協調世界時(UTC).
* 世界標準時間,Universal Time Coordinated,縮寫 UTC.現在最主要的世界時間標準
* 夏令時,Daylight Saving time,縮寫 DST
* @detail
* 比上一個版本功能增強.. 2018年7月1日 10:23:11
* getsomeTimes升級爲getSpecificTime 新增時分秒調整 2018年7月6日09:23:43
* 新增毫秒值轉date類型,以及毫秒值轉時間字符串 2018年7月13日09:23:46
* 引入Calendar類 2018年7月19日09:23:58
* 新增依據毫秒值獲取當前時延的功能 2018年7月21日09:23:05
* 新增transMills方法,可以將formatTime方法轉出來的字符串還原回毫秒值(經測試無誤,ok-~) 2018年7月25日17:11:23
* 新增依據日期獲取毫秒值的相應方法 2018年7月26日17:51:34
* 新增時間戳相關功能 2018年8月1日14:00:37
* 新增依據Date獲取時分秒毫秒及星期值功能 2018年8月3日09:25:43
* 新增Timezone和周,天在全年中的獲取方法 2018年8月3日16:04:44
* 新增季度天數首尾獲取方法 2018年8月5日12:11:09
* 新增java.sql.Date和java.util.Date之間的轉化方法 2018年8月11日18:05:03
* 新增依據格式化字符串,獲取當前時間字符串的功能 2018年8月18日15:02:59
* 更新默認format的獲取方法,爲每個線程分配一個默認的format,避免多線程的併發競爭 2018年8月22日17:52:00
* 新增Date之間的毫秒絕對值之差的計算方法 2018年12月18日15:40:13
* @remark
* 在Java 8中日期時間功能中,時區使用的是java.time.ZoneId類
* getOffset(time)方法返回的是以毫秒爲單位的,與世界標準時間UTC爲time的時差
* timeZone(如此段代碼若在上海時區運行,將返回28800,000,即正好8個小時,符合預期中國UTC+8的時區)
* TimeZone.getAvailableIDs(),可以列出TimeZone中所有可用的時區ID
*
*
*/
public class DateUtil {
public static final String DEFAULT_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
public static final String CHINA_FORMAT_PATTERN = "yyyy年MM月dd日";
public static final String TIME_FORMAT_PATTERN = "HH:mm:ss";
public static final String ORDER_FORMAT_PATTERN = "yyyyMMddHHmmss";
public static final String LOGGER_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss:SSS";
public static final String TIMESTAMP_FORMAT_PATTERN = "yyyyMMddHHmmssSSS";
public static final String WEEKDAY_FORMAT_PATTERN = "yyyy年MM月dd日 HH時mm分ss秒 EEEE";
public static final String CHINA_FORMAT_PATTERN_EX = "yyyy年MM月dd日 HH時mm分ss秒";
// 說明:使用ThreadLocal, 也是將共享變量變爲線程獨享,線程獨享一般肯定比方法獨享在併發環境中更能減少不少創建對象的開銷的
// 如果對性能要求比較高的情況下,一般推薦使用
private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public static SimpleDateFormat format = threadLocal.get();
// public static SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd
// HH:mm:ss");
/**
* 返回一個SimpleDateFormat("yyyy-MM-dd HH:mm:ss")對象
* @return
* @version 1.0.0
*/
public static SimpleDateFormat getDefaultFormat() {
if (threadLocal.get() != null) {
return threadLocal.get();
} else {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
threadLocal.set(format);
return format;
}
}
/**
* 獲得當前時間,返回格式爲yyyy-MM-dd HH:mm:ss
*/
public static String getCurrentTimeString() {
return format.format(Calendar.getInstance().getTime());
}
/**
* 以yyyy-MM-dd HH:mm:ss格式,返回當前時間字符串
*/
public static String getDateString() {
return getCurrentTimeString();
}
/**
* 返回格式 yyyy-MM-dd HH:mm:ss
*/
public static String getDateString(Date date) {
return format.format(date);
}
/**
* 以制定的格式返回當前日期的日期字符串
*/
public static String getDateString(String dateFormatStr) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormatStr);
return sdf.format(Calendar.getInstance().getTime());
}
/**
* 返回指定格式的時間字符串
*/
public static String getDateString(Date date, String dateFormatStr) {
SimpleDateFormat f = new SimpleDateFormat(dateFormatStr);
return f.format(date);
}
/**
* 以yyyy-MM-dd HH:mm:ss格式,返回毫秒值轉成的時間字符串
*/
public static String getDateString(Long mill) {
Date date = Calendar.getInstance().getTime();
date.setTime(mill);
return format.format(date);
}
/**
* 以指定的格式,返回毫秒值轉成的時間字符串
*/
public static String getDateString(Long mill, String dateFormatStr) {
SimpleDateFormat f = new SimpleDateFormat(dateFormatStr);
Date date = Calendar.getInstance().getTime();
date.setTime(mill);
return f.format(date);
}
/**
* 獲取當前日期時間
*
* @return Date
*/
public static Date getDate() {
return Calendar.getInstance().getTime();
}
/**
* 依據毫秒值獲取時間
*/
public static Date getDate(Long millis) {
Date date = Calendar.getInstance().getTime();
date.setTime(millis);
return date;
}
/**
* 依據sql.Date獲取util.Date
*/
public static Date getDate(java.sql.Date sqlDate) {
return new Date(sqlDate.getTime());
}
/**
* 依據傳入的日期字符串返回相應的Date時間類型,傳入時間格式爲默認的yyyy-MM-dd HH:mm:ss格式
*
* @param dateString 傳入的日期字符串
* @return java.util.Date
* @throws ParseException
*/
public static Date getDate(String dateString) throws ParseException {
return format.parse(dateString);
}
/**
* 依據傳入的日期字符串和日期格式化字符串返回相應的Date時間類型(注:默認爲java.util.Date)
*
* @param dateString 傳入的日期字符串
* @param dateFormatStr 日期格式化字符串
* @return java.util.Date
* @throws ParseException
*/
public static Date getDate(String dateString, String dateFormatStr) throws ParseException {
SimpleDateFormat f = new SimpleDateFormat(dateFormatStr);
return f.parse(dateString);
}
/**
* 獲取java.sql.Date
*/
public static java.sql.Date getsSqlDate() {
return new java.sql.Date(System.currentTimeMillis());
}
/**
* 獲取java.sql.Date,依據傳入的java.util.Date
*
* @param date java.util.Date
* @return ava.sql.Date
* @version 1.0.0
*/
public static java.sql.Date getsSqlDate(java.util.Date date) {
return new java.sql.Date(date.getTime());
}
/**
* 使用默認的日期格式化字符串格式化,格式傳入的時間字符串,返回java.util.Date時間類型
*
* @param dateStr
* @return java.util.Date時間類型
* @throws ParseException
*/
public static Date getDateFromDateStr(String dateStr) throws ParseException {
return format.parse(dateStr);
}
/**
* 依據傳入的日期字符串和日期格式化字符串返回相應的Date時間類型(注:默認爲java.util.Date)
*
* @param dateStr 傳入的日期字符串
* @param dateFormatStr 日期格式化字符串
* @return java.util.Date
* @throws ParseException
*/
public static Date getDateFromDateStr(String dateStr, String dateFormatStr) throws ParseException {
SimpleDateFormat f = new SimpleDateFormat(dateFormatStr);
return f.parse(dateStr);
}
/**
* 獲取時間戳,默認爲yyyyMMddHHmmssSSS格式,如20180801113148243
*
* @return 時間戳字符串
*/
public static String getTimestamp() {
SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT_PATTERN);
return f.format(Calendar.getInstance().getTime());
}
/**
* 依據傳入的日期格式化字符串,獲取相應的時間戳
*
* @param dateFormatStr 日期格式化字符串
* @return 時間戳字符串
*/
public static String getTimestamp(String dateFormatStr) {
SimpleDateFormat f = new SimpleDateFormat(dateFormatStr);
return f.format(Calendar.getInstance().getTime());
}
/**
* 獲取昨天此時的時間
*
* @return 昨天此時的日期 java.util.Date
*/
public static Date getYesterday() {
Calendar cale = Calendar.getInstance();
cale.add(Calendar.DATE, -1);
return cale.getTime();
}
/**
* 取得昨天此時的時間
*
* @return 昨天此時的日期 java.util.Date
*/
public static Date getYesterdayDate() {
return new Date(getMills() - 0x5265c00L);
}
/**
* 取得去過i天的時間
*
* @param i 過去時間天數
* @return 過去i天的日期 java.util.Date
*/
public static Date getPastdayDate(int i) {
return new Date(getMills() - 0x5265c00L * i);
}
/**
* 返回年份
*
* @param date java.util.Date
* @return 正常爲4位數的年數
*/
public static Integer getYear(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
String year = formatter.format(date);
return Integer.parseInt(year);
}
/**
* 返回月份
*
* @param date java.util.Date
* @return 月數
*/
public static Integer getMonth(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("MM");
String month = formatter.format(date);
return Integer.parseInt(month);
}
/**
* 返回天數
*
* @param date java.util.Date
* @return 當月的天數
*/
public static Integer getDay(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("dd");
String day = formatter.format(date);
return Integer.parseInt(day);
}
/**
* 返回小時數(24小時制)
*
* @param date java.util.Date
* @return 24小時的小時數
*/
public static Integer getHours(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("HH");
return Integer.parseInt(formatter.format(date));
}
/**
* 返回該日期的分鐘值
*
* @param date java.util.Date
* @return 分鐘數
*/
public static Integer getMinutes(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("HH");
return Integer.parseInt(formatter.format(date));
}
/**
* 該日期的秒值
*
* @param date java.util.Date
* @return 秒值
*/
public static Integer getSeconds(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("ss");
return Integer.parseInt(formatter.format(date));
}
/**
* 該日期的當前毫秒值
*
* @param date java.util.Date
* @return 該日期的當前毫秒值0-999
*/
public static Integer getMillsInThisSecond(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("SSS");
return Integer.parseInt(formatter.format(date));
}
/**
* 獲取該日期在一年中的天數
*
* @param date java.util.Date
* @return 一年中的天數
*/
public static Integer getdayOfYear(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("D");
return Integer.parseInt(formatter.format(date));
}
/**
* 獲取該日期在一年中的週數
*
* @param date
* @return 該日期在一年中的第幾周
*/
public static Integer getWeekOfYear(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("w");
return Integer.parseInt(formatter.format(date));
}
/**
* 該日期在月份中的週數
*
* @param date
* @return 該日期在是其當前月中的第幾周
*/
public static Integer getWeekOfMonth(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("W");
return Integer.parseInt(formatter.format(date));
}
/**
* 返回該日期的星期數
*
* @param date java.util.Date
* @return Integer之星期數, 如 星期五 返回5
*/
public static Integer getWeek(Date date) {
Calendar instance = Calendar.getInstance();
instance.setTime(date);
return instance.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
* 獲得當前月的第一天,返回格式爲yyyy-MM-dd HH:mm:ss
*
* @return 格式化後的時間字符串
*/
public static String getFirstDayOfMonth() {
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH, 1);
return format.format(cale.getTime());
}
/**
* 獲得當前月的最後一天,返回格式爲yyyy-MM-dd HH:mm:ss
*
* @return 格式化後的時間字符串
*/
public static String getLastDayOfMonth() {
Calendar cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
return format.format(cale.getTime());
}
/**
* 獲得下個月的第一天,返回格式爲yyyy-MM-dd HH:mm:ss
*
* @return 格式化後的時間字符串
*/
public static String getFirstDayOfNextMonth() {
Calendar cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 1);
return format.format(cale.getTime());
}
/**
* 獲取本週週一的日期,返回yyyy-MM-dd的日期格式字符串
*
* @return yyyy-MM-dd格式的日期字符串
*/
@SuppressWarnings("static-access")
public static String getMondayOFCurrentWeek() {
Calendar calendar = Calendar.getInstance();
calendar.add(calendar.DAY_OF_MONTH, 1 - calendar.DAY_OF_WEEK);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
return sdf.format(calendar.getTime());
}
/**
* 獲取本週週日的日期,返回yyyy-MM-dd的日期格式字符串
*
* @return yyyy-MM-dd格式的日期字符串
*/
@SuppressWarnings("static-access")
public static String getWeekdayOfCurrentWeek() {
Calendar calendar = Calendar.getInstance();
calendar.add(calendar.DAY_OF_MONTH, 7 - calendar.DAY_OF_WEEK);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
return sdf.format(calendar.getTime());
}
/**
* 獲取本季度第一天
*
* @return 本季度第一天, yyyy-MM-dd格式的日期字符串
* @Attention 中國以現陽曆3, 4, 5月份爲春季, 6, 7, 8月份爲夏季, 9, 10, 11月份爲秋季, 12, 1, 2月份爲冬季
*/
@SuppressWarnings("static-access")
public static String getSeasonFirstDay() {
Calendar calendar = Calendar.getInstance();
int month = calendar.get(2) + 1;
if (month >= 3 && month <= 5) {
calendar.set(calendar.MONTH, 3 - 1);
}
if (month >= 6 && month <= 8) {
calendar.set(calendar.MONTH, 6 - 1);
}
if (month >= 9 && month <= 11) {
calendar.set(calendar.MONTH, 9 - 1);
}
if (month == 12 && month <= 2) {
calendar.set(calendar.MONTH, 12 - 1);
}
calendar.set(calendar.DAY_OF_MONTH, 1);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
return sdf.format(calendar.getTime());
}
/**
* 獲取本季度最後一天,返回yyyy-MM-dd格式的日期字符串
*
* @return 本季度最後一天, yyyy-MM-dd格式的日期字符串
* @Attention 中國以現陽曆3, 4, 5月份爲春季, 6, 7, 8月份爲夏季, 9, 10, 11月份爲秋季, 12, 1, 2月份爲冬季
*/
@SuppressWarnings("static-access")
public static String getSeasonLastDay() {
Calendar calendar = Calendar.getInstance();
int month = calendar.get(2) + 1;
if (month >= 3 && month <= 5) {
calendar.set(calendar.MONTH, 5 - 1);
}
if (month >= 6 && month <= 8) {
calendar.set(calendar.MONTH, 8 - 1);
}
if (month >= 9 && month <= 11) {
calendar.set(calendar.MONTH, 11 - 1);
}
if (month == 12 && month <= 2) {
calendar.set(calendar.MONTH, 2 - 1);
}
calendar.set(calendar.DAY_OF_MONTH, 1);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
return sdf.format(calendar.getTime());
}
/**
* 獲取兩個日期之間間隔天數
*
* @param date1 java.util.Date
* @param date2 java.util.Date
* @return 兩個日期之間相隔的天數
* @Attention 返回的是絕對值, 所以兩個日期可以不分先後
*/
public static int getIntevalDays(Date date1, Date date2) {
Integer t1 = getdayOfYear(date1);
Integer t2 = getdayOfYear(date2);
return Math.abs(t1 - t2);
}
/**
* 獲得半年後的日期
*
* @return 半年後的日期
*/
public static Date getDateAfterHalfYear() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 6);
return calendar.getTime();
}
/**
* 獲取當前程序所在時區
*
* @return "CST+0800 中國標準時間"
* @remark 傳說這是中國標準時間:China Standard Time UT+8:00
* CST可視爲美國,澳大利亞,古巴或中國的標準時間
*/
public static String getTimezone() {
//return TimeZone.getDefault().toString();
return "CST+0800 中國標準時間";
}
/**
* 獲取傳入日期的時區
*
* @param date
* @return
*/
public static String getTimezone(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("zZ zzzz");
return sdf.format(date);
}
/**
* 得到當前星期數
*
* @return 當前的星期數
*/
public static int getCurrentWeekDay() {
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
}
// 依據傳入的月數,返回該月有多少天
//
/**
* 獲取系統當前時間的上幾天/周/月/年或者以後幾天/周/月/年的時間
*
* @param type :1 天 2 周 3 月 4 年 5 時 6分 7秒
* @param count:-1 上一 1 下一 -2 上二 2 下二...
* @param dateFormatStr :返回的時間樣式,如:yyyy-MM-dd hh:mm:ss
*/
public static String getSpecificTime(int type, int count, String dateFormatStr) {
Date date = null;
Calendar curr = Calendar.getInstance();
switch (type) {
case 1:
curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) + count);
date = curr.getTime();
break;
case 2:
curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) + 7 * count);
date = curr.getTime();
break;
case 3:
curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) + count);
date = curr.getTime();
break;
case 4:
curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + count);
date = curr.getTime();
break;
case 5:
curr.set(Calendar.HOUR_OF_DAY, curr.get(Calendar.HOUR_OF_DAY) + count);
date = curr.getTime();
break;
case 6:
curr.set(Calendar.MINUTE, curr.get(Calendar.MINUTE) + count);
date = curr.getTime();
break;
case 7:
curr.set(Calendar.SECOND, curr.get(Calendar.SECOND) + count);
date = curr.getTime();
break;
default:
break;
}
SimpleDateFormat f = new SimpleDateFormat(dateFormatStr);
return f.format(date);
}
/**
* 返回當前的的毫秒值(注意是java時間的毫秒值)
*
* @return 毫秒值
*/
public static Long getMills() {
return System.currentTimeMillis();
}
/**
* 依據傳入的日期對象,返回相應的毫秒值
*
* @param date java.util.Date對象
* @return 該日期對象相應的毫秒值
* @version 1.0.0
* @Attention 注意是java時間的毫秒值
*/
public static Long getMills(java.util.Date date) {
return date.getTime();
}
/**
* 依據傳入的傳入的兩個日期對象,返回相應的毫秒絕對值之差
*
* @param date java.util.Date對象
* @return 該日期對象相應的毫秒值
* @version 1.0.0
* @Attention 返回的是兩個java時間的毫秒絕對值之差
*/
public static Long getMills(java.util.Date date1,java.util.Date date2) {
return Math.abs(date1.getTime()-date2.getTime());
}
/**
* 依據傳入的日期字符串獲取相應的毫秒值
* 默認傳入的字符串格式 yyyy-MM-dd HH:mm:ss
*
* @param dateStr 傳入的日期字符串
* @return 毫秒值
* @throws ParseException
*/
public static Long getMills(String dateStr) throws ParseException {
Date date = format.parse(dateStr);
Calendar curr = Calendar.getInstance();
curr.setTime(date);
return curr.getTimeInMillis();
}
/**
* 依據傳入的日期字符串和日期格式化字符串獲取相應的毫秒值
* formatStr 備選值有DateUtil類裏的常量
*
* @throws ParseException
*/
public static Long getMills(String dateStr, String formatStr) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
Date date = sdf.parse(dateStr);
Calendar curr = Calendar.getInstance();
curr.setTime(date);
return curr.getTimeInMillis();
}
/**
* 獲取當前的納秒值
*/
public static Long getNanosecond() {
return System.nanoTime();
}
/**
* 依據傳入的毫秒值,獲取當前時延
*/
public static String getDelayTime(Long mills) {
return formatMills(System.currentTimeMillis() - mills);
}
/**
* 依據傳入的毫秒值,獲取當前時延
*/
public static String getDelayTime(String mills) {
long ms = Long.parseLong(mills);
return formatMills(System.currentTimeMillis() - ms);
}
/**
* 將傳入的毫秒值轉化時分秒毫秒得形式
*
* @param ms 毫秒值 注:支持毫秒值爲負數 的情況
* @return 轉化而來的時分秒毫秒字符串
* @version 2.0.0
*/
public static String formatMills(Long ms) {
if (ms == 0L) {
return "0毫秒";
}
boolean isPlus = true;
if (ms < 0L) {
isPlus = false;
ms = Math.abs(ms);
}
Long ss = 1000L;
Long mi = ss * 60;
Long hh = mi * 60;
Long dd = hh * 24;
Long day = ms / dd;
Long hour = (ms - day * dd) / hh;
Long minute = (ms - day * dd - hour * hh) / mi;
Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
StringBuffer sb = new StringBuffer();
if (day > 0) {
sb.append(day + "天");
}
if (hour > 0) {
sb.append(hour + "小時");
}
if (minute > 0) {
sb.append(minute + "分");
}
if (second > 0) {
sb.append(second + "秒");
}
if (milliSecond > 0) {
sb.append(milliSecond + "毫秒");
}
return isPlus == false ? "-" + sb.toString() : sb.toString();
}
/**
* 將formatMills轉出來的字符串,如**天**小時**分**秒**毫秒,還原回毫秒值
*
* @param str formatTime轉出來的字符串
* @return 轉化而來的毫秒值
* @Attention 如果返回0, 可能是參數有問題, 注意檢查參數~
*/
public static Long transMills(String str) {
if (isNull(str)) {
return 0L;
}
boolean isPlus = true;
if (str.charAt(0) == '-') {
isPlus = false;
}
String reg = "^[-?](?:(\\d*)天){0,1}(?:(\\d.*?)小時){0,1}(?:(\\d*)分){0,1}(?:(\\d*)秒){0,1}(?:(\\d*)毫秒){0,1}$";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
if (m.find()) {
long day = Long.parseLong(m.group(1) == null ? 0 + "" : m.group(1));
long hour = Long.parseLong(m.group(2) == null ? 0 + "" : m.group(2));
long min = Long.parseLong(m.group(3) == null ? 0 + "" : m.group(3));
long sec = Long.parseLong(m.group(4) == null ? 0 + "" : m.group(4));
long mill = Long.parseLong(m.group(5) == null ? 0 + "" : m.group(5));
Long mills = day * 24 * 60 * 60 * 1000 + hour * 60 * 60 * 1000 + min * 60 * 1000 + sec * 1000 + mill;
return isPlus == false ? (0 - mills) : mills;
}
return 0L;
}
/**
* 返回兩個日期是否是相同的天數
*
* @param date1 要比對的第一個日期
* @param date2 要比對的第二個日期
* @return true or false
*/
public static boolean isSameDay(Date date1, Date date2) {
return DateUtils.isSameDay(date1, date2);
}
/**
* 驗證日期字符串合法性
*
* @param str 日期字符串
* @param dateFormatStr 日期格式化字符串
* @return true or false
* @remark 使用了try-catch格式化的方式校驗日期
*/
public static boolean validateDateStr(String str, String dateFormatStr) {
SimpleDateFormat format = new SimpleDateFormat(dateFormatStr);
try {
// 設置lenient爲false. 否則SimpleDateFormat會比較寬鬆地驗證日期,比如2007/02/29會被接受,並轉換成2007/03/01
format.setLenient(false);
format.parse(str);
return true;
} catch (ParseException e) {
// 如果throw java.text.ParseException或者NullPointerException,就說明格式不對
return false;
}
}
/**
* 依據傳入的格式化日期字符串,驗證日期字符串合法性
*
* @param str 日期字符串
* @param formatStr 日期格式化字符串
* @return
*/
public static boolean checkDateStr(String str, String formatStr) {
return validateDateStr(str, formatStr);
}
/**
* 判斷字符是否爲空或空白字符串,或各種null組成
*/
private static boolean isNull(String str) {
return str == null || str.length() == 0 || "".equals(str.trim()) || "NULL".equals(str.trim().toUpperCase()) || "NULLNULL".equals(str.trim().toUpperCase()) || "NULLNULLNULL".equals(str.trim().toUpperCase());
}
public static void testCalendar(String[] args) {
Calendar calendar = Calendar.getInstance();
// System.out.println(calendar.get(0));//ERA 年代時代紀元
System.out.println(calendar.get(1));// 年份
System.out.println(calendar.get(2));// 減了1的月份
System.out.println(calendar.get(3));// WEEK_OF_YEAR
System.out.println(calendar.get(4));// WEEK_OF_MONTH
System.out.println(calendar.get(5));// 天數
System.out.println(calendar.get(6));// DAY_OF_YEAR
System.out.println(calendar.get(7));// DAY_OF_WEEK
System.out.println(calendar.get(8));// DAY_OF_WEEK_IN_MONTH
System.out.println(calendar.get(9));// AM_PM
System.out.println(calendar.get(10));// HOUR 12小時制
System.out.println(calendar.get(11));// HOUR_OF_DAY 24小時制
System.out.println(calendar.get(12));// 分數
System.out.println(calendar.get(13));// 秒值
System.out.println(calendar.get(14));// 毫秒值
System.out.println(calendar.get(15));// ZONE_OFFSET 28800000
System.out.println(calendar.get(16));// DST_OFFSET 0
System.out.println(DateUtil.getDateString(calendar.getTime(), LOGGER_FORMAT_PATTERN));
}
}
DateUtil 工具類 3.2.1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.