0

I found this code at Google Quickstart calendar API and I have been trying to format the time output but I'm not succeeding.

I tried changing time zones and reading documentation from Android developers but I didn't manage to fix it.

I'm trying to get something like

2017-07-29 16:00:00

But I'm getting

2017-07-29T14:00:00.000Z

Also time zone is different it should be +2.

    private List<String> getDataFromApi() throws IOException {
        DateTime now = new DateTime(System.currentTimeMillis());
        List<String> eventStrings = new ArrayList<String>();
        Events events = mService.events().list("primary")
                .setMaxResults(10)
                .setTimeMin(now)
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute();
        List<Event> items = events.getItems();

        for (Event event : items) {
            DateTime start = event.getStart().getDateTime();
            if (start == null) {
                start = event.getStart().getDate();
            }
            eventStrings.add(
                    String.format("%s (%s)", event.getSummary(), start));
        }
        return eventStrings;
    }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
BigBoiVladica
  • 181
  • 1
  • 12

2 Answers2

0

The Calendar API's default timezone is UTC (which is what you're seeing), formatted according to RFC3339.

The time, as a combined date-time value (formatted according to RFC3339). A time zone offset is required unless a time zone is explicitly specified in timeZone.

Try using toTimeString() to convert it to human-readable format. You can also check this SO post for other alternatives.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
0
private String getFormattedDate(Date date)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+05:30");
        df.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
        return df.format(date);
    }
Siju
  • 2,585
  • 4
  • 29
  • 53
Toji
  • 1