java – 添加日期到日期
我有一个程序,需要从1/1/09开始,当我开始新的一天,我的程序将显示第二天.
GregorianCalendar startDate = new GregorianCalendar(2009,Calendar.JANUARY,1); SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy"); public void setStart() { startDate.setLenient(false); System.out.println(sdf.format(startDate.getTime())); } public void today() { newDay = startDate.add(5,1); System.out.println(newDay); //I want to add a day to the start day and when I start another new day,I want to add another day to that. } 我发现错误void but expected int,in’newDay = startDate.add(5,1);’ 解决方法Calendar 对象具有
add 方法,允许一个添加或减去指定字段的值.
例如, Calendar c = new GregorianCalendar(2009,1); c.add(Calendar.DAY_OF_MONTH,1); 用于指定字段的常量可以在 仅供将来参考,The Java API Specification包含有关如何使用作为Java API一部分的类的大量有用信息. 更新:
add方法不返回任何内容,因此,尝试分配调用Calendar.add的结果无效. 编译器错误表示一个人试图为int类型的变量分配一个void.这是无效的,因为不能将“nothing”赋给int变量. 只是一个猜测,但也许这可能是想要实现的: // Get a calendar which is set to a specified date. Calendar calendar = new GregorianCalendar(2009,1); // Get the current date representation of the calendar. Date startDate = calendar.getTime(); // Increment the calendar's date by 1 day. calendar.add(Calendar.DAY_OF_MONTH,1); // Get the current date representation of the calendar. Date endDate = calendar.getTime(); System.out.println(startDate); System.out.println(endDate); 输出: Thu Jan 01 00:00:00 PST 2009 Fri Jan 02 00:00:00 PST 2009 需要考虑的是日历实际上是什么. 日历不是日期的表示.它是一个日历的表示,它正在指向哪里.为了获取当前日历指向的位置,应使用 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java – 我们如何使用JPA计算LAST页面?
- Java语言实现Blowfish加密算法完整代码分享
- Selenium 2(WebDriver)Java Maven Eclipse Hello World Pro
- jdbc 使用PreparedStatement来存储和读取大数据(Blob或Clob
- log4j2 项目日志组件的实例代码
- java – 设置流回浏览器的Pdf的文件名
- SpringMVC自定义属性编辑器详解及实例
- ScreenUtils 可用于获取屏幕宽高,截屏的工具类
- java – Spring jdbcTemaplate如何一次性将完整的批量大小发
- java – 浏览器关闭时Servlet长时间处理取消
- 如何使用Selenium WebDriver Java获取浏览器控制
- 所有JVM /系统中的Java,Object.hashCode()结果常
- java – 为什么使用maven shade插件重定位不起作
- 如何从Java EE应用程序发送事件(推送通知)?
- java – 应用程序类’com.sun.xml.messaging.saa
- 详解Java正则表达式中Pattern类和Matcher类
- java – SimpleDateFormat parse(string str)在s
- java – jax-ws将内容类型更改为Content-Type,因
- java – 对序列进行分组是具有字典优先级的给定总
- Java中Json解析的方法分析