Boon Tutorial
Selected Reading
- Boon - Discussion
- Boon - Useful Resources
- Boon - Quick Guide
- Boon - @JsonProperty
- Boon - @JsonViews
- Boon - @JsonInclude
- Boon - @JsonIgnore
- Boon - Generating Date
- Boon - String To Date
- Boon - Long To Date
- Boon - From Map
- Boon - From Object
- Boon - Sources
- Boon - To Map
- Boon - To Object
- Boon - Environment Setup
- Boon - Overview
- Boon - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Boon - Long To Date
Boon - Long To Date
ObjectMapper class can be used to work with different date formats in JSON. It can be used to parse/generate long version of date.
Example
Following example is using ObjectMapper class to generate a Date string from a long version.
import java.util.Date; import org.boon.json.JsonFactory; import org.boon.json.ObjectMapper; pubpc class BoonTester { pubpc 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 { pubpc String name; pubpc int age; pubpc Date dateOfBirth; pubpc Student(String name, int age, Date dateOfBirth) { this.name = name; this.age = age; this.dateOfBirth = dateOfBirth; } }
Output
Given below is the output of the code −
Tue Dec 12 00:00:00 IST 2000 {"name":"Mahesh","age":21,"dateOfBirth":976559400000}Advertisements