日历提货日期格式问题

时间:2017-06-14 05:43:14

标签: java date selenium datetime date-parsing

我想从日历选择中选择一个日期,以使用selenium webdriver自动化我的应用程序。

我使用的是SimpleDateFormat类,它支持不同类型的日期格式。但我不确定"14 Jun, Wed"之类的日期需要采用哪种格式,而这些日期不属于dd/mm/yyyy或任何其他可用格式。

3 个答案:

答案 0 :(得分:1)

SimpleDateFormat format=new SimpleDateFormat("dd MMM, EEE");

参考:https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

答案 1 :(得分:1)

要获取星期几,您可以使用java.util.Calendar

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

由于您需要'Wed'而不是4(星期几从1开始编制索引),而不是通过日历,只需重新格式化字符串:new SimpleDateFormat("EE").format(date)(EE表示“星期几,短版本” “)。

要在字符串中获取当前月份,请通过以下方式调用此函数:

String month = getMonthForInt(Calendar.getInstance().get(Calendar.MONTH))
//**********************************************************************
String getMonthForInt(int num) {
        String month = "wrong";
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] months = dfs.getMonths();
        if (num >= 0 && num <= 11 ) {
            month = months[num];
        }
        return month;
    }

然后以你想要的格式连接两个字符串。

答案 2 :(得分:1)

您是否考虑过使用新的日期/时间API?旧类(DateCalendarSimpleDateFormat)有lots of problems,并且它们将被新API替换。

如果您使用 Java 8 ,请考虑使用new java.time API。它更容易,less bugged and less error-prone than the old APIs

如果您正在使用 Java&lt; = 7 ,则可以使用ThreeTen Backport,这是Java 8新日期/时间类的绝佳后端。对于 Android ThreeTenABP(更多关于如何使用它here)。

以下代码适用于两者。 唯一的区别是包名称(在Java 8中为java.time而在ThreeTen Backport(或Android的ThreeTenABP中)为org.threeten.bp),但类和方法名称是一样的。

首先,您需要创建DateTimeFormatter以匹配格式14 Jun, Wed。因此,我们需要使用此模式(日/月/工作日)创建格式化程序。由于月份名称和工作日是英语,因此即使您的系统的默认语言环境不是英语,我们也会使用java.util.Locale类来确保其有效。

还有另一个问题:采用这种格式(14 Jun, Wed),没有。所以我们有两种选择:

  1. 假设当前年度
  2. 搜索与日/月/工作日相匹配的年份:我考虑到,如果输入是14 Jun, Tue,我们需要找到一年中&#34; 14君&#34;是星期二(因此,我们不能假设它总是当年)。
  3. 我正在为两者编写代码。在此解析中,我使用LocalDate类,因为它似乎是您案例的最佳选择(此类表示日期,月份和年份的日期):

    String input = "14 Jun, Wed";
    
    // alternative 1: formatter assuming it's always the current year
    DateTimeFormatter formatter1 = new DateTimeFormatterBuilder()
        // pattern matching day/month/weekday
        .appendPattern("dd MMM, EEE")
        // assume current year as default
        .parseDefaulting(ChronoField.YEAR, Year.now().getValue())
        // use English locale (to make sure month and weekday are parsed correctly)
        .toFormatter(Locale.ENGLISH);
    LocalDate dt = LocalDate.parse(input, formatter1);
    System.out.println(dt); // 2017-06-14
    
    // alternative 2: parse day and month, find a year that matches the weekday
    DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM, EEE", Locale.ENGLISH);
    // get parsed object (think of this as an intermediary object that contains all the parsed fields)
    TemporalAccessor parsed = formatter2.parse(input);
    // get the current year
    int year = Year.now().getValue();
    // get the parsed weekday (Wednesday)
    DayOfWeek weekday = DayOfWeek.from(parsed);
    // try a date with the parsed day and month, at this year
    MonthDay monthDay = MonthDay.from(parsed);
    dt = monthDay.atYear(year);
    // find a year that matches the day/month and weekday
    while (dt.getDayOfWeek() != weekday) {
        // I'm going to previous year, but you can choose to go to the next (year++) if you want
        year--;
        dt = monthDay.atYear(year);
    }
    System.out.println(dt); // 2017-06-14
    

    两者的输出是:

      

    2017年6月14日

    请注意,如果输入为14 Jun, Tue,则替代1将抛出异常(因为工作日与当前年份匹配)和备选2(代码启发(不是说复制){ {3}})将查找与工作日匹配的一年(在这种情况下,它会找到2016-06-14)。 由于不清楚您想要的替代方案,只需根据您的使用情况选择一个。

    输出格式(2017-06-14)是LocalDate.toString()的结果。如果要更改它,只需创建另一个格式化程序:

    // create formatter with another format for output
    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    System.out.println(outputFormatter.format(dt)); // 14/06/2017
    

    对于上面的示例,输出为:

      

    14/06/2017

    您可以查看this answer以获取有关可用格式的更多详细信息。

相关问题