I tried to access current datetime in android application as follows :
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
//long time = System.currentTimeMillis();
Date date2 = new Date(seconds);
Log.d(">>>>>>>Current Date : ",""+date2);
It gives me date and time with the year 1970 as follows :
>>>>>>>Current Date :﹕ Thu Jan 01 05:30:00 GMT+05:30 1970
but, It should be 2015 instead of 1970. What the problem is ?
I have solved above problem from solution provided. Atually, I am generating notification as the datetime value from the databse matches to the current datetime value. but, it does not generating notification.
My code is as follows :
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
doAsynchTask = new TimerTask() {
@Override
public void run() {
Log.d("Timer Task Background", "Timer task background");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
long time = System.currentTimeMillis();
Date dateCurrent = new Date(time);
Log.d(">>>>>>>Current Date : ", "" + dateCurrent);
getListData();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
Date dateFromDatabase;
for (int i = 0; i < remiderList.size(); i++) {
try {
System.out.print("+++" + remiderList.get(i).toString());
dateFromDatabase = formatter.parse(remiderList.get(i).toString());
Log.d(">>>>>Database date : ", "" + dateFromDatabase);
if (dateCurrent.equals(dateFromDatabase)) {
Toast.makeText(getApplicationContext(), "Date matched", Toast.LENGTH_LONG).show();
displayNotification();
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
};
timer.schedule(doAsynchTask, 0, 1000);
}
public void displayNotification() {
Notification.Builder builder = new Notification.Builder(MyRemiderService.this);
Intent intent1 = new Intent(this.getApplicationContext(),
HomeActivity.class);
Notification notification = new Notification(R.drawable.notification_template_icon_bg,
"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)
.setContentTitle("ContentTitle").setContentText("this for test massage")
.setContentIntent(pendingNotificationIntent);
notification = builder.getNotification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
/* notification.setLatestEventInfo(this.getApplicationContext(),
"AlarmManagerDemo", "This is a test message!",
pendingNotificationIntent);*/
mManager.notify(0, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void getListData() {
remiderList = dbHelper.getAllRemiders();
}
I have checked both the values in Logcat as follows :
09-15 17:50:00.629 17915-17927/? D/>>>>>>>Current Date :﹕ Tue Sep 15 17:50:00 GMT+05:30 2015
09-15 17:50:00.637 17915-17927/? D/>>>>>Database date :﹕ Tue Sep 15 17:50:00 GMT+05:30 2015