1. 程式人生 > >Gson解析Date,日期格式

Gson解析Date,日期格式

//		Weather weather = new Weather();
//		weather.setId(123);
//		weather.setUpdatedTime(new Date());
//		System.out.println(new Gson().toJson(weather));//{"id":123,"updatedTime":"Feb 11, 2017 12:06:29 PM"}
		
		
		
//		Weather weather = new Weather();
//		weather.setId(123);
//		try {
//			weather.setUpdatedTime(sdfyyyyMMddHHmmss.parse("2017-02-08 00:16:47"));
//		} catch (ParseException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		System.out.println(new Gson().toJson(weather));//{"id":123,"updatedTime":"Feb 11, 2017 12:06:29 PM"}
		
		Weather weather = new Weather();
		weather.setId(123);
		weather.setUpdatedTime(new Date());
//		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		System.out.println(gson.toJson(weather));//{"id":123,"updatedTime":"2017-02-11 12:14:06"}