- Boon 教程
- Boon - 主页
- Boon - 概述
- Boon - 环境设置
- 解析 JSON
- Boon - 转换为对象
- Boon - 转换为 Map
- Boon - 来源
- 生成 JSON
- Boon - 从对象生成
- Boon - 从 Map 生成
- 日期处理
- Boon - 将 Long 转换为 Date
- Boon - 字符串转换为日期
- Boon - 生成日期
- 注解
- Boon - @JsonIgnore
- Boon - @JsonInclude
- Boon - @JsonViews
- Boon - @JsonProperty
- Boon 有用资源
- Boon - 快速指南
- Boon - 有用资源
- Boon - 讨论
Boon - 将 Long 转换为 Date
ObjectMapper 类可用于处理 JSON 中的不同日期格式。它可用于解析/生成长版本的日期。
示例
以下示例使用 ObjectMapper 类从 long 版本生成一个 Date 字符串。
import java.util.Date;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;
public class BoonTester {
public static void main(String args[]) {
ObjectMapper mapper = JsonFactory.create();
String jsonString = "{\"name\":\"Mahesh\", \"age\":21, \"dateOfBirth\":976559400000}";
//mapper converts long to date automatically
Student student = mapper.readValue(jsonString, Student.class);
System.out.println(student.dateOfBirth);
//by default mapper converts date to long
jsonString = mapper.writeValueAsString(student);
System.out.println(jsonString);
}
}
class Student {
public String name;
public int age;
public Date dateOfBirth;
public Student(String name, int age, Date dateOfBirth) {
this.name = name;
this.age = age;
this.dateOfBirth = dateOfBirth;
}
}
输出
以下是代码的输出 -
Tue Dec 12 00:00:00 IST 2000
{"name":"Mahesh","age":21,"dateOfBirth":976559400000}
广告