java中年月日的加減法使用示例
java計(jì)算兩個年月日之間相差的天數(shù):
?public static int daysBetween(String smdate,String bdate) throws ParseException{ ? ? ? ? int daysInterval=0; ? ? ? ? if(StringUtils.isNoneBlank(smdate)&&StringUtils.isNoneBlank(bdate)){ ? ? ? ? ? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); ? ? ? ? ? ? ? Calendar cal = Calendar.getInstance(); ? ? ? ? ? ? ? ? cal.setTime(sdf.parse(smdate)); ? ? ? ? ? ? ? ? long time1 = cal.getTimeInMillis(); ? ? ? ? ? ? ? ?? ? ? ? ? ? ? cal.setTime(sdf.parse(bdate)); ? ? ? ? ? ? ? ? long time2 = cal.getTimeInMillis(); ? ? ? ?? ? ? ? ? ? ? long between_days=(time2-time1)/(1000*3600*24); ? ? ? ? ? ? daysInterval = Integer.parseInt(String.valueOf(between_days)); ? ? ? ? } ? ? ? ? ? ?? ? ? ? ?return daysInterval; ? ?? ? ? } ?
java計(jì)算年月之間的加法和減法:
public static void main(String[] args) throws ParseException ? ? { ? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); ?? ? ? ? ? String time=sdf.format(new Date());? ? ? ? ? Calendar cd = Calendar.getInstance();? ? ? ? ? cd.setTime(sdf.parse(time));? ? ? ? ? cd.add(Calendar.MONTH, 0);//往前一月,,如果是整數(shù)則進(jìn)行加法,如果是負(fù)數(shù),則進(jìn)行減法 ? ? ? ? ? ? Date date=cd.getTime(); ? ? ? ? ? String endTimeString = sdf.format(date); ? ? ? ? cd.add(Calendar.MONTH, -7);//往后7個月 ? ? ? ? String startTimeString = sdf.format(cd.getTime()); ? ? ? ? String startTime = startTimeString.replaceAll("-", "");//格式日期yyyyMM ? ? ? ? String endTime ?= endTimeString.replaceAll("-", "");//格式日期yyyyMM ? ? ? ? System.out.println(startTime); ? ? ? ? System.out.println(endTime); ? ? } ? ? ? ? ? ?? ? ? ? ??
獲取一個月中的第一天和最后一天:
public static void main(String[] args) ? ? { ? ? ? ? // 獲取當(dāng)前年份、月份、日期 ? ? ? ? ? Calendar cale = null; ? ? ? ? ? cale = Calendar.getInstance(); ? ? ? ? ? // 獲取當(dāng)月第一天和最后一天 ? ? ? ? ? SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); ? ? ? ? ? // 獲取前月的第一天 ? ? ? ? ? cale = Calendar.getInstance(); ? ? ? ? ? cale.add(Calendar.MONTH, -6); ? ? ? ? ? cale.set(Calendar.DAY_OF_MONTH, 1); ? ? ? ? ? String startTimeString = format.format(cale.getTime()); ? ? ? ? ? // 獲取前月的最后一天 ? ? ? ? ? cale = Calendar.getInstance(); ? ? ? ? ? cale.add(Calendar.MONTH, 0); ? ? ? ? ? cale.set(Calendar.DAY_OF_MONTH, 0); ? ? ? ? ? String endTimeString = format.format(cale.getTime()); ? ? ? ? ? String startTime =startTimeString.replaceAll("-", "");//格式日期yyyyMMdd ? ? ? ? String endTime ?=endTimeString.replaceAll("-", "");//格式日期yyyyMMdd ? ? ? ? String layerTime = startTimeString +"至"+endTimeString; ? ? ? ? System.out.println(layerTime); ? ? } ? ?? ?
計(jì)算兩個月份之間相差的月份個數(shù):
public int getMonthInterval(String str1,String str2) throws ParseException{ ? ? ? ? int monthLength = 0; ? ? ? ? if(StringUtils.isNoneBlank(str1)&& StringUtils.isNoneBlank(str2)){ ? ? ? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); ? ? ? ? ? ? Calendar bef = Calendar.getInstance(); ? ? ? ? ? ? Calendar aft = Calendar.getInstance(); ? ? ? ? ? ? bef.setTime(sdf.parse(str1)); ? ? ? ? ? ? aft.setTime(sdf.parse(str2)); ? ? ? ? ? ? int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH); ? ? ? ? ? ? int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12; ? ? ? ? ? ? monthLength = Math.abs(month + result); ? ? ? ? } ? ? ? ?return monthLength; ? ? } ? ?
java對當(dāng)前月份進(jìn)行傳值計(jì)算:
? public static Map<String,Object> getMonth(int length) throws ParseException{ ? ? ? ? Map<String, Object> map = new HashMap<>(); ? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); ?? ? ? ? ? String time=sdf.format(new Date());? ? ? ? ? Calendar cd = Calendar.getInstance();? ? ? ? ? cd.setTime(sdf.parse(time));? ? ? ? ? cd.add(Calendar.MONTH, -length);//往前一月 ? ? ? ? ? ? Date date=cd.getTime(); ? ? ? ? ? String monthFormat= sdf.format(date); // ? ? ? ?System.out.println(monthFormat); ? ? ? ? String monthString =monthFormat.replaceAll("-", "");//格式日期yyyyMM ? ? ? ? map.put("monthFormat", monthFormat); ? ? ? ? map.put("monthString", monthString); ? ? ? ? return map; ? ? }
到此這篇關(guān)于java中年月日的加減法使用示例的文章就介紹到這了,更多相關(guān)java中年月日的加減法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java面試題沖刺第十三天--數(shù)據(jù)庫(3)
這篇文章主要為大家分享了最有價值的三道數(shù)據(jù)庫面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-07-07Spring Boot熱加載jar實(shí)現(xiàn)動態(tài)插件的思路
本文主要介紹在 Spring Boot 工程中熱加載 jar 包并注冊成為 Bean 對象的一種實(shí)現(xiàn)思路,在動態(tài)擴(kuò)展功能的同時支持在插件中注入主程序的 Bean 實(shí)現(xiàn)功能更強(qiáng)大的插件2021-10-10Java8新特性之接口中的默認(rèn)方法和靜態(tài)方法
這篇文章主要介紹了Java8新特性之接口中的默認(rèn)方法和靜態(tài)方法的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07Spring?Boot?3.2.5集成mysql的詳細(xì)步驟記錄
作為一名Java開發(fā)者,我們經(jīng)常需要在我們的應(yīng)用程序中使用數(shù)據(jù)庫,在Spring Boot中集成數(shù)據(jù)庫是非常容易的,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot?3.2.5集成mysql的詳細(xì)步驟,需要的朋友可以參考下2024-04-04list轉(zhuǎn)tree和list中查找某節(jié)點(diǎn)下的所有數(shù)據(jù)操作
這篇文章主要介紹了list轉(zhuǎn)tree和list中查找某節(jié)點(diǎn)下的所有數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java實(shí)現(xiàn)矩陣加減乘除及轉(zhuǎn)制等運(yùn)算功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)矩陣加減乘除及轉(zhuǎn)制等運(yùn)算功能,結(jié)合實(shí)例形式總結(jié)分析了java常見的矩陣運(yùn)算實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01