我通过JSONObject从php-server(String zeit)获取时间戳。如何从响应的时间戳中减去当前时间,我得到的时间是"自"值?我希望我的问题可以理解。
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
tische = json.getJSONArray(TAG_TISCHE);
// looping through All Products
for (int i = 0; i < tische.length(); i++) {
JSONObject c = tische.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String wer = c.getString(TAG_KELLNER);
String zeit = c.getString(TAG_ZEIT);
String str_date="2015-09-23 17:53:00"; //string date & time of old time time stamp
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //format for string to date conversion
Date date = (Date)formatter.parse(str_date); //string to date convert
long oldtime = date.getTime(); //date to milliseconds base 1970-01-01
Calendar nowdate = Calendar.getInstance(); //pick present time
long nowtime = nowdate.getTimeInMillis(); //convert to milli seconds
long timediff = nowtime-oldtime; //find difference
String zeitdiff = Long.toString(timediff);
System.out.println("difference is " + (timediff/(1000*3600*24))+ " days");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_KELLNER, wer);
map.put(TAG_ZEIT, zeitdiff);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
答案 0 :(得分:2)
以下是查找时间差异的代码。
String str_date="2015-09-23 17:53:00"; //string date & time of old time time stamp
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //format for string to date conversion
Date date = (Date)formatter.parse(str_date); //string to date convert
long oldtime = date.getTime(); //date to milliseconds base 1970-01-01
Calendar nowdate = Calendar.getInstance(); //pick present time
long nowtime = nowdate.getTimeInMillis(); //convert to milli seconds
long timediff = nowtime-oldtime; //find difference
System.out.println("difference is " + (timediff/(1000*3600*24))+ " days");
在这里,您将timediff/1000
的逻辑应用为秒,timediff/1000*60
作为分钟等。