-4

When I call this method sometimes it's shows exception

public static long getTimestampFromDate(String date, String format) {
    DateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH);
    //formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d = null;
    try {
        d = formatter.parse(date);
    } catch (Exception e) {
        e.printStackTrace();
        Crashlytics.logException(e);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(d);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 1);
    return calendar.getTimeInMillis();
}

My error :

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference
       at java.util.Calendar.setTime(Calendar.java:1197)
       at com.utils.Utils.getTimestampFromDate(SourceFile:105)
       at com.fragment.MyProfileFragment.onViewClicked(SourceFile:768)
       at com.fragment.MyProfileFragment_ViewBinding$4.doClick(SourceFile:79)
       at butterknife.internal.DebouncingOnClickListener.onClick(SourceFile:22)
       at android.view.View.performClick(View.java:5205)
       at android.view.View$PerformClick.run(View.java:21162)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5453)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)

Date format :

public static String SLASH_MM_DD_YYYY = "dd-MMM-yyyy";

Any solutions for this?

Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57

3 Answers3

3

This will happen when your date is not in desire formate . ParseException will thrown.You can handle it in catch

 try {
    d = formatter.parse(date);
} catch (ParseException e) {
    e.printStackTrace();
   // ParseException will thrown
}

You should use your method as.

 public static long getTimestampFromDate(String date, String format) {
    DateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH);
    //formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d = null;
    try {
        d = formatter.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 1);
        return calendar.getTimeInMillis();
    } catch (Exception e) {
        e.printStackTrace();
        Crashlytics.logException(e);
    }
    // return the default value here 
    // Can be System.currentTimeMillis();
   return 0;
}
ADM
  • 20,406
  • 11
  • 52
  • 83
0

This should fix it:

public static long getTimestampFromDate(String date, String format) {
    DateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH);
    //formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d = null;
    try {
        d = formatter.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 1);
        return calendar.getTimeInMillis();
    } catch (Exception e) {
        e.printStackTrace();
        Crashlytics.logException(e);
    }
} 

the problem is that your d is not being parsed, so you can catch that or you can make sure its being parsed by changing the way you are parsing it or recieving it. You can also do it this way :

public static long getTimestampFromDate(String date, String format) {
    DateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH);
    //formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d = null;
    try {
        d = formatter.parse(date);
    } catch (Exception e) {
        Crashlytics.logException(e);
        return 0;
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(d);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 1);
    return calendar.getTimeInMillis();
}

this way you will return the call upon exception

Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48
0

Please specify the format that you are sending when you are using this method

public static long getTimestampFromDate(String date, String format) {
 DateFormat formatter = new SimpleDateFormat(yyyy-DD-MM hh:mm);
    //formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d = null;
    try {
        d = formatter.parse(date);
    } catch (Exception e) {
        e.printStackTrace();
        Crashlytics.logException(e);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(d);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 1);
    return calendar.getTimeInMillis();
}

while using this method i.e getTimestampFromDate() the date should be in year month day hour and minute

ADM
  • 20,406
  • 11
  • 52
  • 83
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18