Java 日期格式加上指定月数(一个期限)得到一个新日期的实现代码

时间:2021-05-02

下面一段实例代码给大家介绍java日期格式加上指定月数得到一个新日期,具体代码如下所示:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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; }

下面给大家补充介绍java中一个指定日期加上指定天数得到新日期的实现代码

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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(); // 得到指定日期的毫秒数 day = day*24*60*60*1000; // 要加上的天数转换成毫秒数 time+=day; // 相加得到新的毫秒数 return new date(time); // 将毫秒数转换成日期 } }

原文链接:https://blog.csdn.net/hrlsnow/article/details/80266845

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章