【问题标题】:Split a date and inserting it to a web form拆分日期并将其插入 Web 表单
【发布时间】:2017-10-28 08:53:19
【问题描述】:

我有一个字符串格式为“01-23-2004”的日期。 我想拆分日期并将每天/每月/每年插入到 Web 表单中的不同文件中。

我这样做了:

public static void  dateInString()  
{
    String dt = "01-23-2004";
    String dateParts[] = dt.split("-");
    String month  = dateParts[0];
    String day  = dateParts[1];
    String year = dateParts[2];

}


public void insertBirthDateBounus() throws IOException, ParserConfigurationException, SAXException 
{
    comOps.selectDropDown(birthdayMonth,day);
    comOps.selectDropDown(birthdayDay, mounth);
    comOps.selectDropDown(birthdayYear, year);

}

那天我收到了一条错误消息,月份,年份 - 无法通过变量解析。

(comOps.selectDropDown 是从下拉列表中选择日期的选择。这是用我所有常用方法在不同的类中编写的)

【问题讨论】:

  • 你为什么不呢?当您在一种方法中声明变量并尝试在另一种方法中使用它们时,您认为会发生什么?
  • 除了变量被命名为month而不是mounth...

标签: java selenium


【解决方案1】:

这样使用。

1.让monthdayyear作为全局变量

2.在insertBirthDateBounus方法中使用

private static String month;
private static String day;
private static String year;

public static void dateInString() {
    String dt = "01-23-2004";
    // 1.format first
    SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy",Locale.ENGLISH);
    SimpleDateFormat sdf2 = new SimpleDateFormat("MMM-dd-yyyy",Locale.ENGLISH);
    Date date = null;
    try {
        date = sdf1.parse(dt);
        String newDate= sdf2.format(date);
        System.out.println(newDate);

        // 2.split
        String[] dateParts = newDate.split("-");
        month = dateParts[0];
        day = dateParts[1];
        year = dateParts[2];
        System.out.println(month);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

public void insertBirthDateBounus() throws IOException, ParserConfigurationException, SAXException {
    comOps.selectDropDown(birthdayMonth, day);
    comOps.selectDropDown(birthdayDay, month);
    comOps.selectDropDown(birthdayYear, year);
}

【讨论】:

  • 现在好了。没有错误。但是运行失败,因为应该插入的文件是月份。在方法中,月份是一个数字,而在网络中是这样的:Sep,Oct 等。我能做些什么来解决它?
  • 稍等。
  • 你可以检查一下。@libi
  • "e" 出现错误:未定义类型 Log 的方法 e(String, String)
  • 删除它@libi
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多