当前日期和输入日期在android中的区别 [英] Difference between current date and entered date in android
问题描述
这里我发现输入日期和当前日期之间的区别。我的意思是我试图找到两个日期之间的日子。如果两个日期都在同一个月,如果日期在不同的月份,那么我得到正确的值,差值是不正确的。我没有得到如何纠正这一个。
Here I am finding the difference between the entered date and current date. I mean I am trying to find the no of days between two Dates. If both the dates are in the same month, I am getting correct value if the dates are in different months, the difference value is incorrect. I am not getting how to rectify this one.
任何人都可以帮助我获得这个?
can anyone pls help me get this one?
是我的代码:
public class MyService extends Service {
String dayDifference;
NotificationManager manager;
Notification myNotication;
@Override public IBinder onBind(Intent intent) {
return null;
}//End of onBind method
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
try {
//Dates to compare
String CurrentDate;
String FinalDate = "08/24/2016";
Date date1 = new Date();
Date date2;
SimpleDateFormat dates = new SimpleDateFormat("mm/dd/yyyy");
//Setting dates
date1.setTime(System.currentTimeMillis());
CurrentDate = dates.format(date1);
date2 = dates.parse(FinalDate);
date1 = dates.parse(CurrentDate);
//Comparing dates
long difference = Math.abs(date1.getTime() - date2.getTime());
long differenceDates = difference / (24 * 60 * 60 * 1000);
//Convert long to String
dayDifference = Long.toString(differenceDates);
Log.e("HERE.................", "HERE: " + dayDifference);
System.out.println("..............difference date " + dayDifference);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent targetIntent = new Intent(this, Mnst.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, targetIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setAutoCancel(true);
builder.setTicker("Predictor");
builder.setContentTitle("Miisky Notification");
builder.setContentText(
"You have " + dayDifference + " days left. Click on notification to know more..");
builder.setSmallIcon(R.drawable.miilogo);
builder.setSound(soundUri);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
//builder.setSubText("Your vault number is " + s.vault_no); //API level 16
builder.setNumber(100);
builder.build();
//noinspection deprecation
myNotication = builder.getNotification();
builder.setAutoCancel(true);
manager.notify(11, myNotication);
Toast.makeText(getApplicationContext(), " " + dayDifference + " Days Left ",
Toast.LENGTH_LONG).show();
} catch (Exception exception) {
Log.e("DIDN'T WORK............", "exception " + exception);
}
}
}
日期为08/24/2016,当前日期为09/28/2016。结果不正确,我得到4。我想我必须使用日历或某事,但是没有得到确切的方法来做那个..
推荐答案
根据文档,信 m
被理解为分钟小时。为了参考月份,您需要使用资本 M
。
According to documentation the letter m
is understood as minute in hour. In order to refer to month you need to use capital M
.
尝试更改您的 SimpleDateFormat
到:
SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");
这篇关于当前日期和输入日期在android中的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!