java中年月日的加減法使用示例
更新時間:2023年02月24日 08:13:38 作者:mb622205c8dce06
這篇文章主要介紹了java中年月日的加減法使用示例的相關(guān)資料,需要的朋友可以參考下
java計算兩個年月日之間相差的天數(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計算年月之間的加法和減法:
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ù)則進行加法,如果是負(fù)數(shù),則進行減法 ? ?
? ? ? ? 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);
? ? }
? ??
? 計算兩個月份之間相差的月份個數(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)前月份進行傳值計算:
? 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)文章
Intellij idea下使用不同tomcat編譯maven項目的服務(wù)器路徑方法詳解
今天小編就為大家分享一篇關(guān)于Intellij idea下使用不同tomcat編譯maven項目的服務(wù)器路徑方法詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
java 從int數(shù)組中獲取最大數(shù)的方法
這篇文章主要介紹了java 從int數(shù)組中獲取最大數(shù)的方法,需要的朋友可以參考下2017-02-02
詳解SpringBoot中的tomcat優(yōu)化和修改
這篇文章主要介紹了詳解SpringBoot中的tomcat優(yōu)化和修改,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

