I am using the this code to convert date time to unixtime gmt . It is working fine on most of the devices but it is crashing in a few . I am unable to determine the cause. How can I fix it ?
int gmtOffset = TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings();
String dt="11-01-2016 5:8 AM";
DateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
try {
date = format.parse(dt);
System.out.println("Date ->" + date);
} catch (Exception e) {
e.printStackTrace();
}
long unixtime = (date.getTime() -(gmtOffset))/1000;
error:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference