android 如何将选中的hourOfDay和minute放到一个变量中与当前时间进行比较?

9o685dep  于 2022-12-02  发布在  Android
关注(0)|答案(2)|浏览(120)

我尝试做一个if-else语句,如果用户输入了过去的时间,它会提醒用户时间已经过去。而且我也无法在用户打开timedialogpicker时将其设置为当前时间。提前感谢。
代码:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Calendar timeNow = Calendar.getInstance();
        timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                int currHour = calendar.get(Calendar.HOUR_OF_DAY);
                int currMin = calendar.get(Calendar.MINUTE);
                Time currTime = new Time(currHour, currMin); //This part display error
                if (currTime.getTimeInMillis() >= timeNow.getTimeInMillis()) { //This part display error
                    //it's after current
                    int hour = hourOfDay % 12;
                } else {
                    //it's before current'
                    Toast.makeText(getApplicationContext(), "Invalid Time", Toast.LENGTH_LONG).show();
                }
                time = "";
                time = hourOfDay+":"+minute;

                Userbook book = new Userbook(temp, time);

                db.collection("Booking").add(book).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {
                        Toast.makeText(MainActivity.this, "Note saved", Toast.LENGTH_SHORT).show();
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                    }
                });
               Toast.makeText(MainActivity.this, "Time is "+currHour+":"+currMin, Toast.LENGTH_SHORT).show();
            }
        },timeNow.get(Calendar.HOUR_OF_DAY), timeNow.get(Calendar.MINUTE),false);
        timePickerDialog.show();
    }
});
pbpqsu0x

pbpqsu0x1#

简而言之:! LocalTime.of( 10 , 20 ).isBefore( LocalTime.now() )

LocalTime#isBefore

java.time API于2014年3月随Java-8一起发布,取代了error-prone legacy date-time API。从那时起,强烈建议使用这种现代的日期-时间API。
您可以使用LocalTime#of(int hour, int minute)获取具有给定hourminute值的LocalTime示例,然后使用LocalTime#isBefore将其与当前时间(即LocalTime#now)进行比较。

演示

import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();

        // Sample hour and minute values
        int hour = 10;
        int minute = 20;

        LocalTime givenTime = LocalTime.of(hour, minute);

        System.out.println(!givenTime.isBefore(now));
    }
}

输出

false

!givenTime.isBefore(now)表示givenTime等于或晚于now
从**Trail: Date Time**了解有关现代日期-时间API的更多信息。

ia2d9nvy

ia2d9nvy2#

一种方法是将日历的时间设置为newDate(),然后将小时和分钟设置为选定的值。例如,
然后,您可以使用Java内置的compareTo函数来比较这两个日期。例如:

Calendar timeNow = Calendar.getInstance();
        Date curDate = new Date();
        timeNow.setTime(curDate);
        timeNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
        timeNow.set(Calendar.MINUTE, minute);
        // If you want to warn on dates being equal to current time, then 
        // switch the conditional to be greater than or equal to 0, instead
        // of greater than 0. 
        if (curDate.compareTo(timeNow.getTime()) > 0) {
           //it's after current
           int hour = hourOfDay % 12;
        }
        ...
        ...

相关问题