如何更改springboot中时间戳的格式

发布于 2025-01-10 09:22:27 字数 321 浏览 0 评论 0原文

我写了一个像这样的控制器,它只返回当前时间戳

@GetMapping(value = "/i/testTime")
Timestamp testTime(HttpServletRequest req) throws IOException {

    return new Timestamp(System.currentTimeMillis());
}

我访问网址并返回:

"2022-02-25T08:23:32.690+00:00"

有没有办法配置这种格式?

任何答案都会有帮助

I write a controller like this and it just return the current timestamp

@GetMapping(value = "/i/testTime")
Timestamp testTime(HttpServletRequest req) throws IOException {

    return new Timestamp(System.currentTimeMillis());
}

I access the url and it returns:

"2022-02-25T08:23:32.690+00:00"

Is there a way to configure this format?

Any answer will be helpful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动听の歌 2025-01-17 09:22:27

我建议使用 java.time 包的 LocalDateTime 类。

LocalDateTime now = LocalDateTime.now();
 
// LocalDateTime cvDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneId.systemDefault()).toLocalDateTime();
// LocalDateTime utcDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneId.of("UTC")).toLocalDateTime();
 
System.out.println("Before Formatting: " + now); 
 
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
String formatDateTime = now.format(format);

在你的情况下输出

Before Formatting: 2017-01-13T17:09:42.411
After Formatting: 13-01-2017 17:09:42

SO,它会是这样的:

@GetMapping(value = "/i/testTime")
String testTime(HttpServletRequest req) throws IOException {

    LocalDateTime currentDateTime = LocalDateTime.now();
    DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
    return currentDateTime.format(format);
}

I would suggest using java.time package's LocalDateTime class.

LocalDateTime now = LocalDateTime.now();
 
// LocalDateTime cvDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneId.systemDefault()).toLocalDateTime();
// LocalDateTime utcDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneId.of("UTC")).toLocalDateTime();
 
System.out.println("Before Formatting: " + now); 
 
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
String formatDateTime = now.format(format);

Output

Before Formatting: 2017-01-13T17:09:42.411
After Formatting: 13-01-2017 17:09:42

SO in your case, it would be something like this:

@GetMapping(value = "/i/testTime")
String testTime(HttpServletRequest req) throws IOException {

    LocalDateTime currentDateTime = LocalDateTime.now();
    DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
    return currentDateTime.format(format);
}
靑春怀旧 2025-01-17 09:22:27

您甚至可以使用注释来完成此操作,而无需控制器中的逻辑。

    public class DateDto { 
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
        private LocalDateTime date;

        public DateDto(LocalDateTime date){
          this.date = date;
        }
    
        public LocalDateTime getDate(){
          return this.date;
        }
    }

你的控制器喜欢:

    @GetMapping(value = "/i/testTime")
    DateDto testTime(HttpServletRequest req) throws IOException {
        return new DateDto(LocalDateTime.now());
    }

You can even do it with annotations without having logic in your controller.

    public class DateDto { 
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
        private LocalDateTime date;

        public DateDto(LocalDateTime date){
          this.date = date;
        }
    
        public LocalDateTime getDate(){
          return this.date;
        }
    }

And your controller like:

    @GetMapping(value = "/i/testTime")
    DateDto testTime(HttpServletRequest req) throws IOException {
        return new DateDto(LocalDateTime.now());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文