编程语言
首页 > 编程语言> > java-如何使用TimePicker获取今天的日期/时间?

java-如何使用TimePicker获取今天的日期/时间?

作者:互联网

我有一个TimePicker来获取时间.而且我正在为Android应用程序遵循Java代码:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Log.e("Alarm", "" + calendar.getTime());

calendar.set(Calendar.HOUR, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
Log.e("Alarm", "" + calendar.getTime());

第二个过程比第一个过程提前一天.

我试过下面的代码,并减去了一天.

calendar.add(Calendar.DATE, -1);

这是唯一的方法还是最好的方法?如何使用TimePicker获取今天的日期/时间?

解决方法:

检查以下代码…..可能会从中获得帮助

public void setCurrentTimeOnView() {

    tvDisplayTime = (TextView) findViewById(R.id.tvTime);
    timePicker1 = (TimePicker) findViewById(R.id.timePicker1);

    final Calendar c = Calendar.getInstance();
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

    // set current time into textview
    tvDisplayTime.setText(
                new StringBuilder().append(pad(hour))
                                   .append(":").append(pad(minute)));

    // set current time into timepicker
    timePicker1.setCurrentHour(hour);
    timePicker1.setCurrentMinute(minute); 
}

标签:timepicker,calendar,java,android,date
来源: https://codeday.me/bug/20191029/1961288.html