- Boon 教程
- Boon - 主页
- Boon - 概览
- Boon - 环境设置
- 解析 JSON
- Boon - 转换为对象
- Boon - 转换为映射
- Boon - 源代码
- 生成 JSON
- Boon - 从对象
- Boon - 从映射
- 日期处理
- Boon - Long 转化日期
- Boon - String 转化日期
- Boon - 生成日期
- 注解
- Boon - @JsonIgnore
- Boon - @JsonInclude
- Boon - @JsonViews
- Boon - @JsonProperty
- Boon 实用资源
- Boon - 快速指南
- Boon - 实用资源
- Boon - 讨论
Boon - 生成日期
ObjectMapper 类可用于处理 JSON 中的不同日期格式。它还可用于生成日期对象。默认情况下,ObjectMapper 以长整型毫秒版本生成日期。如果使用 JsonFactory.createUseJSONDates() 方法返回的 ObjectMapper,我们可以在解析期间获取日期的字符串版本。
示例
以下示例使用 ObjectMapper 类通过解析 JSON 生成日期字符串。
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.createUseJSONDates();
String jsonString = "{\"name\":\"Mahesh\", \"age\":21, \"dateOfBirth\":\"1998-08-11T11:31:00.034Z\" }";
//mapper converts String to date automatically
Student student = mapper.readValue(jsonString, Student.class);
System.out.println(student.dateOfBirth);
//Mapper converts date to date string now
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 Aug 11 17:01:00 IST 1998
{"name":"Mahesh","age":21,"dateOfBirth":"1998-08-11T11:31:00.034Z"}
广告