如何对date对象加一天呢?

书欣 Java经验 发布时间:2023-01-29 22:26:41 阅读数:7314 1
下文笔者讲述对date对象加一天操作的方法分享,如下所示
实现思路:
    1.将Date对象转换为Calendar对象
	2.使用calendar.add()方法即可实现对日期加指定天数的操作
public void test() {
	// 小时时间段
	String start = "08:30";
	String end = "00:00";

	// 时间格式
	SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-dd ");

	Date date = new Date();

	// 当前时间格式
	String currentTimeFormat = sdfOne.format(date);

	// 时间段范围格式转换
	String startTimeFormat = sdfTwo.format(date) + start;
	String endTimeFormat = sdfTwo.format(date) + end;

	// 解析成日期格式
	Date currentDate = null;
	Date startDate = null;
	Date endDate = null;
	try {
		currentDate = sdfOne.parse(currentTimeFormat);
		startDate = sdfOne.parse(startTimeFormat);
		endDate = sdfOne.parse(endTimeFormat);
		if (end != null && end.startsWith("00")) {
			// 时间段结束以00 开头,日期时间加一天
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(endDate);
			// 日期加一天
			calendar.add(Calendar.DATE, 1);
			endDate = calendar.getTime();
		}
	} catch (ParseException e) {
		e.printStackTrace();
	}
	System.out.println(currentDate);
	System.out.println(startDate);
	System.out.println(endDate);
	 
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202301/16750027885573.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者