0

I'm trying to add event but it shows a different date that I'm trying to put and what I'm getting.

eventButton.setOnClickListener(
        new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendarEvent = Calendar.getInstance();
                Intent calendarIntent = new Intent(Intent.ACTION_EDIT);
                calendarIntent.setType("vnd.android.cursor.item/event");

                Calendar time = Calendar.getInstance();
                time.clear();
                time.set(2018, 05, 23);

                calendarIntent.putExtra(CalendarContract.Events.TITLE, "Whatever");
                calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,time.getTimeInMillis());
                calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,time.getTimeInMillis());
                calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY,true);
                calendarIntent.putExtra(CalendarContract.Events.RRULE, "FREQ=YEARLY");
                startActivity(calendarIntent);

            }
        }
);

I'm getting this Jun 22, instead of May 23: screenshot

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
CoDesign
  • 43
  • 1
  • 8
  • 1
    Please don’t use 05 for 5. It works, but it’s a bad habit since you will get a compile error once you try 08 for 8. – Ole V.V. Apr 16 '18 at 10:33
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Apr 17 '18 at 18:50
  • Were you able to fix your issue? Do you need any more help? –  Apr 18 '18 at 22:13

2 Answers2

1
    long eventTimeInMillis = LocalDate.of(2018, Month.MAY, 23)
            .atStartOfDay(ZoneOffset.UTC)
            .toInstant()
            .toEpochMilli();

As Jamie Corkhill said in a comment, you need the time at the start of day (00:00) in UTC. The above code not only gives you the time at 00:00 in UTC, it is also pretty clear about that this is what it gives you.

I am using java.time, the modern Java date and time API. I find it much nicer to work with than the long outdated Calendar class.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

The Java Calendar.set() method counts months just like arrays in programming, starting from 0 for January. Therefore, your month should be 4 for May, since January is 0, February is 1, March is 2, and so on. That is what I believe is causing your issue.

You may also use the Month enumeration. That is, instead of using your line

time.set(2018, 05, 23);

Use

time.set(2018, Calendar.May, 23);

I hope this helps.

  • Thanks for the answer. – CoDesign Apr 15 '18 at 19:20
  • You're right, this solves the Month problem but the day is again lesser by one, so, is there any other way for the day too or I just have to increment by one the date I've to add? – CoDesign Apr 15 '18 at 19:24
  • @CoDesign I'll get back to you on that shortly, I'm going to look into it and test it myself. –  Apr 15 '18 at 19:27
  • @CoDesign From the docs, "If allDay is set to 1 eventTimezone must be TIMEZONE_UTC and the time must correspond to a midnight boundary." That means you should use `Calendar.getInstance(TimeZone.getTimeZone("UTC"));` –  Apr 15 '18 at 19:43
  • @CoDesign I just tested your code on my emulator with the only change of decrementing your month by one as per my answer, and I get May 23rd, which is what you want. What timezone is your device in? It would be helpful if you could log `calendarEvent.getTimeZone().toString();` and show me it. –  Apr 15 '18 at 19:53