Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
118 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have not set current date in calender object.

 Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    //Calendar.SECOND will return only seconds from the date
    //int seconds = c.get(Calendar.SECOND);
    long time = c.getTime();
    Date date2 = new Date(time);
    Log.d(">>>>>>>Current Date : ",""+date2);

You can use SimpleDateFormat class to format the dates as follow

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
System.out.println(format.format(new Date()));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...