简体   繁体   English

将字符串转换为 java.time.localDate

[英]Convert String to java.time.localDate

1.way 1.方式

    LocalDate ld = LocalDate.parse(reservationDTO.getReservationDate());

2.way 2种方法

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate dateTime = LocalDate.parse(reservationDTO.getReservationDate(), 
    formatter);

3.way 3.路

LocalDate localDate = LocalDate.parse(reservationDTO.getReservationDate());

I try convert String to java.time.localDate but for this 3 ways i have error我尝试将 String 转换为 java.time.localDate 但对于这 3 种方式我有错误

java.time.format.DateTimeParseException: Text '2018-7-11' could not be parsed at index 5

Pls notice this line请注意这一行

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

Specially format yyyy-MM-dd特殊格式yyyy-MM-dd

You configure to pass a Date of Format with 4 Char of year, 2 Char of Month and 2 Char of Date .您配置为传递带有年份的 4 个字符、月份的 2 个字符和日期的 2 个字符的格式日期 But you are passing only 1 Digit in month.但是您每个月只传递 1 位数。

So what you need to pass is 2018-07-11所以你需要通过的是2018-07-11

If you cannot change reservationDTO.getReservationDate() you need to use only one M如果你不能改变reservationDTO.getReservationDate()你只需要使用一个M

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-dd");
LocalDate dateTime = LocalDate.parse(reservationDTO.getReservationDate(), 
formatter);

As you can see in the javadoc M/L matches with 7, 07, Jul, July, J正如您在javadoc M/L与 7, 07, Jul, July, J 匹配中所见

Otherwise, if you can change reservationDTO.getReservationDate() just transform 2018-7-11 into 2018-07-11否则,如果您可以更改2018-7-11 reservationDTO.getReservationDate()只需将2018-7-11转换为2018-07-11

The date string you are trying to parse is not proper.您尝试解析的日期字符串不正确。 Date should be 'yyyy-MM-dd' eg '2018-07-24'.日期应为“yyyy-MM-dd”,例如“2018-07-24”。 You are trying to parse the string 2018-7-24您正在尝试解析字符串 2018-7-24

i think this example will resolve your problem.我认为这个例子会解决你的问题。

 String startDateString1 = "06/27/2007";
    String startDateString2 = "2007-06-27";
    DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy"); 
    DateFormat df2 = new SimpleDateFormat("yyyy-mm-dd"); 
    Date startDate1;
    Date startDate2;
    try {
        startDate1 = df1.parse(startDateString1);
        startDate2 = df2.parse(startDateString2);
        LocalDate localDate1 = DateTimeUtils.toLocalDate(startDate1);
        LocalDate localDate2 = DateTimeUtils.toLocalDate(startDate2);
        System.out.println("localDate1:" + localDate1);
        System.out.println("localDate2:" + localDate2);
        System.out.println("Result: " + localDate1.isBefore(localDate2));
    } catch (ParseException e) {
        e.printStackTrace();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将“java.lang.String”类型的值转换为所需类型“java.time.LocalDate” - Cannot convert value of type 'java.lang.String' to required type 'java.time.LocalDate' 无法将 [java.lang.String] 类型的属性值转换为所需类型 [java.time.LocalDate] - Failed to convert property value of type [java.lang.String] to required type [java.time.LocalDate] 要转换的源必须是java.lang.String的实例; 相反,它是一个java.time.LocalDate - source to convert from must be an instance of java.lang.String; instead it was a java.time.LocalDate 无法将“java.lang.String”类型的值转换为所需的“java.time.LocalDate”类型; - Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; 将java.time.LocalDate转换为java.util.Date - Convert java.time.LocalDate to java.util.Date 将 java.time.LocalDate 转换为 java.util.Date 类型 - Convert java.time.LocalDate into java.util.Date type 将 java.util.Date 转换为 java.time.LocalDate - Convert java.util.Date to java.time.LocalDate 可以从字符串创建Java.time.LocalDate对象吗? - Possible to create a Java.time.LocalDate object from a String? setCellValue(String value) throws java: cannot access java.time.LocalDate class file for java.time.LocalDate not found - setCellValue(String value) throws java: cannot access java.time.LocalDate class file for java.time.LocalDate not found Spring Boot 2.1.5 无法将 java.lang.String 类型的属性值转换为所需类型 java.time.LocalDate - Spring Boot 2.1.5 Failed to convert property value of type java.lang.String to required type java.time.LocalDate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM