3

As i am new to android development I am unable to find code for calculating the difference between two datetime formats. My question is.

I am using webservice in my project where i am getting datetime response as follows..

 starttime  :- [2012-11-04 10:00:00]
 endtime    :- [2012-11-04 12:00:00]

Here i want to display on screen as

   Today Timings  :-  2012-11-04   2 hours

Means need to calculate the difference between two dates and need to display the time difference on screen.

Can anyone please help me with this.

  • Are you trying to get time and date difference both? or just time? – Vinay Nov 04 '12 at 06:47
  • I just want time difference and need to display starttime as i mentioned above.. in this pattern "Today Timings :- 2012-11-04 2 hours" –  Nov 04 '12 at 10:54

4 Answers4

1

Given you know the exact format in which you are getting the date-time object, you could use the SimpleDateFormat class in Android which allows you to parse a string as a date given the format of the date expressed in the string. For your question:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startTime = sdf.parse(<startTime/endTime String>, 0);

Similarly parse your endtime, and then the difference can be obtained using getTime() of the individual objects.

long diff = endTime.getTime() - startTime.getTime()

Then it's as simple as converting the difference to hours using:

int hours = (int)(diff/(60*60*1000));

Hope that helps.

varevarao
  • 2,186
  • 13
  • 26
  • Thanks dude, solved the issue. Here other thing which i want is as per my question i need to seperate date from startTime string & need to display as like this "2012-11-04 2 hours". So, how can i do that..? –  Nov 04 '12 at 19:00
  • Ah sorry missed that part I guess. Use the substring method of Java to get the string containing your Date string. The docs have this method as String `substring(int startIndex, int endIndex)` So for `starttime :- [2012-11-04 10:00:00]` this would be something like `timeString = inputString.substring(15, inputString.length());`. – varevarao Nov 05 '12 at 05:46
1

java.time

Solution using java.time, the modern API:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s", Locale.ENGLISH);
        LocalDateTime startTime = LocalDateTime.parse("2012-11-04 10:00:00", dtf);
        LocalDateTime endTime = LocalDateTime.parse("2012-11-04 12:00:00", dtf);
        long hours = ChronoUnit.HOURS.between(startTime, endTime);
        System.out.println(hours);
    }
}

Output:

2

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

If you're using java.util.Date; you're gonna have to use an intermediate variable. you can convert your dates to long values and subtract those and then convert your long back to a date.

    long diff = new Date().getTime() - new Date(milliseconds).getTime();
    Date dateDiff = new Date(diff);

But you should be warned that it doesn't take daylight savings and whatever else variables into account. if you're that scrupulous, you might be indulged with this replacement date class. It has the functionality you seek.

mango
  • 5,577
  • 4
  • 29
  • 41
0

I use this code to get difference of two date.

public void getTimeDifference(Date endDate,Date startDate) {
            Date diff = new Date(endDate.getTime() - startDate.getTime());
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            calendar.setTime(diff);
            int day=calendar.get(Calendar.DAY_OF_MONTH);
            int hours = calendar.get(Calendar.HOUR_OF_DAY);
            }
        }
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87