0
votes

I would like to know how to format the date time correctly? The result is Localdatetime yyyy-MM-ddTHH:mm.

Could you advise how to solve?

I'm using Java 11, and does it because @JsonFormat not support @RequestParam?

Controller:

@PostMapping("/checkFollowupDate")
public LocalDateTime updateCaseFollowup(@RequestParam("followupDate") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") LocalDateTime followupDate) {
 return followupDate;
}

Entity:

@Entity
@Table(name = "caseFollowup")
public class CaseFollowup {
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private LocalDateTime followupDate;
2
However its still showing date format like "Thu Aug 06 09:01:00 CST 2020". Where?Alan Hay
@AlanHay When I check the date format in debugging: public CaseFollowup(Date followupDate) { this.followupDate = followupDate; The date show in string.Stephen Chu
Stop using java.util.Date and switch to java.time...deHaar
That is the internal representation of the Date via its toString() method. You can format it however you want for display using docs.oracle.com/javase/7/docs/api/java/text/… or whatever. Additionally @JsonFormat is not going to have any effect on a @RequestParam.Alan Hay
@AlanHay Thanks, how can I parse the LocalDateTime object from RequestParam to the format of "yyyy-MM-dd HH:mm:ss.SSS" to repository?Stephen Chu

2 Answers

2
votes

Since you are using Spring-boot , I'm also assuming you are using java8 . In any case try using java8 time api for date like :

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime followupDate;

and if you are on JPA 2.1 which was released before java8 then in your entity class you could have a converter to convert it for sql timestamp like :

@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
     
    @Override
    public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
        return locDateTime == null ? null : Timestamp.valueOf(locDateTime);
    }
 
    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
        return sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime();
    }
}

Remember that in newer version of Hibernate(Hibernate 5) and JPA the above conversion will be performed automatically and doesn't require you to provide the above method.

If your requirement is just to persist the Date read from the @RequestParam through the entity class in a particular format, you could always convert it manually into any format that you may choose before setting the value into your entity class like :

@PostMapping("/caseFollowup")
    public Integer updateCaseFollowup(@RequestParam("followupDate")
                                       LocalDateTime followupDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDateTime = followupDate.format(formatter);

}
0
votes

use this code in you model class :

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", shape = JsonFormat.Shape.STRING)
        private OffsetDateTime lastModifiedDate;



import java.sql.Timestamp;
        import java.time.OffsetDateTime;
        import java.time.ZoneOffset;
        @Component
        public class DateMapper {
        
            public OffsetDateTime asOffsetDateTime(Timestamp ts){
                if (ts != null){
                    return OffsetDateTime.of(ts.toLocalDateTime().getYear(), ts.toLocalDateTime().getMonthValue(),
                            ts.toLocalDateTime().getDayOfMonth(), ts.toLocalDateTime().getHour(), ts.toLocalDateTime().getMinute(),
                            ts.toLocalDateTime().getSecond(), ts.toLocalDateTime().getNano(), ZoneOffset.UTC);
                } else {
                    return null;
                }
            }
        
            public Timestamp asTimestamp(OffsetDateTime offsetDateTime){
                if(offsetDateTime != null) {
                    return Timestamp.valueOf(offsetDateTime.atZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
                } else {
                    return null;
                }
            }
        }