詳解JAVA常用的時(shí)間操作【實(shí)用】
項(xiàng)目中經(jīng)常有對(duì)時(shí)間進(jìn)行處理的需求,下面是一些常用的操作整理,方便以后再次使用以及做相關(guān)復(fù)習(xí)。
1.字符串轉(zhuǎn)換為日期
/** * 字符串轉(zhuǎn)換為日期 * @param dateStr 需要轉(zhuǎn)換的日期 * @param dateFormat 日期格式y(tǒng)yyy-MM-dd/yyyy-MM-dd HH:mm:ss */ public static Date toDate(String dateStr, SimpleDateFormat dateFormat) throws ParseException{ Date date = null; try { date = dateFormat.parse(dateStr); } catch (ParseException e) { logger.debug("Fail to convert String to Date, {}", dateStr); } return date; }
2.時(shí)間戳轉(zhuǎn)日期
/** * 時(shí)間戳轉(zhuǎn)日期 * @param date * @return */ public static String dateToTime(long time, SimpleDateFormat dateFormat) throws ParseException{ String data = null; try { dateFormat.format(new Date(time*1000)); } catch (Exception e) { logger.debug("Fail to convert long to Date, {}", time); } return data; }
3.日期格式化成字符串
/** * 日期格式化成字符串 * @param date * @param dateFormat * @return * @throws ParseException */ public static String toString(Date date, SimpleDateFormat dateFormat) throws ParseException{ return dateFormat.format(date); }
4.獲取指定日期之前或之后的日期 ,十分秒為00:00:00
/** * 獲取指定日期之前或之后的日期 * @param date * @param num 正數(shù)為之后,負(fù)數(shù)為之前 * @return yyyy-MM-dd 00:00:00 */ public static Date getSpecificDate(Date date, int num){ Calendar todayCal = Calendar.getInstance(); todayCal.setTime(date); Calendar c = Calendar.getInstance(); c.set(todayCal.get(Calendar.YEAR), todayCal.get(Calendar.MONTH), todayCal.get(Calendar.DAY_OF_MONTH) + num, 0, 0, 0); return c.getTime(); }
5.獲取指定日期之前或之后的日期 ,時(shí)分秒為當(dāng)前的
/** * 獲取指定日期之前或之后的日期 * @param date * @param num 正數(shù)為之前,負(fù)數(shù)為之后 * @return yyyy-MM-dd + 當(dāng)前的時(shí)分秒 */ public static Date getSpecificDateAndHhMmSs(Date date,int num){ Calendar c = Calendar.getInstance(); c.setTime(date); int day=c.get(Calendar.DATE); c.set(Calendar.DATE,day - num); return c.getTime(); }
6.將time類(lèi)型的時(shí)間字符串 轉(zhuǎn)換成 時(shí)、分
/** * 將time類(lèi)型的時(shí)間字符串 轉(zhuǎn)換成 時(shí)、分 * HH-mm-ss -->> HH-mm * @param time * @return */ public static String timeToHHMM(String time){ return time.substring(0, time.length() - 3); }
7.獲取某個(gè)日期的時(shí)、分
/** * 獲取某個(gè)日期的時(shí)、分 * @param date * @return HH-mm */ public static String getHM(Date date){ Calendar ca = Calendar.getInstance(); ca.setTime(date); Integer hour = ca.get(Calendar.HOUR_OF_DAY);//小時(shí) Integer minute = ca.get(Calendar.MINUTE);//分 String rs_hour = hour.toString(); String rs_minute = minute.toString(); if (rs_hour.length() == 1){ rs_hour = "0" + hour; } if(rs_minute.length() == 1){ rs_minute = "0" + minute; } return rs_hour + ":" + rs_minute; }
8.time類(lèi)型的時(shí)間字符串 -->> 零點(diǎn)開(kāi)始的秒數(shù)
/** * time類(lèi)型的時(shí)間字符串 -->> 零點(diǎn)開(kāi)始的秒數(shù) * @param time HH-mm / HH-mm-ss * @return */ public static Integer timeToSeconds(String time){ String[] timeSplit = null; int hours = 0,minutes = 0,seconds = 0; try { timeSplit = time.split(":"); if (timeSplit.length == 2) { hours = Integer.valueOf(timeSplit[0])*60*60; minutes = Integer.valueOf(timeSplit[1])*60; }else if(timeSplit.length == 3){ hours = Integer.valueOf(timeSplit[0])*60*60; minutes = Integer.valueOf(timeSplit[1])*60; seconds = Integer.valueOf(timeSplit[2]); }else{ logger.debug("Fail to convert the time, {}", time); } } catch (Exception e) { logger.debug("Fail to convert the time, {}", time); throw e; } return hours + minutes + seconds; }
9.零點(diǎn)開(kāi)始的秒數(shù)轉(zhuǎn)時(shí)間 -->> HH-mm-ss
/** * 零點(diǎn)開(kāi)始的秒數(shù)轉(zhuǎn)時(shí)間 -->> HH-mm-ss * @param durationSeconds * @return */ public static String getDuration(int durationSeconds){ int hours = durationSeconds /(60*60); int leftSeconds = durationSeconds % (60*60); int minutes = leftSeconds / 60; int seconds = leftSeconds % 60; StringBuffer sBuffer = new StringBuffer(); sBuffer.append(addZeroPrefix(hours)); sBuffer.append(":"); sBuffer.append(addZeroPrefix(minutes)); sBuffer.append(":"); sBuffer.append(addZeroPrefix(seconds)); return sBuffer.toString(); } public static String addZeroPrefix(int number){ if(number < 10) return "0"+number; else return ""+number; }
10.比較兩個(gè)日期相差的秒數(shù)
/** * 比較兩個(gè)日期相差的秒數(shù) * @param startDate * @param endDate * @return */ public static int getTimeSeconds(Date startDate,Date endDate) { long a = endDate.getTime(); long b = startDate.getTime(); return (int)((a - b) / 1000); }
11.判斷兩個(gè)時(shí)間段是否存在交集
/** * 比較兩個(gè)日期相差的秒數(shù) * @param startDate * @param endDate * @return */ public static int getTimeSeconds(Date startDate,Date endDate) { long a = endDate.getTime(); long b = startDate.getTime(); return (int)((a - b) / 1000); }
12.獲取指定日期是星期幾(1-7分別代表周一至周日)
/** * 獲取指定日期是星期幾(1-7分別代表周一至周日) * @return */ public static int DayOfWeek(Date date){ Calendar now = Calendar.getInstance(); now.setTime(date); boolean isFirstDay = (now.getFirstDayOfWeek() == Calendar.SUNDAY); int weekday = now.get(Calendar.DAY_OF_WEEK); if(isFirstDay){ weekday = weekday - 1; if(weekday == 0){ weekday = 7; } } return weekday; }
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- 關(guān)于JAVA經(jīng)典算法40題(超實(shí)用版)
- Java實(shí)現(xiàn)超級(jí)實(shí)用的日記本
- 簡(jiǎn)潔實(shí)用的Java Base64編碼加密異常處理類(lèi)代碼
- 20個(gè)非常實(shí)用的Java程序代碼片段
- Java無(wú)限級(jí)樹(shù)(遞歸)超實(shí)用案例
- Java Web十條開(kāi)發(fā)實(shí)用小知識(shí)
- Java用freemarker導(dǎo)出word實(shí)用示例
- java實(shí)用驗(yàn)證碼的實(shí)現(xiàn)代碼
- Java對(duì)象簡(jiǎn)單實(shí)用案例之計(jì)算器實(shí)現(xiàn)代碼
- 總結(jié)十個(gè)實(shí)用但偏執(zhí)的Java編程技術(shù)
相關(guān)文章
Java實(shí)現(xiàn)生成pdf并解決表格分割的問(wèn)題
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)生成pdf,并解決表格分割的問(wèn)題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11SpringBoot項(xiàng)目中的favicon.ico圖標(biāo)無(wú)法顯示問(wèn)題及解決
這篇文章主要介紹了SpringBoot項(xiàng)目中的favicon.ico圖標(biāo)無(wú)法顯示問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Spring Data JPA實(shí)現(xiàn)動(dòng)態(tài)條件與范圍查詢(xún)實(shí)例代碼
本篇文章主要介紹了Spring Data JPA實(shí)現(xiàn)動(dòng)態(tài)條件與范圍查詢(xún)實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-06-06IDEA創(chuàng)建方法時(shí)如何快速添加注釋
這篇文章主要介紹了IDEA創(chuàng)建方法時(shí)如何快速添加注釋問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02詳解SpringBoot中@SessionAttributes的使用
這篇文章主要通過(guò)示例為大家詳細(xì)介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07springboot+thymeleaf國(guó)際化之LocaleResolver接口的示例
本篇文章主要介紹了springboot+thymeleaf國(guó)際化之LocaleResolver的示例 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11