如何设置 GsonBuilder 以保持来自 sql.timestamp 的纳秒

how set GsonBuilder to hold the nanoseconds from sql.timestamp

我知道 Gson 将纳秒舍入为毫秒,但在序列化为反序列化时我确实需要获取纳秒。

主要:

package pck;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Arrays;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {
   public static void main(String[] args) {
      Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").create();
      Employee employee = new Employee();
      employee.setId(1);
      employee.setFirstName("Lokesh");
      employee.setLastName("Gupta");
      employee.setRoles(Arrays.asList("ADMIN", "MANAGER"));

      employee.setBirthDate(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date())));

      System.out.println("employee.getBirthDate = " +  employee.getBirthDate());
      System.out.println("employee = " +  gson.toJson(employee));
      System.out.println(
                 gson.fromJson("{'id':1,'firstName':'Lokesh','lastName':'Gupta','roles':['ADMIN','MANAGER'],'BirthDate':'2015-01-07 11:37:52.390452'}",
                 Employee.class));
   }
}

波乔:

package pck;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

public class Employee
{
   private Integer id;
   private String firstName;
   private String lastName;
   private List<String> roles;
   private Timestamp BirthDate;   

   public Date getBirthDate() {
          return BirthDate;
   }

   public void setBirthDate(Timestamp birthDate) {
          BirthDate = birthDate;
   }

   public Employee(){     
   }

   @Override
   public String toString()
   {
      return "Employee [id=" + id + ", firstName=" + firstName + ", " +
                   "lastName=" + lastName + ", roles=" + roles + ", BirthDate = " + BirthDate + "]";
   }
}

控制台:

employee.getBirthDate = 2015-01-07 14:22:17.000005
employee = {"id":1,"firstName":"Lokesh","lastName":"Gupta","roles":["ADMIN","MANAGER"],"BirthDate":"2015-01-07 14:22:17.000000"}
Employee [id=1, firstName=Lokesh, lastName=Gupta, roles=[ADMIN, MANAGER], BirthDate = 2015-01-07 11:44:22.452]

原始教程,但使用 util.Date 而不是 Sql.Timestamp

http://howtodoinjava.com/2014/06/17/google-gson-tutorial-convert-java-object-to-from-json/

您需要将日期格式从 "yyyy-MM-dd HH:mm:ss.SSSSSS" 更改为 "yyyy-MM-dd HH:mm:ss.SSS",一切正常:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss.SSS").setPrettyPrinting().create();
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("Lokesh");
employee.setLastName("Gupta");
employee.setRoles(Arrays.asList("ADMIN", "MANAGER"));
employee.setBirthDate(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())));

String jsonData = gson.toJson(employee);

System.out.println("employee.getBirthDate = " +  employee.getBirthDate());
System.out.println("\nEmployee JSON = " + jsonData);
System.out.println("\n" + gson.fromJson(jsonData, Employee.class));

和输出:

employee.getBirthDate = 2015-01-13 14:56:59.853

Employee JSON = {
  "id": 1,
  "firstName": "Lokesh",
  "lastName": "Gupta",
  "roles": [
    "ADMIN",
    "MANAGER"
  ],
  "BirthDate": "2015-01-13 14:56:59.853"
}

Employee [id=1, firstName=Lokesh, lastName=Gupta, roles=[ADMIN, MANAGER], BirthDate = 2015-01-13 14:56:59.853]