- 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}
廣告