BeanUtils包的使用
19 System.out.println(st.getBirth());
20
21 }
三、 自定义日期格式转换器
1 @Test
2
3 public void test04()throws Exception{
4
5 //创建Student对象
6
7 Student st = new Student();
8
9 //使用CovertUtils注册创建一个日期格式转换器
10
11 ConvertUtils.register(new Converter() {
12
13
14
15 @Override
16
17 public Object convert(Class type, Object value) {
18
19 //当value参数等于空时返回空
20
21 if(value==null){
22
23 return null;
24
25 }
26
27 //自定义时间的格式为yyyy-MM-dd
28
29 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
30
31 //创建日期类对象
32
33 Date dt = null;
34
35
36
37 try {
38
39 //使用自定义日期的格式转化value参数为yyyy-MM-dd格式
40
41 dt = sdf.parse((String)value);
42
43 } catch (ParseException e) {
44
45 // TODO Auto-generated catch block
46
47 e.printStackTrace();
48
49 }
50
51 //返回dt日期对象
52
53 return dt;
54
55 }
56
57 }, Date.class);
58
59 //给birth赋值60 61 BeanUtils.setProperty(st, "birth", "1991-09-25");62 63 //输出64 65 System.out.println(st.getBirth());66 67 }
2
3 public void test04()throws Exception{
4
5 //创建Student对象
6
7 Student st = new Student();
8
9 //使用CovertUtils注册创建一个日期格式转换器
10
11 ConvertUtils.register(new Converter() {
12
13
14
15 @Override
16
17 public Object convert(Class type, Object value) {
18
19 //当value参数等于空时返回空
20
21 if(value==null){
22
23 return null;
24
25 }
26
27 //自定义时间的格式为yyyy-MM-dd
28
29 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
30
31 //创建日期类对象
32
33 Date dt = null;
34
35
36
37 try {
38
39 //使用自定义日期的格式转化value参数为yyyy-MM-dd格式
40
41 dt = sdf.parse((String)value);
42
43 } catch (ParseException e) {
44
45 // TODO Auto-generated catch block
46
47 e.printStackTrace();
48
49 }
50
51 //返回dt日期对象
52
53 return dt;
54
55 }
56
57 }, Date.class);
58
59 //给birth赋值60 61 BeanUtils.setProperty(st, "birth", "1991-09-25");62 63 //输出64 65 System.out.println(st.getBirth());66 67 }
ConvertUtils.register(new Converter()中,new Converter()就相当于重写了Converter类中方法:
Public class MyConerter implements Converter{
//并且在此处创建了匿名的对象
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)