Android calendar one day error

Asked 2 years ago, Updated 2 years ago, 26 views


                TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker);
                int hour = timePicker.getHour();
                int minute = timePicker.getMinute();

                Calendar calendar = Calendar.getInstance();

                calendar.add(Calendar.DATE, 1);
                calendar.set(Calendar.HOUR_OF_DAY, hour-9);
                calendar.set(Calendar.MINUTE, minute);
                calendar.set(Calendar.SECOND, 0);

                Date date = calendar.getTime();

                Intent intent = new Intent(SubActivity.this, AlarmDialogActivity.class);
                intent.putExtra("Ryington", ringtonUri); // Send RingtonUri value to intent
                PendingIntent pIntent = PendingIntent.getActivity(SubActivity.this, 0, intent, 0);

                log.i ("time set by logging", "+date);

                //Create Alarm Manager
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);

After creating a calendar, take the value from the timepicker and modify it to make the alarm go off at that time Yes.

If you set the calendar as the default, the value indicates the current time.

But the problem here is that if I set the time and minutes and record it, Everything else is accurate at that time, but the DATE value, that is, the number of days, is jagged by a day.

When the actual current time is August 30th, sometimes it's August 30th After a day, it is taken at the price before the day, and after another day, it is taken at the normal DATE value.

After a day's walk, it was accurate, but the DATE was set at the previous day's price calendar.add(Calendar.DATE, 1); I wrote and erased this code and I'm repeating it.

Until yesterday, it was filmed exactly on August 29th, but today it's August 30th, so it's August 29th, the day before I added calendar.add(Calendar.DATE,1) again because it worked

android

2022-09-22 14:01

1 Answers

I think we should check the following two things.

1) Check how the time zone of the device is set. In "Settings > Date and Time > Select Time Zone" (menu name is based on Nexus device), it is necessary to verify that it is set to GMT+09:00 Korea Standard Time. If it is not GMT+09:00, it may differ from the current time (=Seoul time).

2) If you look at the code you uploaded calendar.set(Calendar.HOUR_OF_DAY, hour-9; line, why do you do this at the current time? The current code is set to a time 9 hours before the time you set in the time picker.


2022-09-22 14:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.