SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd")

LocalDate expirationDate
String expirDate="0"
Date date = df.parse(expirationDate as String) // 指定日期
expirDate = addDate(date, im.expiry2y).format("yyyy-MM-dd")  // 指定日期加上天数
得到一个新的日期
/**
* 效期增加天数,得到一个新的日期
* @param date
* @param day
* @return
* @throws Exception
*/
static Date addDate(Date date,long day) throws Exception {
long time = date.getTime(); // 得到指定日期的毫秒数
day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
time+=day; // 相加得到新的毫秒数
return new Date(time); // 将毫秒数转换成日期
}


下面代码是给日期年数增加年的时间
                        //把LocalDate 转date
ZoneId zoneId = ZoneId.systemDefault()
ZonedDateTime zdt = exdm.expirationDate.atStartOfDay(zoneId)
//给年数加两年
Calendar rightNow = Calendar.getInstance()
rightNow.setTime(Date.from(zdt.toInstant()))
rightNow.add(Calendar.YEAR, 2)//给年数增加两年,得到一个新的日期
expirDate = df.format(rightNow.getTime())

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd")
Integer daysToExpire=180
Integer warnShelfLife = 60 // 临期天数
if (isValidDate(batchPart)) {//判断时间格式是否正确
formatter.setLenient(false)
Date newDate = formatter.parse(batchPart)
formatter = new SimpleDateFormat("yyyy-MM-dd")
String ds = formatter.format(newDate)
Date d = formatter.parse(ds)
expirDate = df.format(new Date(d.getTime() + ((im.daysToExpire ?: 0) - (im.warnShelfLife ?: 0)) * 24 * 60 * 60 * 1000L)) as String

}
结果得到一个新的日期
/**
* 判断日期格式是否正确
* @param str
* @return
*/
static boolean isValidDate(String str) {
boolean convertSuccess = true
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd")
try {
// 设置lenient为false.
// 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false)
format.parse(str)
} catch (Throwable t) {
convertSuccess = false
}
return convertSuccess
}

===========================================================以下是网上找到的方法=======================================================
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); // 将毫秒数转换成日期
}
}

java 给时间增加时间得到一个新的时间(日期)的更多相关文章

  1. java8时间类API安全问题(赠送新的时间工具类哟)

    LocalDateTime等新出的日期类全是final修饰的类,不能被继承,且对应的日期变量都是final修饰的,也就是不可变类.赋值一次后就不可变,不存在多线程数据问题. simpleDateFor ...

  2. Java 从原字符串中截取一个新的字符串 subString()

    Java 手册 substring public String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串.该子字符串从指定索引处的字符开始,直 ...

  3. [转]JavaSE 8—新的时间和日期API

    为什么我们需要一个新的时间日期API Java开发中一直存在一个问题,JDK提供的时间日期API一直对开发者没有提供良好的支持. 比如,已有的的类(如java.util.Date和SimpleDate ...

  4. java8新特性——时间日期API

    传统的时间 API 存在线程安全的问题,在多线程开发中必须要上锁,所以 java8 现在为我们提供了一套全新的时间日期 API ,今天进来学习一下java8 的时间日期 API. 一.使用 Local ...

  5. Java 8新的时间日期库的20个使用示例

    原文链接 作者:Javin Paul 译者:之诸暇 除了lambda表达式,stream以及几个小的改进之外,Java 8还引入了一套全新的时间日期API,在本篇教程中我们将通过几个简单的任务示例来学 ...

  6. 【转】Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...

  7. Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...

  8. Java 8新的时间日期库,这二十个案例看完你还学不会算我的!!!

    Java对日期,日历及时间的处理一直以来都饱受诟病,尤其是它决定将java.util.Date定义为可修改的以及将SimpleDateFormat实现成非线程安全的.看来Java已经意识到需要为时间及 ...

  9. Java 8 (二) 新的时间API

    新的时间API 一)时间线 Instant对象:表示时间轴上的一个点,原点为1970-1-1的午夜. Duration对象:表示一段时间. 注意Instant和Duration类都是final. 二) ...

随机推荐

  1. Python:装饰器是如何调用的

    应用举例:(1)装饰器 # 装饰器的调用: # 一旦用上装饰器会: # 第一步:调outer函数 # 第二步:被装饰的函数play_game会被当作参数fn给outer # 第三步:最后调用play_ ...

  2. K8S(05)核心插件-ingress(服务暴露)控制器-traefik

    K8S核心插件-ingress(服务暴露)控制器-traefik 1 K8S两种服务暴露方法 前面通过coredns在k8s集群内部做了serviceNAME和serviceIP之间的自动映射,使得不 ...

  3. 【原创】Linux虚拟化KVM-Qemu分析(九)之virtio设备

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...

  4. woj1009 最短路 The Legend of Valiant Emigration

    title: woj1009 最短路 The Legend of Valiant Emigration date: 2020-03-07 categories: acm tags: [acm,最短路, ...

  5. Linux 应用开发----socket编程笔记

    Linux socket编程 套接字定义描述 套接字的域 AF_INET ====>IPv4 AF_INET6 ====>IPv6 AF_UNIX ====>unix 域 AF_UP ...

  6. macOS 显示/隐藏 AirPlay

    macOS 显示/隐藏 AirPlay AirPlay Sidecar 必须用相同的 Apple ID 登录 mac 和 ipad, 才能使用! https://www.apple.com/macos ...

  7. Flutter 1.5

    Flutter 1.5 Flutter SDK https://flutter.dev/docs/get-started/install/windows Android SDK This instal ...

  8. Raspberry Pi & Raspberry Pi 4

    Raspberry Pi & Raspberry Pi 4 pdf https://www.raspberrypi.org/magpi/issues/beginners-guide-2nd-e ...

  9. egg.js in action

    egg.js in action fullstack https://github.com/eggjs/egg/ cli config router service midlewares HTTP C ...

  10. 小程序 in action

    小程序 in action https://github.com/xgqfrms/xcx-taro taro https://taro-docs.jd.com/taro/docs/README.htm ...