springboot---->springboot中的格式化(一)
这里面我们简单的学习一下springboot中关于数据格式化的使用。我以为你不是个好人,没想到你连个坏人都不是。
springboot中的格式化
我们的测试环境是springboot,一个将字符串格式化成时间的例子。代码结构如下:

一、定义我们的字符串格式化器
package com.linux.huhx.learn.format; import org.springframework.format.Formatter;
import org.springframework.stereotype.Component; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale; /**
* @Author: huhx
* @Date: 2017-12-15 下午 4:16
* @Desc: formatter的学习
*/
@Component
public class DateFormatter implements Formatter<Date> {
private SimpleDateFormat dateFormat; @Override
public Date parse(String text, Locale locale) throws ParseException {
System.out.println("parse input text: " + text);
System.out.println("parse date: " + dateFormat.parse(text));
return dateFormat.parse(text);
} public DateFormatter() {
dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
dateFormat.setLenient(false);
} @Override
public String print(Date object, Locale locale) {
System.out.println("print method." + object);
return dateFormat.format(object);
}
}
二、我们的测试控制器类
package com.linux.huhx.learn.format; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; /**
* @Author: huhx
* @Date: 2017-12-15 下午 4:24
*/ @RestController
@RequestMapping("/formatter")
public class DateFormatterAction { @PostMapping("/date")
public Date formatterStringToDate(Date date) {
System.out.println("action method: " + date.toString());
return date;
}
}
使用postman发送post请求:http://localhost:9998/formatter/date。

返回的数据:以下的时间戳是spring经过转换生成的
控制台打印数据:
parse input text:
parse date: Fri Dec :: CST
action method: Fri Dec :: CST
友情链接
springboot---->springboot中的格式化(一)的更多相关文章
- SpringBoot中时间格式化的5种方法!
在我们日常工作中,时间格式化是一件经常遇到的事儿,所以本文我们就来盘点一下 Spring Boot 中时间格式化的几种方法. 时间问题演示 为了方便演示,我写了一个简单 Spring Boot 项 ...
- springboot 项目中获取默认注入的序列化对象 ObjectMapper
在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...
- SpringBoot返回date日期格式化
SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差 问题描述 在Spring Boot项目中,使用@RestController注解,返回的java对象中 ...
- 国际化的实现i18n--错误码国际化以及在springboot项目中使用
国际化 ,英文叫 internationalization 单词太长 ,又被简称为 i18n(取头取尾中间有18个字母); 主要涉及3个类: Locale用来设置定制的语言和国家代码 Resource ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- 在SpringBoot项目中添加logback的MDC
在SpringBoot项目中添加logback的MDC 先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...
- 带着新人学springboot的应用06(springboot+RabbitMQ 中)
上一节说了这么多废话,看也看烦了,现在我们就来用鼠标点点点,来简单玩一下这个RabbitMQ. 注意:这一节还是不用敲什么代码,因为上一节我们设置了那个可视化工具,我们先用用可视化工具熟悉一下流程. ...
- 自身使用的springboot项目中比较全的pom.xml
在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...
- 记一次SpringBoot 开发中所遇到的坑和解决方法
记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...
- springboot配置文件中使用当前配置的变量
在开发中,有时我们的application.properties某些值需要重复使用,比如配置redis和数据库或者mongodb连接地址,日志,文件上传地址等,且这些地址如果都是相同或者父路径是相同的 ...
随机推荐
- R语言绘制花瓣图flower plot
R语言中有很多现成的R包,可以绘制venn图,但是最多支持5组,当组别数大于5时,venn图即使能够画出来,看上去也非常复杂,不够直观: 在实际的数据分析中,组别大于5的情况还是经常遇到的,这是就可以 ...
- Tomcat介绍 安装jdk 安装Tomcat
Tomcat介绍 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发而 ...
- scala 日期格式转换
scala> val format = new java.text.SimpleDateFormat("dd-MM-yyyy") 注意MM必须要大写 format: java ...
- jsTree 插件Ajax数据
完整代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
- Objective-C 语法之 Debug 表达式
main.m #import <Foundation/Foundation.h> #import "TestClass.h" int main(int argc, co ...
- maven pom文件详解
http://www.blogjava.net/hellxoul/archive/2013/05/16/399345.html http://blog.csdn.net/houpengfei111/a ...
- Android开发学习笔记-SharedPreferences的用法
SharedPreferences介绍: 做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...
- geoserver 添加图层数据
1.添加shapefile文件 首先到http://www2.census.gov/geo/tiger/TIGER2011/CONCITY/上下载名称为tl_2011_47_concity的shape ...
- 前端可视化编程:liveReload安装,sublime 3
需要插件 sublime text3:View in Browser:LiveReload chrome:liveReload 配置方法 一:sublime text3 sublime 3下载地址: ...
- npm 安装碰到SSL问题
经过仔细排查和google,原来是ssl 的问题: 解决办法: npm config set strict-ssl false