获取当前日期和时间 [英] Getting Current date and time

查看:214
本文介绍了获取当前日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Android应用程序中访问当前日期时间如下:

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);

它给出了1970年的日期和时间,如下:

It gives me date and time with the year 1970 as follows :

>>>>>>>Current Date :﹕ Thu Jan 01 05:30:00 GMT+05:30 1970

但是,它应该是2015而不是1970.
问题是什么?

but, It should be 2015 instead of 1970. What the problem is ?




我已经从提供的解决方案中解决了上述问题。事实上,我生成通知作为datetime值从databse匹配当前datetime值。但不会产生通知。

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.



我的代码如下:

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();
}

我已经在Logcat中检查了这两个值,如下所示:

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


推荐答案

您尚未在日历对象中设置当前日期。

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);

您可以使用SimpleDateFormat类将日期格式化为

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()));

这篇关于获取当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆