English 中文(简体)
Boon - String To Date
  • 时间:2024-11-03

Boon - String To Date


Previous Page Next Page  

ObjectMapper class can be used to work with different date formats in JSON. It can be used to parse/generate String version of date.

Example

Following example is using ObjectMapper class to generate a Date string from a String 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":"1998-08-11T11:31:00.034Z" }";
      
      // mapper converts String 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

When you execute the above code, you should see the following output −


Tue Aug 11 17:01:00 IST 1998
{"name":"Mahesh","age":21,"dateOfBirth":902835060034}
Advertisements