[英]How to parse JSON date time in Java/Android
我到處都看過,但找不到任何有關在android中解析日期時間json對象的信息。
我想這個JSON轉換2016-08-12T20:07:59.518451
只得到這樣的時間20:07
,並在正確的時區UTC / GMT +1小時格式它。
我可以用javascript完成,但無法在Java / Android中正確使用。
有沒有適合我的方法,還是需要使用Regex來獲取正確的時間?
編輯:這是代碼。 ExpectedArrival是帶有json日期/時間的,我只想獲取UTC / GMT +1小時時區的時間。
public class JSONTaskArrivals extends AsyncTask<String, String,List<ArrivalItem>> {
@Override
protected List<ArrivalItem> doInBackground(String... params) {
//json
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null){
buffer.append(line);
}
String finalJSON = buffer.toString();
JSONArray parentArray = new JSONArray(finalJSON);
List<ArrivalItem> arrivalItems = new ArrayList<>();
int time = 0;
for (int i = 0; i < parentArray.length(); i++){
JSONObject finalObject = parentArray.getJSONObject(i);
ArrivalItem item = new ArrivalItem();
item.setDestination(finalObject.getString("destinationName"));
item.setEstimated(finalObject.getString("expectedArrival"));
time = finalObject.getInt("timeToStation")/60;
if(time < 1){
item.setLive("Due");
}else{
item.setLive(Integer.toString(time)+" mins");
}
arrivalItems.add(item);
}
return arrivalItems;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null){
connection.disconnect();
}
try {
if (reader != null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<ArrivalItem> result) {
super.onPostExecute(result);
ArrivalsAdapter adapter = new ArrivalsAdapter(ArrivalsActivity.this, R.layout.arrivals_row, result);
lv = (ListView) findViewById(R.id.arrivals_listView);
lv.setAdapter(adapter);
}
}
沒有“ JSON日期時間”之類的東西。 JSON定義的數據類型很少 。
如果您的輸入字符串2016-08-12T20:07:59.518451
用UTC表示一個時刻,請附加一個Z
(Zulu的縮寫,表示UTC)。
String input = "2016-08-12T20:07:59.518451" + "Z" ;
然后解析為Instant
。 Instant
類表示UTC時間線上的時刻,分辨率為納秒 。 因此,您的示例輸入(以微秒為單位)非常合適。
Instant instant = Instant.parse( input );
調整為所需的UTC偏移量。
ZoneOffset offset = ZoneOffset.ofHours( 1 ); // One hour ahead UTC.
申請以獲取OffsetDateTime
。
OffsetDateTime odt = instant.atOffset( offset );
如果你想只從時間的日OffsetDateTime
提取LocalTime
。
LocalTime lt = odt.toLocalTime(); // Example: 20:39
如果要使用toString
方法使用的ISO 8601格式以外的格式,請使用DateTimeFormatter
對象。 如果在Stack Overflow上進行搜索,則會發現許多示例。
如果知道,最好使用時區,而不是僅使用時區。 產生一個ZonedDateTime
對象。
ZoneId zoneId = ZoneId.of( "Europe/Paris" );
ZonedDateTime zdt = instant.atZone( zoneId );
LocalTime lt = zdt.toLocalTime();
java.time框架內置於Java 8及更高版本中。 這些類取代了麻煩的舊日期時間類,例如java.util.Date
, .Calendar
和java.text.SimpleDateFormat
。
現在處於維護模式的Joda-Time項目建議遷移到java.time。
要了解更多信息,請參見Oracle教程 。 並在Stack Overflow中搜索許多示例和說明。
多的java.time功能后移植到Java 6和7在ThreeTen-反向移植並且還適於使用Android在ThreeTenABP 。
ThreeTen-Extra項目使用其他類擴展了java.time。 該項目為將來可能在java.time中添加內容提供了一個試驗場。
你應該做這個:
您可以使用split方法拆分日期和時間,並將其存儲在另外兩個變量中。.現在,您有了日期和時間的單獨值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.