Calendar set () method does not set the correct time

I set the time for my calendar instance as follows

private Calendar mCalendar = Calendar.getInstance();

public void onTimeSet(int hourOfDay, int minute, int second) {
    int year = mCalendar.get(Calendar.YEAR);
    int monthOfYear = mCalendar.get(Calendar.MONTH);
    int dayOfMonth = mCalendar.get(Calendar.DAY_OF_MONTH);

    Calendar lCalendar = Calendar.getInstance();
    lCalendar.set(year, monthOfYear, dayOfMonth, hourOfDay, minute);

    Date lDate = lCalendar.getTime();
    if (lDate.before(new Date())) {
        return;
    } else {
        mCalendar.set(year, monthOfYear, dayOfMonth, hourOfDay, minute);
        setItemTime();
    }
}

However, when I call getTime () on instances of Calendar lCalendar and mCalendar, I get the time at which the calendar was not created from hourOfDayand the minutearguments set in the instances of the calendar. Also, if I call the method again getTime()in both cases, I get the correct time.

Why is this happening and how can I solve it?

+4
source share
2 answers

Go to the section Manipulating the field in the calendar documentation

time time, . Calendar time set(...).

, ,

.

, , .

+2

. System.out.println() if. , lDate . ( ), mCalendar , , , , . , onTimeSet()?

, second? set() , . lCalendar.clear(), .

0

Source: https://habr.com/ru/post/1627022/


All Articles