0

I am having trouble parsing String to LocalDateTime(Java) which is giving me really unexpected parsing exception. Here's my payload :

{"tokenTime":"2022-05-31 10:49:00","txt_cc_contactnumber":"9770697705","txt_cc_firstname":"ketan","txt_cc_lastname":"KHEMANI","txt_cc_emailid":"KHEANI@GMAIL.COM","txt_cc_disposition":1,... so on}

I am fetching first attribute tokenTime from this JSON and than getting its value which is in this case is 2022-05-31 10:49:00. Please follow my code below :

 String valueTimeObject = null;
 String valueTime = null;
 ServletRequestWrapper wrapperRequest = new ServletRequestWrapper(httpRequest);

 String body = wrapperRequest.getBody(); // complete payload
 valueTimeObject = Arrays.asList(body.split(","))
       .stream()
       .filter(predicate -> predicate.contains("tokenTime"))
       .findFirst().orElse(null); // "tokenTime":"2022-05-31 10:49:00"

 boolean check = false;
 if(valueTimeObject != null) {
     valueTime = valueTimeObject
             .contains("null") ? null : valueTimeObject.split(":", 2)[1].trim()
             .replaceAll("\"", "");
     System.out.println(valueTime); // 2022-05-31 10:49:00

     
 }
 if(valueTime == null) {
    String ipAddress = ipAddressFetchService.getClientIp((HttpServletRequest) request);
    System.out.println(ipAddress);
    if(InetAddresses.isInetAddress(ipAddress)) {
       check = ipAuthenticationService.checkLoginSessionUsingIpAddress(ipAddress);
    }
 } else {
    LocalDateTime tokenTime = LocalDateTime.parse(valueTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    check = LocalDateTime.now().isBefore(tokenTime);
 }

Now the unexpected error which I am getting is when i give "tokenTime" value in payload as future time value from now it gives me error as follows :

"JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String \"2022-05-31 10:49:00\": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-05-31 10:49:00' could not be parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String \"2022-05-31 10:49:00\": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-05-31 10:49:00' could not be parsed at index 10\n at [Source: (PushbackInputStream); line: 1, column: 15] (through reference chain: com.magicbrick.engine.payload.RegisterCallCenterDataRequest[\"tokenTime\"])"

However, If i gave "tokenTime" value as past time value it is working fine as expected.

I am getting error when Spring tries to parse "tokenTime" String value to LocalDateTime in my Payload Class. Please follow model class below :

@Data
@Builder
public class RegisterCallCenterDataRequest {
    @JsonProperty("txt_cc_mortgagedpropertysamecity")
    private Integer mortgagedPropertyInSameCity;
    @JsonProperty("txt_cc_mortgagedpropertyislocated")
    private String mortgagedPropertyLocatedIn;
    @JsonProperty("txt_cc_againstwhichpropertytobal_transfer")
    private Integer balTransferToWhichProperty;
    @JsonProperty("txt_cc_whoisexisting_loanpartner")
    private Integer existingLoanPartner;
    @JsonProperty("txt_cc_outstandingofcurrentloan")
    private Long outstandingOfCurrentLoan;
    @JsonProperty("txt_cc_currentHLemi")
    private Integer currentHLEmi;
    @JsonProperty("txt_cc_remainingtenureofloanmonth")
    private Long remainingTenure;
    @JsonProperty("txt_cc_currentval_ofproperty")
    private Long currentValOfProperty;
    @JsonProperty("txt_cc_primaryreason_trans_loan")
    private Integer primaryReasonForLoanTransfer;
    @JsonProperty("txt_cc_primaryreason_trans_loan_other")
    private String primaryReasonForLoanTransferOther;
    
    @JsonProperty("txt_cc_closurelead_status")
    private Integer leadStatus;
    @JsonProperty("txt_cc_closurelead_status_date")
    private LocalDate leadStatusDate;
    
    @JsonProperty("txt_cc_rulesDetail")
    private List<RecommendationResponse> recommendationResponse;


    private LocalDateTime tokenTime;
}

I already tried @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss) annotation above tokenTime attribute but its not working.

PseudoDev
  • 11
  • 5
  • 1
    what line doesn raise the error ? – azro May 30 '22 at 05:46
  • 1
    Can you please try to create a [mre] instead, please? I’m confident that you can show us your problem in approximately three lines of code. – Ole V.V. May 30 '22 at 06:42
  • 1
    @Ole V.V. --> This is right. Even I get the tokenTime same as you do(I checked by logging) and also in same format as you get i.e. `2022-05-31T10:49`. However, the error I am getting while Spring try to parse "tokenTime" String value to LocalDateTime. I am updating my question. Please follow at the end. – PseudoDev May 30 '22 at 06:55
  • Probably related: [LocalDateTime parsing with jackson](https://stackoverflow.com/questions/62576883/localdatetime-parsing-with-jackson). Search for more. – Ole V.V. May 30 '22 at 07:21
  • Thanks man ! Yeah it works after placing , @JsonDeserialize(using = LocalDateTimeDeserializer.class) over model class. – PseudoDev May 31 '22 at 12:00

0 Answers0