Json日期格式在LocalDate中

huangapple go评论59阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年5月11日 13:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定