[英]Converting JSON object to util.Date in Java
我有一個看起來像這樣的對象:
{
"date": 12,
"day": 3,
"hours": 12,
"minutes": 32,
"month": 8,
"seconds": 32,
"time": 1536755552909,
"timezoneOffset": 0,
"year": 118
}
我之前使用util.Date
生成日期,但是現在我從Node.js服務器以上面的格式獲取日期。 如何將其轉換為Date
類型?
當我嘗試去做
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(response);
Gson gson = new Gson();
Date date = gson.fromJson(jsonElement,Date.class);
lastLoadedDate = date;
elapsedTime = 0;
}
}
這只是失敗,因為它不是字符串,Node服務器發送JSON。 如果我執行new Response.Listener<Date>()
因為它是JSON,而不是日期。 將JSON轉換為類似Date這樣的Date now = (Date) myJsonResponse
會導致Android Studio錯誤提示:
不可爭議的類型:無法將JSONObject轉換為Date
編輯。 我不能使用util.Date,因為我需要時間來自服務器,這對於每個人來說都是通用的。
如果可能的話,請讓服務器以更好的格式(例如ISO-8601)返回日期,因為這樣可以避免歧義,並且可以減少JSON負載。
如果無法做到這一點,則必須決定使用JSON的哪些部分, time
或不同的值(例如hours
, minutes
,...)。通過手動選擇這些部分,可以使用一個手動構建Date
對象。的構造函數或通過java.util.Calendar
對象進行構造(因為Java 8中不推薦使用java.util.Date
中的許多構造函數)。
這就是解決的方法。 謝謝Ayush Gupta的評論。
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
timestamp = jsonObject.getLong("time");
lastLoadedDate = new Date(timestamp);
} catch (JSONException e) {
//Handle the exception
}
}
}
如果要使用Java的util.Date,則可以自己生成。
在文檔中,您可以找到適當的構造函數聲明,如Date(int year, int month, int date, int hrs, int min, int sec)
。
因此,您可以從json創建它,只需從中獲取適當的值(以指定json對象的方式->在我的示例中為Object):
Date date = new Date(jsonElement.get(year), jsonElement.get(month), jsonElement.get(date), jsonElement.get(hrs), jsonElement.get(min), jsonElement.get(sec));
首先,您創建一個像這樣的POJO類。
public class ResponseModel {
private int date;
private int day;
private int hours;
private int minutes;
private int month;
private int seconds;
private long time;
private int timezoneOffset;
private int year;
public int getDate() {
return date;
}
public ResponseModel setDate(int date) {
this.date = date;
return this;
}
public int getDay() {
return day;
}
public ResponseModel setDay(int day) {
this.day = day;
return this;
}
public int getHours() {
return hours;
}
public ResponseModel setHours(int hours) {
this.hours = hours;
return this;
}
public int getMinutes() {
return minutes;
}
public ResponseModel setMinutes(int minutes) {
this.minutes = minutes;
return this;
}
public int getMonth() {
return month;
}
public ResponseModel setMonth(int month) {
this.month = month;
return this;
}
public int getSeconds() {
return seconds;
}
public ResponseModel setSeconds(int seconds) {
this.seconds = seconds;
return this;
}
public long getTime() {
return time;
}
public ResponseModel setTime(long time) {
this.time = time;
return this;
}
public int getTimezoneOffset() {
return timezoneOffset;
}
public ResponseModel setTimezoneOffset(int timezoneOffset) {
this.timezoneOffset = timezoneOffset;
return this;
}
public int getYear() {
return year;
}
public ResponseModel setYear(int year) {
this.year = year;
return this;
}
}
然后像這樣將您的字符串響應轉換為ResponseModel.class
Gson gson = new Gson();
ResponseModel model = gson.fromJson(jsonElement,ResponseModel.class);
然后創建如下所示的方法
private Date getDateFromResponse(ResponseModel model) {
Calendar calendar = Calendar.getInstance();
//-----
calendar.set(Calendar.DAY_OF_MONTH, model.getDate());
calendar.set(Calendar.MONTH, model.getMonth());
calendar.set(Calendar.YEAR, model.getYear());
calendar.set(Calendar.HOUR, model.getHours());
calendar.set(Calendar.MINUTE, model.getMinutes());
calendar.set(Calendar.SECOND, model.getSeconds());
//OR
//You can simply add this line with this time is equal to your all fields
//And comment all above lines.
//calendar.setTimeInMillis(model.getTime());
return calendar.getTime();
}
並簡單地調用此方法。
Date date = getDateFromResponse(model);
希望這對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.