苏州怎么政府网站建设/营销模式有哪些 新型
Java的@DateTimeFormat注解与@JsonFormat注解的使用对比
在Java开发中,处理日期和时间格式时,我们经常会使用到@DateTimeFormat
和@JsonFormat
注解。这两个注解主要用于格式化日期和时间,但在使用场景和功能上有所不同。本文将详细介绍这两个注解的使用方法,并对比它们的异同点。
引入我的其他博客:
1、Java 前后端时间格式转换注解
https://blog.csdn.net/qq_29689343/article/details/125749447
文章目录
- Java的@DateTimeFormat注解与@JsonFormat注解的使用对比
- 一、简介
- 二、使用场景
- 1. @DateTimeFormat注解
- 2. @JsonFormat注解
- 三、基本使用
- 1. @DateTimeFormat的基本使用
- 2. @JsonFormat的基本使用
- 四、功能详解
- 1. @DateTimeFormat注解的功能
- 2. @JsonFormat注解的功能
- 五、最佳实践及案例
- 1. 在Spring Boot项目中使用@DateTimeFormat和@JsonFormat
- 2. 处理不同格式的日期
- 六、总结
一、简介
在Spring和Jackson框架中,日期和时间格式化是一个常见需求。@DateTimeFormat
注解主要用于Spring的表单绑定,而@JsonFormat
注解则用于Jackson的JSON序列化和反序列化。了解这两个注解的使用场景和方法,可以帮助开发者更高效地处理日期和时间。
二、使用场景
1. @DateTimeFormat注解
@DateTimeFormat
注解通常用于Spring MVC中,主要用于将字符串日期转换为Java的日期对象,或者将Java的日期对象转换为特定格式的字符串。
2. @JsonFormat注解
@JsonFormat
注解主要用于Jackson库,通常在序列化和反序列化JSON数据时使用,用于指定日期和时间的格式。
三、基本使用
1. @DateTimeFormat的基本使用
在Spring MVC中,@DateTimeFormat
注解可以用于控制器方法的参数:
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDate;@RestController
public class DateController {@GetMapping("/date")public String getDate(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {return "Parsed date is: " + date.toString();}
}
2. @JsonFormat的基本使用
在使用Jackson进行JSON序列化和反序列化时,可以使用@JsonFormat
注解来指定日期格式:
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;import java.time.LocalDate;public class User {@JsonFormat(pattern = "yyyy-MM-dd")private LocalDate birthDate;// getters and setterspublic static void main(String[] args) throws Exception {User user = new User();user.setBirthDate(LocalDate.of(1990, 1, 1));ObjectMapper mapper = new ObjectMapper();String json = mapper.writeValueAsString(user);System.out.println(json); // {"birthDate":"1990-01-01"}User deserializedUser = mapper.readValue(json, User.class);System.out.println(deserializedUser.getBirthDate()); // 1990-01-01}
}
四、功能详解
1. @DateTimeFormat注解的功能
- 作用范围:主要用于Spring MVC的请求参数绑定和表单数据绑定。
- 支持的类型:支持
java.util.Date
、java.time.LocalDate
、java.time.LocalDateTime
等。 - 常用属性:
pattern
:指定日期格式模式,例如"yyyy-MM-dd"
。iso
:使用ISO标准格式,例如DateTimeFormat.ISO.DATE
。
2. @JsonFormat注解的功能
- 作用范围:主要用于Jackson的JSON序列化和反序列化。
- 支持的类型:支持
java.util.Date
、java.time.LocalDate
、java.time.LocalDateTime
等。 - 常用属性:
pattern
:指定日期格式模式,例如"yyyy-MM-dd"
。shape
:指定数据的形状,例如JsonFormat.Shape.STRING
。timezone
:指定时区,例如"GMT+8"
。
五、最佳实践及案例
1. 在Spring Boot项目中使用@DateTimeFormat和@JsonFormat
在Spring Boot项目中,可以同时使用@DateTimeFormat
和@JsonFormat
来处理不同场景下的日期格式化需求。
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;import java.time.LocalDate;public class Event {@DateTimeFormat(pattern = "yyyy-MM-dd")@JsonFormat(pattern = "yyyy-MM-dd")private LocalDate eventDate;// getters and setters
}
2. 处理不同格式的日期
在不同的场景下,可能需要处理不同格式的日期。例如,在请求参数中使用@DateTimeFormat
,在JSON序列化时使用@JsonFormat
:
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDate;@RestController
public class EventController {@GetMapping("/event")public Event getEvent(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {Event event = new Event();event.setEventDate(date);return event;}
}class Event {@JsonFormat(pattern = "MM/dd/yyyy")private LocalDate eventDate;// getters and setters
}
在这个例子中,请求参数使用yyyy-MM-dd
格式,而返回的JSON数据使用MM/dd/yyyy
格式。
六、总结
@DateTimeFormat
和@JsonFormat
是处理日期和时间格式化的两个重要注解。@DateTimeFormat
主要用于Spring MVC的请求参数绑定,而@JsonFormat
主要用于Jackson的JSON序列化和反序列化。了解它们的使用场景和功能,可以帮助开发者更高效地处理日期和时间格式化需求。
通过本文的介绍,希望读者能够更清晰地理解@DateTimeFormat
和@JsonFormat
的使用方法,并在实际项目中灵活应用。
相关文章:

Java的@DateTimeFormat注解与@JsonFormat注解的使用对比
Java的DateTimeFormat注解与JsonFormat注解的使用对比 在Java开发中,处理日期和时间格式时,我们经常会使用到DateTimeFormat和JsonFormat注解。这两个注解主要用于格式化日期和时间,但在使用场景和功能上有所不同。本文将详细介绍这两个注解…...

德国云手机:企业移动办公解决方案
在现代商业环境中,移动办公已经成为一种趋势。德国云手机作为一种高效的解决方案,为企业提供了强大的支持。本文将探讨德国云手机如何优化企业的移动办公环境。 一、德国云手机的主要优势 高灵活性 德国云手机具有高度的灵活性,能够根据用户需…...

【React】useState:状态管理的基石
文章目录 一、什么是 useState?二、useState 的基本用法三、useState 的工作原理四、高级用法五、最佳实践 在现代前端开发中,React 是一个非常流行的库,而 useState 是 React 中最重要的 Hook 之一。useState 使得函数组件能够拥有自己的状态…...

商品中心关于缓存热key的解决方案
缓存热key一旦被击穿,流量势必会打到数据库,如果数据库崩了,游戏直接结束。 从两点来讨论:如何监控、如何解决。 如何监控 通过业务评估:比如营销活动推出的商品或者热卖的商品。基于LRU的命令,redis-cl…...

【Python系列】Parquet 数据处理与合并:高效数据操作实践
💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…...

大脑自组织神经网络通俗讲解
大脑自组织神经网络的核心概念 大脑自组织神经网络,是指大脑中的神经元通过自组织的方式形成复杂的网络结构,从而实现信息的处理和存储。这一过程涉及到神经元的生长、连接和重塑,是大脑学习和记忆的基础。其核心公式涉及神经网络的权重更新…...

org.springframework.context.annotation.DeferredImportSelector如何使用?
DeferredImportSelector 是 Spring 框架中一个比较高级的功能,主要用于在 Spring 应用上下文的配置阶段延迟导入某些组件或配置。这个功能特别有用,比如在处理依赖于其他自动配置的场景,或者当你想基于某些条件来决定是否导入特定的配置类时。…...

缓慢变化维
缓慢变化维 缓慢变化维(Slowly Changing Dimensions,简称SCD)是数据仓库中的一个重要概念,用于处理维度表中数据随时间发生的变化。以下是一个具体的例子来描述缓慢变化维: 假设我们有一个销售数据仓库,其…...

Vue常用的指令都有哪些?都有什么作用?什么是自定义指令?
常用指令: 1、v-model 多用于表单元素实现双向数据绑定 (同angular中的ng-model) 2、v-for格式: v-for"字段名in(of)数组json"循环数组或json(同angular中的ng repeat),需要注意从vue2开始取消了$index 3、v-show 4、v-hide 隐藏内容 (同a…...

kettle从入门到精通 第八十一课 ETL之kettle kettle中的json对象字段写入postgresql中的json字段正确姿势
1、上一节可讲解了如何将json数据写入pg数据库表中的json字段,虽然实现了效果,但若客户继续使用表输出步骤则仍然无法解决问题。 正确的的解决方式是设置数据库连接参数stringtypeunspecified 2、stringtypeunspecified 参数的作用: 当设置…...

计算机网络实验-RIP配置与分析
前言:本博客仅作记录学习使用,部分图片出自网络,如有侵犯您的权益,请联系删除 一、相关知识 路由信息协议(Routing Information Protocol,RIP)是一种基于距离向量(Distance-Vector&…...

33.【C语言】实践扫雷游戏
预备知识: 第13篇 一维数组 第13.5篇 二维数组 第28篇 库函数 第29篇 自定义函数 第30篇 函数补充 0x1游戏的运行: 1.随机布置雷 2.排雷 基本规则: 点开一个格子后,显示1,对于9*9,代表以1为中心的去…...

git学习笔记(总结了常见命令与学习中遇到的问题和解决方法)
前言 最近学习完git,学习过程中也遇到了很多问题,这里给大家写一篇总结性的博客,主要大概讲述git命令和部分难点问题(简单的知识点这里就不再重复讲解了) 一.git概述 1.1什么是git Git是一个分布式的版本控制软件。…...

【计算机网络】TCP协议详解
欢迎来到 破晓的历程的 博客 ⛺️不负时光,不负己✈️ 文章目录 1、引言2、udp和tcp协议的异同3、tcp服务器3.1、接口认识3.2、服务器设计 4、tcp客户端4.1、客户端设计4.2、说明 5、再研Tcp服务端5.1、多进程版5.2、多线程版 5、守护进程化5.1、什么是守护进程5.2…...

2.3 大模型硬件基础:AI芯片(上篇) —— 《带你自学大语言模型》系列
本系列目录 《带你自学大语言模型》系列部分目录及计划,完整版目录见:带你自学大语言模型系列 —— 前言 第一部分 走进大语言模型(科普向) 第一章 走进大语言模型 1.1 从图灵机到GPT,人工智能经历了什么࿱…...

Java | Leetcode Java题解之第279题完全平方数
题目: 题解: class Solution {public int numSquares(int n) {if (isPerfectSquare(n)) {return 1;}if (checkAnswer4(n)) {return 4;}for (int i 1; i * i < n; i) {int j n - i * i;if (isPerfectSquare(j)) {return 2;}}return 3;}// 判断是否为…...

JS逆向高级爬虫
JS逆向高级爬虫 JS逆向的目的是通过运行本地JS的文件或者代码,以实现脱离他的网站和浏览器,并且还能拿到和浏览器加密一样的效果。 10.1、编码算法 【1】摘要算法:一切从MD5开始 MD5是一个非常常见的摘要(hash)逻辑. 其特点就是小巧. 速度快. 极难被破解. 所以,…...

基于Golang+Vue3快速搭建的博客系统
WANLI 博客系统 项目介绍 基于vue3和gin框架开发的前后端分离个人博客系统,包含md格式的文本编辑展示,点赞评论收藏,新闻热点,匿名聊天室,文章搜索等功能。 项目在线访问:http://bloggo.chat/ 访客账号…...

DVWA中命令执行漏洞细说
在攻击中,命令注入是比较常见的方式,今天我们细说在软件开发中如何避免命令执行漏洞 我们通过DVWA中不同的安全等级来细说命令执行漏洞 1、先调整DVWA的安全等级为Lower,调整等级在DVWA Security页面调整 2、在Command Injection页面输入127.0.0.1&…...

【YOLOv5/v7改进系列】引入中心化特征金字塔的EVC模块
一、导言 现有的特征金字塔方法过于关注层间特征交互而忽视了层内特征的调控。尽管有些方法尝试通过注意力机制或视觉变换器来学习紧凑的层内特征表示,但这些方法往往忽略了对密集预测任务非常重要的被忽视的角落区域。 为了解决这个问题,作者提出了CF…...

【QT】常用控件(概述、QWidget核心属性、按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
一、控件概述 Widget 是 Qt 中的核心概念,英文原义是 “小部件”,此处也把它翻译为 “控件”。控件是构成一个图形化界面的基本要素。 像上述示例中的按钮、列表视图、树形视图、单行输入框、多行输入框、滚动条、下拉框都可以称为 “控件”。 Qt 作为…...

【Python】字母 Rangoli 图案
一、题目 You are given an integer N. Your task is to print an alphabet rangoli of size N. (Rangoli is a form of Indian folk art based on creation of patterns.) Different sizes of alphabet rangoli are shown below: # size 3 ----c---- --c-b-c-- c-b-a-b-c --…...

html+css 实现水波纹按钮
前言:哈喽,大家好,今天给大家分享htmlcss 绚丽效果!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏关注哦 💕 文…...

科技与占星的融合:AI 智能占星师
本文由 ChatMoney团队出品 在科技的前沿领域,诞生了一位独特的存在——AI占星师。它并非传统意义上的占星师,而是融合了先进的人工智能技术与神秘的占星学知识。 这能够凭借其强大的数据分析能力和精准的算法,对星辰的排列和宇宙的能量进行深…...

判断字符串,数组方法
判断字符串方法 在JavaScript中,可以使用typeof操作符来判断一个变量是否为字符串。 function isString(value) {return typeof value string; } 判断数组 在JavaScript中,typeof操作符并不足以准确判断一个变量是否为数组,因为typeof会…...

SpringBoot Vue使用Jwt实现简单的权限管理
为实现Jwt简单的权限管理,我们需要用Jwt工具来生成token,也需要用Jwt来解码token,同时需要添加Jwt拦截器来决定放行还是拦截。下面来实现: 1、gradle引入Jwt、hutool插件 implementation com.auth0:java-jwt:3.10.3implementatio…...

java中的多态
多态基础了解: 面向对象的三大特征:封装,继承,多态。 有了面向对象才有继承和多态,对象代表什么,就封装对应的数据,并提供数据对应的行为,可以把零散的数据和行为进行封装成一个整…...

【数据结构】:用Java实现链表
在 ArrayList 任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为 O(n),效率比较低,因此 ArrayList 不适合做任意位置插入和删除比较多的场景。因此:java 集合中又引入了 LinkedList&…...

前端开发知识(三)-javascript
javascript是一门跨平台、面向对象的脚本语言。 一、引入方式 1.内部脚本:使用<script> ,可以放在任意位置,也可以有多个,一般是放在<body></body>的下方。 2.外部脚本:单独编写.js文件ÿ…...

Windows图形界面(GUI)-MFC-C/C++ - MFC绘图
公开视频 -> 链接点击跳转公开课程博客首页 -> 链接点击跳转博客主页 目录 MFC绘图 绘图基础 CPaintDC 实例代码 MFC绘图 绘图基础 设备上下文(Device Context, DC): 设备上下文是一个Windows GDI(图形设备接口)…...