Java Date時(shí)間類型的操作實(shí)現(xiàn)
本文主要介紹Java Date 日期類型,以及Calendar的怎么獲取時(shí)間,然后寫成時(shí)間工具類里面有下面這些方法:
- - 時(shí)間轉(zhuǎn)字符串(有默認(rèn)時(shí)間格式,帶時(shí)間格式)
- - 字符串轉(zhuǎn)時(shí)間(有默認(rèn)時(shí)間格式,帶時(shí)間格式)
- - 計(jì)算兩個(gè)日期之間相差的天數(shù)
- - 計(jì)算當(dāng)前時(shí)間多少天以后的日期
- - 判斷是否是日期格式
代碼
很多說明都注釋在代碼上:
package datedemo;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
/**
?* 日期工具
?*/
public class DateUtil {
? ? private final static String A="yyyy-MM-dd";//日期格式
? ? private final static String B="yyyy-MM-dd HH:mm:ss";//日期格式
? ? private final static String C="yyyy/MM/dd HH:mm:ss";//日期格式
? ? private final static String exp="((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(10|12|0?[13578])([-\\/\\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(11|0?[469])([-\\/\\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(0?2)([-\\/\\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([3579][26]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$))";
? ? /**
? ? ?* 時(shí)間類型轉(zhuǎn)字符串 固定返回 日期為 yyyy-MM-dd
? ? ?* @param date
? ? ?* @return String
? ? ?*/
? ? public static String getDateToString(Date date) throws Exception{
? ? ? ? String s;
? ? ? ? SimpleDateFormat sft=new SimpleDateFormat(A);//格式時(shí)間對象
? ? ? ? s=sft.format(date);
? ? ? ? return s;
? ? }
? ? /**
? ? ?* 時(shí)間類型轉(zhuǎn)字符串 不固定日期格式
? ? ?* @param date
? ? ?* @param format
? ? ?* @return String
? ? ?*/
? ? public static String getDteToString (Date date,String format) throws Exception{
? ? ? ? String s;
? ? ? ? SimpleDateFormat sft=new SimpleDateFormat(format);//格式時(shí)間對象
? ? ? ? s=sft.format(date);
? ? ? ? return s;
? ? }
? ? /**
? ? ?* 字符串時(shí)間轉(zhuǎn)時(shí)間類型 固定日期格式 yyyy-MM-dd
? ? ?* @param text 字符串時(shí)間
? ? ?* @return Date
? ? ?* @throws Exception
? ? ?*/
? ? public static Date getStringToDate(String text) throws Exception{
? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat(A);//格式時(shí)間對象
? ? ? ? Date date = sdf.parse(text);
? ? ? ? return date;
? ? }
? ? /**
? ? ?* 字符串時(shí)間轉(zhuǎn)時(shí)間類型 不固定時(shí)間格式
? ? ?* @param text 時(shí)間字符串
? ? ?* @param format 日期格式
? ? ?* @return Date
? ? ?* @throws Exception
? ? ?*/
? ? public static Date gettringToDate(String text,String format) throws Exception{
? ? ? ? SimpleDateFormat sdf=new SimpleDateFormat(format);//格式時(shí)間對象
? ? ? ? Date date=sdf.parse(text);
? ? ? ? return date;
? ? }
? ? /**
? ? ?*計(jì)算兩個(gè)日期之間相差的天數(shù)
? ? ?* @param a 第一個(gè)日期時(shí)間
? ? ?* @param b 第二個(gè)日期時(shí)間
? ? ?* @return
? ? ?* @throws Exception
? ? ?*/
? ? public static long getDaysBetweenTwoDates(Date a, Date b) throws Exception {
? ? ? ? //判斷這兩個(gè)時(shí)間的大小
? ? ? ? if(a.equals(b)) return 0;
? ? ? ? if(!a.before(b)){//保證返回的值為正數(shù)
? ? ? ? ? ? Date temp;
? ? ? ? ? ? temp=a;
? ? ? ? ? ? a=b;
? ? ? ? ? ? b=temp;
? ? ? ? }
? ? ? ? Calendar c = Calendar.getInstance();//獲取calendar對像
? ? ? ? c.setTime(a);//設(shè)置時(shí)間 date ?轉(zhuǎn) calendar 類型
? ? ? ? long t1 = c.getTimeInMillis();//獲取時(shí)間戳
? ? ? ? c.setTime(b);
? ? ? ? long t2 = c.getTimeInMillis();
? ? ? ? //計(jì)算天數(shù)
? ? ? ? long days = (t2 - t1) / (24 * 60 * 60 * 1000);
? ? ? ? return days;
? ? }
? ? /**
? ? ?* 計(jì)算當(dāng)前時(shí)間多少天以后的日期
? ? ?* @param currentDate 當(dāng)前時(shí)間
? ? ?* @param distance 距離多少天
? ? ?* @return
? ? ?*/
? ? public static Date getNextDasByNumber(Date currentDate,int distance)throws Exception{
? ? ? ? Calendar calendar=Calendar.getInstance();//獲取日歷對象
? ? ? ? calendar.setTime(currentDate);//設(shè)置當(dāng)前時(shí)間 date ?轉(zhuǎn) calendar 類型
? ? ? ? calendar.add(Calendar.DATE,distance);//計(jì)算離當(dāng)前時(shí)間以后的日期
? ? ? ? Date date=calendar.getTime();//calendar 轉(zhuǎn) date ?類型
? ? ? ? return date;
? ? }
? ? /**
? ? ?* 判斷是否是日期格式
? ? ?* @param date ?字符串
? ? ?* @return
? ? ?*/
? ? public static boolean isDate(String date){
? ? ? ?// 創(chuàng)建 Pattern 對象 java正則表達(dá)式對象
? ? ? ? Pattern r = Pattern.compile(exp);
? ? ? ? boolean flag = ?r.matcher(date).matches();//判斷它格式是否正確
? ? ? ? return flag;
? ? }
? ? public static void main(String []arg){
? ? ? ? Calendar calendar=Calendar.getInstance();
? ? ? ? try {
? ? ? ? ? ? //驗(yàn)證計(jì)算兩個(gè)日期之間相差的天數(shù)
? ? ? ? ? ? long i= getDaysBetweenTwoDates(getStringToDate("2017-02-11"),getStringToDate("2017-03-11"));
? ? ? ? ? ? System.out.println("計(jì)算(2017-02-11,2017-03-11)兩個(gè)日期之間相差的天數(shù):"+i);
? ? ? ? ? ? Date d= getNextDasByNumber(getStringToDate("2017-02-11"),28);
? ? ? ? ? ? System.out.println("計(jì)算(2017-02-11)時(shí)間128天以后的日期:"+getDateToString(d));
? ? ? ? ? ? boolean f= isDate("2017-02-11");
? ? ? ? ? ? System.out.println("判斷2017-02-11日期格式是否正確:"+f);
? ? ? ? ? ? boolean f1= isDate("20170211");//正則表達(dá)式不支持這種
? ? ? ? ? ? System.out.println("判斷20170211日期格式是否正確:"+f1);
? ? ? ? ? ? boolean f2= isDate("2017/02/11");
? ? ? ? ? ? System.out.println("判斷2017/02/11日期格式是否正確:"+f2);
? ? ? ? ? ? System.out.println();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}結(jié)果

總結(jié)
Calendar比Java原有的Date更加強(qiáng)大些,它們之間也可以相互轉(zhuǎn)換 ,不過用Calendar,小心獲取月份時(shí),它是陰歷–得加一才是我們正常的月份;
就說這么多了,都是干貨哈?。。?,大家有不明白的或者有新的需求都可提出來,一起討論討論;
到此這篇關(guān)于Java Date時(shí)間類型的操作實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java Date類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于logback 實(shí)現(xiàn)springboot超級詳細(xì)的日志配置
java web 下有好幾種日志框架,比如:logback,log4j,log4j2(slj4f 并不是一種日志框架,它相當(dāng)于定義了規(guī)范,實(shí)現(xiàn)了這個(gè)規(guī)范的日志框架就能夠用 slj4f 調(diào)用)。這篇文章主要介紹了基于logback springboot超級詳細(xì)的日志配置,需要的朋友可以參考下2019-06-06
SpringBoot整合MybatisPlus的簡單教程實(shí)現(xiàn)(簡單整合)
這篇文章主要介紹了SpringBoot整合MybatisPlus的簡單教程實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
解決使用RestTemplate時(shí)報(bào)錯(cuò)RestClientException的問題
這篇文章主要介紹了解決使用RestTemplate時(shí)報(bào)錯(cuò)RestClientException的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot如何配置MySQL和Oracl雙數(shù)據(jù)源(Mybatis)
這篇文章主要介紹了SpringBoot如何配置MySQL和Oracl雙數(shù)據(jù)源(Mybatis)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
詳解SpringBoot構(gòu)建的Web項(xiàng)目如何在服務(wù)端校驗(yàn)表單輸入
這篇文章主要介紹了詳解SpringBoot構(gòu)建的Web項(xiàng)目如何在服務(wù)端校驗(yàn)表單輸入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
ssm整合之Spring整合MyBatis框架配置事務(wù)的詳細(xì)教程
這篇文章主要介紹了ssm整合之Spring整合MyBatis框架配置事務(wù),本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

