英文:
Json Date format in LocalDate
问题
I have this annotation
@Target(ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@Documented
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Europe/Brussels")
public @interface MyDateFormat {
}
and this method:
@MyDateFormat
public LocalDate getCreationDate() {
return Optional.ofNullable(task)
.map(Task::getCreateTime)
.map(date -> date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate())
.orElse(null);
}
but I got it in long format.
英文:
I have this annotation
@Target(ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@Documented
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Europe/Brussels")
public @interface MyDateFormat {
}
and this method:
@MyDateFormat
public LocalDate getCreationDate() {
return Optional.ofNullable(task)
.map(Task::getCreateTime)
.map(date -> date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate())
.orElse(null);
}
but I got it in long format
答案1
得分: 1
I have checked exactly the same code. It works as expected.
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@Documented
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Europe/Brussels")
public @interface MyDateFormat {
}
@Transient
private Date createDate = new Date();
@MyDateFormat
public LocalDate getCreationDate() {
return Optional.ofNullable(this)
.map(Customer::getCreateDate)
.map(date -> date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate())
.orElse(null);
}
"createDate": "2023-05-11T06:45:24.044+00:00",
"creationDate": "2023-05-11"
(Note: The code itself remains unchanged, and only the comments and annotations have been translated.)
英文:
I have checked exactly the same code. It works as expected.
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@Documented
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Europe/Brussels")
public @interface MyDateFormat {
}
@Transient
private Date createDate = new Date();
@MyDateFormat
public LocalDate getCreationDate() {
return Optional.ofNullable(this)
.map(Customer::getCreateDate)
.map(date -> date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate())
.orElse(null);
}
"createDate": "2023-05-11T06:45:24.044+00:00",
"creationDate": "2023-05-11"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论