1. <i id='IEhDJ'><tr id='IEhDJ'><dt id='IEhDJ'><q id='IEhDJ'><span id='IEhDJ'><b id='IEhDJ'><form id='IEhDJ'><ins id='IEhDJ'></ins><ul id='IEhDJ'></ul><sub id='IEhDJ'></sub></form><legend id='IEhDJ'></legend><bdo id='IEhDJ'><pre id='IEhDJ'><center id='IEhDJ'></center></pre></bdo></b><th id='IEhDJ'></th></span></q></dt></tr></i><div id='IEhDJ'><tfoot id='IEhDJ'></tfoot><dl id='IEhDJ'><fieldset id='IEhDJ'></fieldset></dl></div>

  2. <small id='IEhDJ'></small><noframes id='IEhDJ'>

    <tfoot id='IEhDJ'></tfoot>

    1. <legend id='IEhDJ'><style id='IEhDJ'><dir id='IEhDJ'><q id='IEhDJ'></q></dir></style></legend>
        <bdo id='IEhDJ'></bdo><ul id='IEhDJ'></ul>

      使用@JsonFormat和@DateTimeFormat对Date格式化操作

      时间:2023-12-10
        • <bdo id='uFaNJ'></bdo><ul id='uFaNJ'></ul>
            <legend id='uFaNJ'><style id='uFaNJ'><dir id='uFaNJ'><q id='uFaNJ'></q></dir></style></legend>

              <small id='uFaNJ'></small><noframes id='uFaNJ'>

              <i id='uFaNJ'><tr id='uFaNJ'><dt id='uFaNJ'><q id='uFaNJ'><span id='uFaNJ'><b id='uFaNJ'><form id='uFaNJ'><ins id='uFaNJ'></ins><ul id='uFaNJ'></ul><sub id='uFaNJ'></sub></form><legend id='uFaNJ'></legend><bdo id='uFaNJ'><pre id='uFaNJ'><center id='uFaNJ'></center></pre></bdo></b><th id='uFaNJ'></th></span></q></dt></tr></i><div id='uFaNJ'><tfoot id='uFaNJ'></tfoot><dl id='uFaNJ'><fieldset id='uFaNJ'></fieldset></dl></div>

              <tfoot id='uFaNJ'></tfoot>

                <tbody id='uFaNJ'></tbody>

              1. 使用@JsonFormat和@DateTimeFormat对Date格式化操作的完整攻略如下:

                1. @JsonFormat注解用于序列化Java对象,将日期格式化为指定的格式,例如将日期格式化为yyyy-MM-dd HH:mm:ss,其基本使用方式如下:
                @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
                private Date createTime;
                

                其中pattern属性指定日期格式化的格式,timezone属性指定时区。

                1. @DateTimeFormat注解用于将前端传来的字符串类型的时间格式转换成Date类型,例如将“2022-01-01 00:00:00”转换成Date类型,其基本使用方式如下:
                @RequestMapping("/addUser")
                @ResponseBody
                public String addUser(User user, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date createTime) {
                    user.setCreateTime(createTime);
                    userService.addUser(user);
                    return "success";
                }
                

                在上面的代码中,我们使用了@DateTimeFormat注解,将createTime参数转换成了Date类型。

                示例1:使用@JsonFormat注解格式化日期

                import com.fasterxml.jackson.annotation.JsonFormat;
                import lombok.AllArgsConstructor;
                import lombok.Data;
                import lombok.NoArgsConstructor;
                import java.util.Date;
                
                @Data
                @NoArgsConstructor
                @AllArgsConstructor
                public class User {
                    private Integer id;
                    private String name;
                    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
                    private Date createTime;
                }
                
                // 在控制器中序列化对象
                @RestController
                public class UserController {
                    @RequestMapping("/user")
                    public User user() {
                        return new User(1, "Tom", new Date());
                    }
                }
                

                在浏览器中访问http://localhost:8080/user,返回的JSON格式如下:

                {
                    "id": 1,
                    "name": "Tom",
                    "createTime": "2022-04-16 13:50:10"
                }
                

                我们可以看到createTime被格式化为了指定的格式。

                示例2:使用@DateTimeFormat注解将字符串类型的时间格式转换成Date类型

                import lombok.Getter;
                import lombok.Setter;
                import org.springframework.format.annotation.DateTimeFormat;
                import java.util.Date;
                
                @Getter
                @Setter
                public class User {
                    private Integer id;
                    private String name;
                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                    private Date createTime;
                }
                
                @Controller
                public class UserController {
                    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
                    public String addUser(User user) {
                        userService.addUser(user);
                        return "redirect:/userList";
                    }
                }
                
                // 在模板中使用表单提交时间
                <form role="form" action="${pageContext.request.contextPath}/addUser" method="post">
                    <div class="form-group">
                        <label>用户名</label>
                        <input type="text" name="name" class="form-control" placeholder="请输入用户名">
                    </div>
                    <div class="form-group">
                        <label>创建时间</label>
                        <input type="text" name="createTime" class="form-control" placeholder="请输入创建时间,格式为yyyy-MM-dd HH:mm:ss">
                    </div>
                    <button type="submit" class="btn btn-primary">提交</button>
                </form>
                

                在添加用户时,我们将页面传递来的createTime参数使用@DateTimeFormat注解,转换成了Date类型。

                上一篇:Erlang初学:Erlang的一些特点和个人理解总结 下一篇:JAVA中JSONObject对象和Map对象之间的相互转换

                相关文章

              2. <i id='uM7Ty'><tr id='uM7Ty'><dt id='uM7Ty'><q id='uM7Ty'><span id='uM7Ty'><b id='uM7Ty'><form id='uM7Ty'><ins id='uM7Ty'></ins><ul id='uM7Ty'></ul><sub id='uM7Ty'></sub></form><legend id='uM7Ty'></legend><bdo id='uM7Ty'><pre id='uM7Ty'><center id='uM7Ty'></center></pre></bdo></b><th id='uM7Ty'></th></span></q></dt></tr></i><div id='uM7Ty'><tfoot id='uM7Ty'></tfoot><dl id='uM7Ty'><fieldset id='uM7Ty'></fieldset></dl></div>

                <small id='uM7Ty'></small><noframes id='uM7Ty'>

              3. <legend id='uM7Ty'><style id='uM7Ty'><dir id='uM7Ty'><q id='uM7Ty'></q></dir></style></legend>

                    <bdo id='uM7Ty'></bdo><ul id='uM7Ty'></ul>

                    <tfoot id='uM7Ty'></tfoot>