I'm building an Android application and the application enables the user to insert events to Google Calendar and external calendar (like Exchange account).
The problem is that if the user wants to add an event after 2038, it creates it in the past (January 2038 becomes December 1901).
I did some research and figured that the problem is "Year 2038 problem". From Wikipedia:
The Year 2038 problem is an issue for computing and data storage situations in which time values are stored or calculated as a signed 32-bit integer, and this number is interpreted as the number of seconds since 00:00:00 UTC on 1 January 1970. Such implementations cannot encode times after 03:14:07 UTC on 19 January 2038.
The latest time that can be represented in Unix's signed 32-bit integer time format is 03:14:07 UTC on Tuesday, 19 January 2038 (2,147,483,647 seconds after 1 January 1970). Times beyond that will "wrap around" and be stored internally as a negative number, which these systems will interpret as having occurred on 13 December 1901 rather than 19 January 2038. This is caused by integer overflow.
Is there any way to handle it?
This is my code:
private void InsertEvent(MyEvent myEvent) {
ContentValues values = initEventValue(myEvent);
Uri EVENTS_URI = Uri.parse(getCalendarUriBase() + "events");
Uri u1 = contentResolver.insert(EVENTS_URI, values );
}
private ContentValues initEventValue(MyEvent myEvent) {
ContentValues eventValues = new ContentValues();
eventValues.put("eventTimezone", TimeZone.getDefault().getID());
eventValues.put("calendar_id", myEvent.calId);
eventValues.put("title",myEvent.title);
eventValues.put("allDay", 1);
long dateStart = myEvent.startDate.getTime();
eventValues.put("dtstart", dateStart );
long dateEnd = myEvent.endDate.getTime();
eventValues.put("dtend", dateEnd );
return eventValues;
}