当我调用此方法时,它有时显示异常
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();
}
我的错误:
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)
日期格式:
public static String SLASH_MM_DD_YYYY = "dd-MMM-yyyy";
任何解决方案吗?
答案 0 :(得分:3)
如果你的约会对象没有,那就会发生这种情况。将抛出 ParseException 。您可以在catch
中处理它 try {
d = formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
// ParseException will thrown
}
您应该将您的方法用作。
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;
}
答案 1 :(得分:0)
这应该解决它:
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);
}
}
问题是你的d没有被解析,所以你可以捕获它,或者你可以通过改变你解析它或接收它的方式来确保它被解析。 你也可以这样做:
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();
}
这样你就会在异常时返回调用
答案 2 :(得分:0)
请指定使用此方法时发送的格式
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();
}
使用此方法时,即getTimestampFromDate()日期应为年月日和小时