您好,欢迎来到纷纭教育。
搜索
您的当前位置:首页java添加时间,在日期时间格式java中添加小时

java添加时间,在日期时间格式java中添加小时

来源:纷纭教育

SimpleDateFormat parser = new SimpleDateFormat("HH:mm");

Date time1 = parser.parse("7:30");

Now if I want to add 2 more hours to time1, like:

7:30 + 2 = 9:30

how do I add the 2 hours?

解决方案

java.util.Date is deprecated, you should use java.util.Calendar instead.

SimpleDateFormat parser = new SimpleDateFormat("HH:mm");

Date myDate = parser.parse("7:30");

Calendar cal =Calendar.getInstance();

cal.setTime(myDate);

cal.add(Calendar.HOUR_OF_DAY,2); // this will add two hours

myDate = cal.getTime();

And even better solution is to use Joda Time - Java date and time API.

From their website - Joda-Time provides a quality replacement for the Java date and time classes.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- fenyunshixun.cn 版权所有 湘ICP备2023022495号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务