user3713946
user3713946

Reputation: 27

Subtract Time from PHP-Timestamp (String)

I get a timestamp from php-server (String zeit) via JSONObject. How can I subtract the current time from that responed timestamp that I get the "time since" value? I hope I have formulated my problem understandable.

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 {

Upvotes: 0

Views: 178

Answers (1)

Subin Thomas
Subin Thomas

Reputation: 1416

Here is the code to find difference in time.

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");

Here you apply your logic like timediff/1000 as seconds, timediff/1000*60 as minutes etc.

Upvotes: 2

Related Questions