【问题标题】:Error with Calendar after addition and then converting to date添加后日历出错,然后转换为日期
【发布时间】:2023-04-01 21:29:01
【问题描述】:

我有以下 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: 无法将给定对象格式化为日期时发生异常。]。


我需要帮助检查该方法,我有一个调用此方法的主脚本,其中 addDay 需要改变的能力。需要返回,因为在主脚本中它将日期发送到另一个方法。我正在使用脚本进行测试自动化


更新

我试图在代码中自动插入日期,所以今天是 2011 年 10 月 21 日,我需要自动更改可能 27 天后的日期。在日历方法中,无法更改格式,而在日期方法中,没有选项可以添加到当前日期。

【问题讨论】:

  • 尚不清楚为什么要提取这些字段...为什么不直接使用formatter.format(today.getTime())?也不清楚 what 在这里失败了。你的测试代码在做什么?
  • 这和函数式编程有什么关系?
  • 它应该说我正在使用的 IBM 程序的理性功能测试器。我更新了问题。

标签: java simpledateformat


【解决方案1】:

我建议

  • 使用Joda-Time
  • 返回Date[Time/Midnight] 而不是String

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 2015-04-27
    • 1970-01-01
    • 2014-12-11
    • 2022-01-10
    • 2021-04-15
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多