Java 日历取货日期格式问题

Java 日历取货日期格式问题,java,date,selenium,datetime,date-parsing,Java,Date,Selenium,Datetime,Date Parsing,我想从日历拾取中选择一个日期,以便使用SeleniumWebDriver自动化我的应用程序 我正在使用SimpleDataFormat类,在该类中,它们支持不同类型的日期格式。但是我不确定像6月14日这样的日期需要采用哪种格式,Wed不属于dd/mm/yyyy或任何其他可用格式 SimpleDateFormat format=new SimpleDateFormat("dd MMM, EEE"); 参考资料:要获取一周中的哪一天,可以使用java.util.Calendar: 由于您需要从1开

我想从日历拾取中选择一个日期,以便使用SeleniumWebDriver自动化我的应用程序

我正在使用SimpleDataFormat类,在该类中,它们支持不同类型的日期格式。但是我不确定像6月14日这样的日期需要采用哪种格式,Wed不属于dd/mm/yyyy或任何其他可用格式

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

参考资料:

要获取一周中的哪一天,可以使用java.util.Calendar:

由于您需要从1开始索引“Wed”,而不是一周中的4天,因此不需要遍历日历,只需重新格式化字符串:new SimpleDateFormatEE.formattate 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;
    }

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

您考虑过使用新的日期/时间API吗?旧的类Date、Calendar和SimpleDateFormat已经存在,它们正在被新的API所取代

如果使用java 8,请考虑使用这更容易


如果您使用的是Java,则从日期选择器中选择日期后,日期文本框中显示的格式并不重要。我们需要能够从日期选择器中选择特定日期。您能给我们提供url吗?仅供参考,诸如、和java.text.SimpleTextFormat之类的旧日期时间类现在已被这些类取代。看见
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;
    }
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
// create formatter with another format for output
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
System.out.println(outputFormatter.format(dt)); // 14/06/2017