java 在将excel数据读入Selenium时,我试图通过解析字符串来获取日期和年份值作为整数,

i86rm4rw  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(94)

它们不会被解析,而是显示如下错误:对于输入字符串:“五”
请帮助我如何解析它们,请告诉我,如果我做错了什么地方?
代码:

String day = cellValue.split("-")[0];
int intDay = Integer.parseInt(day);

Excel数据:

"5-Feb-2000"
iaqfqrcu

iaqfqrcu1#

普通java.time ...

获取所需值的一种简单方法是,使用支持该格式的DateTimeFormatter解析String,并直接访问结果LocalDate中的值。

public static void main(String[] args) {
    // input value
    String input = "5-Feb-2000";
    // the DateTimeFormatter to be used as a parser
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM-uuuu", Locale.ENGLISH);
    // use the parser to get a LocalDate from the input String
    LocalDate localDate = LocalDate.parse(input, dtf);
    // print its toString() (implicitly)
    System.out.println(localDate);
    // print the day of month
    System.out.println("day of month:  " + localDate.getDayOfMonth());
    // print the month of year
    System.out.println("month of year: " + localDate.getMonthValue());
    // print the year
    System.out.println("year:          " + localDate.getYear());
}

输出:

2000-02-05
day of month:  5
month of year: 2
year:          2000

如果你有一个 *birthday字段,它有3个不同的字段,分别是日期,月份,年份 *,你可以使用我的示例代码的最后三个print语句中调用的方法,取它们的值并插入到需要的地方...

q3qa4bjr

q3qa4bjr2#

尝试将String值解析为Date对象,将Date对象解析为LocateDate。LocalDate对象包含日期、月份、年份字段...

String cellValue ="5-Feb-2000";
        SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
        
        Date parsedDate= fmt.parse(cellValue);
        System.out.println(parsedDate);
        LocalDate localDate = parsedDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        
        System.out.println("Date= "+localDate.getDayOfMonth()+"  Month = "+localDate.getMonthValue());

相关问题