CalendarView getDate()不返回时间 [英] CalendarView getDate() does not return time

查看:36
本文介绍了CalendarView getDate()不返回时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CalendarView显示日历,并加载当前日期和时间.我需要获取当前日期和在活动中使用它们的时间.因此,时间应该是当前时间,但是用户可以选择其他日期.

I'm using the CalendarView to show a calendar, and load the current date&time. What I need is to get the current date and the time to use them in my activity. So, the time should be the current time, but the user is able to select another date.

这是我的实现方式:

final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

AlertDialog diaBox = dateDialogBox();
diaBox.show();
final CalendarView calendarView = (CalendarView) diaBox.findViewById(R.id.calendarView);

Calendar calendar = Calendar.getInstance();
formatedDate = sdf.format(calendar.getTime());
dateDialog.setTitle(formatedDate);
dateInMillis = calendar.getTimeInMillis();

calendarView.setOnDateChangeListener(new OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        formatedDate = sdf.format(view.getDate());
        dateDialog.setTitle(formatedDate);
        dateInMillis = calendarView.getDate();
    }
});

提示对话框时,它是日历的日期和时间,并且正确执行.但是,当我选择另一个日期时,它将输入 setOnDateChangeListener(),并在此使用 CalendarView 而不是 Calendar 来获取日期和时间.因此,我可以正确获取日期,但是时间以00:00:00的方式显示.

When the dialog is prompted, it get's the date and time from Calendar and it does it right. But when I select another date, it enters in setOnDateChangeListener() and here uses the CalendarView to get the date and time instead of Calendar. Because of this, I get the date correctly but the time appears this way 00:00:00.

我该怎么做才能从CalendarView获取当前时间?

What should I do to get the current time from CalendarView?

推荐答案

CalendarView不包含时间信息(因为它只是日历).如果您想保留时间信息并仅应用选定的日期,请尝试使用Calender.set()这样的方法

CalendarView does not include time information (because it's just calendar). If you want to keep your time information and just apply selected dates, try to use Calender.set() method like this

final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

AlertDialog diaBox = dateDialogBox();
diaBox.show();
final CalendarView calendarView = (CalendarView) diaBox.findViewById(R.id.calendarView);

final Calendar calendar = Calendar.getInstance();
formatedDate = sdf.format(calendar.getTime());
dateDialog.setTitle(formatedDate);
dateInMillis = calendar.getTimeInMillis();

calendarView.setOnDateChangeListener(new OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        calendar.set(year, month, dayOfMonth);
        long selectedDateInMillis = calendar.getTimeInMillis();
        formatedDate = sdf.format(selectedDateInMillis);
        dateDialog.setTitle(formatedDate);
        dateInMillis = selectedDateInMillis;
    }
});

这篇关于CalendarView getDate()不返回时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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