Java 用最新方法获取并比较android中的时间
目前,我正试图在我的android应用程序中获得两个小时的时间间隔,如下所示Java 用最新方法获取并比较android中的时间,java,android,Java,Android,目前,我正试图在我的android应用程序中获得两个小时的时间间隔,如下所示 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.ENGLISH); try { simpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.ENGLISH); Date da
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
try {
simpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
Date date1 = simpleDateFormat.parse(sessionManager.getStringValue("intime"));
Date date2 = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
Date currentDate = new Date();
Log.e("REWARD STORE DATE",date1+"");
Log.e("REWARD CAURRENT DATE",date2+"");
long difference = 0;
if (date2 != null) {
if (date1 != null) {
difference = date2.getTime() - date1.getTime();
}
}
int days = (int) (difference / (1000 * 60 * 60 * 24));
int hours = (int) ((difference - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));
int min = (int) (difference - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours)) / (1000 * 60);
hours = (hours < 0 ? -hours : hours);
Log.e("REWARD ",hours+"");
if (hours >= 24) {
Log.e("REWARD: ","CALLED"+"");
new GlobalApi().rewardUser("2");
sessionManager.saveStringValue("intime", new SimpleDateFormat("hh:mm a", Locale.ENGLISH).format(new Date()));
}
Log.i("======= Hours", " :: " + hours + ":" + min);
} catch (ParseException e) {
e.printStackTrace();
}
我想我遗漏了一些东西,但我不知道我的代码出了什么问题,请告诉我是否有人可以建议我最好和最新的方法,以便在几个小时内进行两次比较。我是java和android新手。
谢谢顺便说一句,考虑扔掉那些过时的、臭名昭著的麻烦代码< SimpleDateFormat > /代码>和朋友们。看看是否可以使用或添加到Android项目中,以便使用java.time,即现代java日期和时间API。和你一起工作真是太好了。
CAURRENT DATE: Thu Jan 01 07:40:00 GMT+05:30 1970