java日期工具類(lèi)實(shí)例分享
更新時(shí)間:2014年01月09日 10:05:43 作者:
本文介紹一個(gè)java日期工具類(lèi),功能有英文簡(jiǎn)寫(xiě)、英文全稱(chēng)、精確到毫秒的完整時(shí)間、中文簡(jiǎn)寫(xiě)、中文全稱(chēng)等方法
復(fù)制代碼 代碼如下:
/**
* 日期工具類(lèi)
* 默認(rèn)使用 "yyyy-MM-dd HH:mm:ss" 格式化日期
*/
public final class DateUtils {
/**
* 英文簡(jiǎn)寫(xiě)(默認(rèn))如:2010-12-01
*/
public static String FORMAT_SHORT = "yyyy-MM-dd";
/**
* 英文全稱(chēng) 如:2010-12-01 23:15:06
*/
public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
/**
* 精確到毫秒的完整時(shí)間 如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/**
* 中文簡(jiǎn)寫(xiě) 如:2010年12月01日
*/
public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
/**
* 中文全稱(chēng) 如:2010年12月01日 23時(shí)15分06秒
*/
public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH時(shí)mm分ss秒";
/**
* 精確到毫秒的完整中文時(shí)間
*/
public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH時(shí)mm分ss秒SSS毫秒";
/**
* 獲得默認(rèn)的 date pattern
*/
public static String getDatePattern() {
return FORMAT_LONG;
}
/**
* 根據(jù)預(yù)設(shè)格式返回當(dāng)前日期
* @return
*/
public static String getNow() {
return format(new Date());
}
/**
* 根據(jù)用戶(hù)格式返回當(dāng)前日期
* @param format
* @return
*/
public static String getNow(String format) {
return format(new Date(), format);
}
/**
* 使用預(yù)設(shè)格式格式化日期
* @param date
* @return
*/
public static String format(Date date) {
return format(date, getDatePattern());
}
/**
* 使用用戶(hù)格式格式化日期
* @param date 日期
* @param pattern 日期格式
* @return
*/
public static String format(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* 使用預(yù)設(shè)格式提取字符串日期
* @param strDate 日期字符串
* @return
*/
public static Date parse(String strDate) {
return parse(strDate, getDatePattern());
}
/**
* 使用用戶(hù)格式提取字符串日期
* @param strDate 日期字符串
* @param pattern 日期格式
* @return
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 在日期上增加數(shù)個(gè)整月
* @param date 日期
* @param n 要增加的月數(shù)
* @return
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
/**
* 在日期上增加天數(shù)
* @param date 日期
* @param n 要增加的天數(shù)
* @return
*/
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}
/**
* 獲取時(shí)間戳
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
/**
* 獲取日期年份
* @param date 日期
* @return
*/
public static String getYear(Date date) {
return format(date).substring(0, 4);
}
/**
* 按默認(rèn)格式的字符串距離今天的天數(shù)
* @param date 日期字符串
* @return
*/
public static int countDays (String date) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int)(t/1000 - t1/1000)/3600/24;
}
/**
* 按用戶(hù)格式字符串距離今天的天數(shù)
* @param date 日期字符串
* @param format 日期格式
* @return
*/
public static int countDays (String date, String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int)(t/1000 - t1/1000)/3600/24;
}
}
您可能感興趣的文章:
- java實(shí)現(xiàn)的日期時(shí)間轉(zhuǎn)換工具類(lèi)完整示例
- java常用工具類(lèi) Date日期、Mail郵件工具類(lèi)
- java日期時(shí)間操作工具類(lèi)
- java字符串與日期類(lèi)型轉(zhuǎn)換的工具類(lèi)
- Java日期處理工具類(lèi)DateUtils詳解
- Java日期工具類(lèi)DateUtils實(shí)例詳解
- Java 中DateUtils日期工具類(lèi)的實(shí)例詳解
- 實(shí)例解析Java日期格式工具類(lèi)DateUtil.java
- java日期處理工具類(lèi)
- java處理日期的工具類(lèi)DateUtil
相關(guān)文章
詳解Java并發(fā)編程之內(nèi)置鎖(synchronized)
這篇文章主要介紹了Java并發(fā)編程之內(nèi)置鎖(synchronized)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
JavaWeb實(shí)現(xiàn)用戶(hù)登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
這篇文章主要基于Servlet+JSP+JavaBean開(kāi)發(fā)模式實(shí)現(xiàn)JavaWeb用戶(hù)登錄注冊(cè)功能實(shí)例代碼,非常實(shí)用,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-05-05
springboot自定義starter方法及注解實(shí)例
這篇文章主要為大家介紹了springboot自定義starter方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
如何基于springboot-admin實(shí)現(xiàn)后臺(tái)監(jiān)控
這篇文章主要介紹了如何基于springboot-admin實(shí)現(xiàn)后臺(tái)監(jiān)控,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Java上傳文件進(jìn)度條的實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了Java上傳文件進(jìn)度條的實(shí)現(xiàn)方法,可簡(jiǎn)單實(shí)現(xiàn)顯示文件上傳比特?cái)?shù)及進(jìn)度的功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2015-12-12

