从 PHP-时间戳(字符串)中减去时间
Subtract Time from PHP-Timestamp (String)
我通过 JSONObject 从 php-server (String zeit) 获取时间戳。如何从获得 "time since" 值的响应时间戳中减去当前时间?我希望我的问题可以理解。
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 {
这是查找时间差异的代码。
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
分钟等。
我通过 JSONObject 从 php-server (String zeit) 获取时间戳。如何从获得 "time since" 值的响应时间戳中减去当前时间?我希望我的问题可以理解。
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 {
这是查找时间差异的代码。
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
分钟等。