-1

I am trying to create an application using springboot-java, front end as html/css/javascript. I get the below error while doing a post to create an employee record. I am passing a join date which is causing the error.

failed - {"readyState":4,
    "responseText":"{\"timestamp\":1515066928232,
        \"status\":500,
        \"error\":\"Internal Server Error\",
        \"exception\":\"java.time.format.DateTimeParseException\",
        \"message\":\"Text '01-17-2018' could not be parsed at index 0\",
        \"path\":\"/employee/\"}",
    "responseJSON":{"timestamp":1515066928232,
        "status":500,
        "error":"Internal Server Error",
        "exception":"java.time.format.DateTimeParseException",
        "message":"Text '01-17-2018' could not be parsed at index 0",
        "path":"/employee/"},
    "status":500,
    "statusText":"error"}

Below is the code I use in java:

private static final long serialVersionUID = -7314008048174880868L;
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM-dd-yyyy");

    private Integer organisation;
    private String empName;
    private String joinDate;
    private String gender;
    private String designation;
    private String email;
    public EmployeeDto(){
    }

    public EmployeeDto(Integer organisation, String empName, String joinDate, String gender,
            String designation, String email) {
        super();
        this.organisation = organisation;
        this.empName = empName;
        this.joinDate = joinDate;
        this.gender = gender;
        this.designation = designation;
        this.email = email;
    }

    public Employee buildEmployee(){
        return new Employee(this.empName,LocalDate.parse(this.joinDate),this.gender,this.designation,this.email);
    }

I use the below date converter:

@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDate locDate) {
        return (locDate == null ? null : Date.valueOf(locDate));
    }

    @Override
    public LocalDate convertToEntityAttribute(Date sqlDate) {
        return (sqlDate == null ? null : sqlDate.toLocalDate());
    }
}

I am unable to find the root cause....

1
  • Search Stack Overflow before posting. You would have found many examples from which you could have copied working code. Commented Jan 4, 2018 at 18:55

1 Answer 1

3

In buildEmployee() I think you want LocalDate.parse(this.joinDate, DATE_FORMAT) instead of LocalDate.parse(this.joinDate).

One often posted link here on Stack Overflow is How to debug small programs. In that blog post the first tip is to turn on compiler warnings and read them carefully. Putting your code into my Eclipse produces the following warning:

The value of the field EmployeeDto.DATE_FORMAT is not used

This may cause you to wonder why DATE_FORMAT is not used and why you wanted to have it there in the first place. Which in turn could inspire you to use it as intended, which fixes your error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.