import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class App {
public static void main(String[] args) throws Exception {
a();
// output:
/*
Inside Date serialize2019-11-04 11:34:05
Inside Timestamp serialize2019-11-04 11:34:05
Inside Date serialize2019-11-04 11:34:05
Inside Timestamp serialize2019-11-04 11:34:05
[
{
"id": 1,
"userId": 1,
"departmentId": 0,
"effectiveFrom": "Nov 4, 2019",
"endsOn": "2019-11-04 11:34:05",
"createdBy": 0,
"createdOn": "2019-11-04 11:34:05"
},
{
"id": 1,
"userId": 1,
"departmentId": 0,
"effectiveFrom": "Nov 4, 2019",
"endsOn": "2019-11-04 11:34:05",
"createdBy": 0,
"createdOn": "2019-11-04 11:34:05"
}
]
*/
}
private static String a() {
Gson gson;
GsonBuilder builder;
SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat dtfDate=new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String jsonAccts = null;
try{
builder = new GsonBuilder();
builder.registerTypeAdapter(Timestamp.class, new JsonSerializer<Timestamp>() {
@Override
public JsonElement serialize(Timestamp src, Type typeOfSrc, JsonSerializationContext context) {
dtf.setTimeZone(TimeZone.getTimeZone("UTC"));
String jsDate = dtf.format(src);
System.out.println("Inside Timestamp serialize" + jsDate);
return new JsonPrimitive(jsDate);
}
});
builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
dtf.setTimeZone(TimeZone.getTimeZone("UTC"));
String jsDate = dtf.format(src);
System.out.println("Inside Date serialize" + jsDate);
return new JsonPrimitive(jsDate);
}
});
gson = builder.create();
List<ClassTeacher> allActPgmMap = new ArrayList<ClassTeacher>();
ClassTeacher c1 = new ClassTeacher();
c1.setId(1);
c1.setUserId(1);
c1.setCreatedOn(new Timestamp(System.currentTimeMillis()));
c1.setEndsOn(new java.util.Date());
c1.setEffectiveFrom(new java.sql.Date(System.currentTimeMillis()));
ClassTeacher c2 = new ClassTeacher();
c2.setId(1);
c2.setUserId(1);
c2.setCreatedOn(new Timestamp(System.currentTimeMillis()));
c2.setEndsOn(new java.util.Date());
c2.setEffectiveFrom(new java.sql.Date(System.currentTimeMillis()));
allActPgmMap.add(c1);
allActPgmMap.add(c2);
Type listType = new TypeToken<List<ClassTeacher>>() {}.getType();
jsonAccts = gson.toJson(allActPgmMap, listType);
System.out.println(jsonAccts);
}catch(Exception e){
e.printStackTrace();
}
return jsonAccts;
}
}
Last active
November 4, 2019 11:35
-
-
Save dineshbhagat/f7620e21ce2e7a26093e4a4ef92ef391 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment