Java 日期格式加上指定月數(shù)(一個(gè)期限)得到一個(gè)新日期的實(shí)現(xiàn)代碼
下面一段實(shí)例代碼給大家介紹java日期格式加上指定月數(shù)得到一個(gè)新日期,具體代碼如下所示:
public static Date getnewDate(Date olddate, String recordDate) throws ParseException { Date date = olddate; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String data = format.format(date); String dataStr[] = data.split("-"); //年份 int year = (Integer.parseInt(dataStr[1]) + Integer.parseInt(recordDate))/12; //月份 int yue = (Integer.parseInt(dataStr[1]) + Integer.parseInt(recordDate))%12; String a = ""; if(yue<10){ if(yue<1){ a = "12"; }else{ a = "0"+yue; } }else { a = yue+""; } dataStr[0]=String.valueOf(Integer.parseInt(dataStr[0]) + year); dataStr[1]=a; String newdata = dataStr[0]+"-"+dataStr[1]+"-"+dataStr[2]; Date newDate = format.parse(newdata); return newDate; }
下面給大家補(bǔ)充介紹Java中一個(gè)指定日期加上指定天數(shù)得到新日期的實(shí)現(xiàn)代碼
package com.date.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Test { public static void main(String[] args) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 日期格式 Date date = dateFormat.parse("2015-07-31"); // 指定日期 Date newDate = addDate(date, 20); // 指定日期加上20天 System.out.println(dateFormat.format(date));// 輸出格式化后的日期 System.out.println(dateFormat.format(newDate)); } public static Date addDate(Date date,long day) throws ParseException { long time = date.getTime(); // 得到指定日期的毫秒數(shù) day = day*24*60*60*1000; // 要加上的天數(shù)轉(zhuǎn)換成毫秒數(shù) time+=day; // 相加得到新的毫秒數(shù) return new Date(time); // 將毫秒數(shù)轉(zhuǎn)換成日期 } }
相關(guān)文章
Java代碼實(shí)現(xiàn)對(duì)properties文件有序的讀寫的示例
本篇文章主要介紹了Java代碼實(shí)現(xiàn)對(duì)properties文件有序的讀寫的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-11-11關(guān)于Spring?Cloud實(shí)現(xiàn)日志管理模塊
這篇文章主要介紹了關(guān)于Spring?Cloud實(shí)現(xiàn)日志管理模塊問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11MyBatis 添加元數(shù)據(jù)自定義元素標(biāo)簽的實(shí)現(xiàn)代碼
這篇文章主要介紹了MyBatis 添加元數(shù)據(jù)自定義元素標(biāo)簽的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07springboot 通過代碼自動(dòng)生成pid的方法
這篇文章主要介紹了springboot 通過代碼自動(dòng)生成pid的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-07-07SpringBoot集成Redis實(shí)現(xiàn)消息隊(duì)列的方法
這篇文章主要介紹了SpringBoot集成Redis實(shí)現(xiàn)消息隊(duì)列的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Springcould多模塊搭建Eureka服務(wù)器端口過程詳解
這篇文章主要介紹了Springcould多模塊搭建Eureka服務(wù)器端口過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11IDEA 自帶的數(shù)據(jù)庫(kù)工具真的很牛(收藏版)
這篇文章主要介紹了IDEA 自帶的數(shù)據(jù)庫(kù)工具真的很牛(收藏版),本文以 IntelliJ IDEA/ Mac 版本作為演示,其他版本的應(yīng)該也差距不大,需要的朋友可以參考下2021-04-04Java實(shí)現(xiàn)短信驗(yàn)證碼詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)短信驗(yàn)證碼的相關(guān)資料, 在業(yè)務(wù)需求中我們經(jīng)常會(huì)用到短信驗(yàn)證碼,比如手機(jī)號(hào)登錄、綁定手機(jī)號(hào)、忘記密碼、敏感操作等,需要的朋友可以參考下2023-09-09