0

I have to find time difference between two String values.I used the following code in my app,

public String diff(String first,String last)
{
    SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
    Date  d1 = null;
    Date d2 = null;
    try {
        d1 = format.parse(first);
        d2 = format.parse(last);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    long diff = d2.getTime() - d1.getTime();
    int diffMinutes = (int)diff / (60 * 1000);
    int diffHours = (int)diff / (60 * 60 * 1000);
    String res=Integer.toString(diffHours);
    String res1=Integer.toString(diffMinutes);
    String fin= res+res1;
    return fin;
}

This is a function which accepts my two Strings and returns a String(Time Diff) which i can use in my class. But my app force closes when i am calling this function on a button click.

Here is the Logs!

Process: mypackage.com.work, PID: 3551 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String mypackage.com.work.UpdateTracker.diff(java.lang.String, java.lang.String)' on a null object reference at mypackage.com.work.UpdateTracker.onClick(UpdateTracker.java:138) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)`

Here is the code of calling that function

         String in_db=txtTime.getText().toString();
            String out_db=txtTime2.getText().toString();
            String d=up.diff(in_db,out_db);

If I just comment this and insert just a hard coded String,its working fine!.So I think no problem with DB code

Help me out to figure this!!

Thanks in advance

lrleon
  • 2,610
  • 3
  • 25
  • 38
Sriram S
  • 169
  • 14

1 Answers1

0

Try this. You can save and retrieve your time as String:

String to long: String.valueOf()
long to String: Long.valueOf()

Use this procedure for calculating the difference:

//Time checker
private String timeCheck(long startTimeInput, long endTimeInput) {
    long millis = endTimeInput - startTimeInput;
    int seconds = (int) (millis / 1000);
    int minutes = seconds / 60;
    int hours = minutes / 60;

    return "Hours: "+hours+" minutes: "+minutes+" seconds: "+seconds;
}

And in your main program call it like this:

String start = "Tue Apr 23 16:08:28 GMT+05:30 2015";
String end = "Tue Apr 23 18:12:30 GMT+05:30 2015";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
    Date mDateStart = sdf.parse(start);
    Date mDateEnd = sdf.parse(end);
    long timeInMillisStart = mDateStart.getTime();
    long timeInMillisEnd = mDateEnd.getTime();
    Log.d(LOG_TAG,"The difference is "+timeCheck(timeInMillisStart,timeInMillisEnd));

} catch (ParseException e) {
    e.printStackTrace();
}

I hope it helps.

Mohammad
  • 6,024
  • 3
  • 22
  • 30