添加然后转换为日期后日历出错
我有以下 JAVA 代码
public static String getstartDateEvent (int addDay) {
Calendar today = Calendar.getInstance();
today.add(Calendar.DATE,addDay);
int year = today.get(Calendar.YEAR);
int month = today.get(Calendar.MONTH);
int day = today.get(Calendar.DATE);
Date date = new Date(year - 1900, month, day);
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String fileDate = formatter.format(date);
return fileDate;
}
运行代码后,出现以下错误:
播放脚本 [TestScripts.Test_applications] [CRFCN0019E: RationalTestScriptException on line 0 of script TestScripts.BlankScript - java.lang.IllegalArgumentException: Cannot format给定时发生异常作为日期的对象。]。
我需要帮助检查该方法,我有一个调用此方法的主脚本,其中 addDay 需要改变的能力。需要返回,因为在主脚本中它将日期发送到另一个方法。 脚本
我正在使用测试自动化更新
,我试图将自动日期插入到代码中,所以今天是 2011 年 10 月 21 日,我需要自动更改可能 27 天后的日期。在日历方法中,无法更改格式,并且在日期方法中,没有选项可以对当前日期进行添加。
I have the following JAVA code
public static String getstartDateEvent (int addDay) {
Calendar today = Calendar.getInstance();
today.add(Calendar.DATE,addDay);
int year = today.get(Calendar.YEAR);
int month = today.get(Calendar.MONTH);
int day = today.get(Calendar.DATE);
Date date = new Date(year - 1900, month, day);
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String fileDate = formatter.format(date);
return fileDate;
}
After I run the code I get the following error:
Exception occurred during playback of script [TestScripts.Test_applications] [CRFCN0019E: RationalTestScriptException on line 0 of script TestScripts.BlankScript - java.lang.IllegalArgumentException: Cannot format given Object as a Date.].
I need help checking the method, I have a main script calling this method, where addDay
need the ability to change. The return is needed because in the main script it sents the date to another method. I am using the script for test automation
Update
I trying to have a automated date insert into the code so today is 10/21/2011 and I need to automatically change the date of the maybe 27 days later. In the calendar method the ability to change the format is not avaibable and in the date method there is no option to do an addition to the current date.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议
Date[Time/Midnight]
而不是字符串
I suggest to
Date[Time/Midnight]
instead of aString