简体   繁体   中英

Google Calendar Api - convert DataTime to Calendar object

I am trying to convert this DataTime object to Calendar object.

DateTime start = event.getStart().getDateTime();

DateTime format: 2015-11-11T19:25:55.000Z I need format: "yyyy/MM/dd HH:mm:ss"

How can I cut TimeZone and convert it to Calendar object?

Original Google Calendar Api:

private List<String> getDataFromApi() throws IOException {
    // List the next 10 events from the primary calendar.
    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) {
            // All-day events don't have start times, so just use
            // the start date.
            start = event.getStart().getDate();
        }
        eventStrings.add(String.format("%s (%s)", event.getSummary(),start));
    }
    return eventStrings;
}

You can use SimpleDateFormat to parse your 2015-11-11T19:25:55.000Z into an actual Date object. Once you've got that Date object, it's as easy as:

Calendar calendar = Calendar.getInstance();
calendar.setTime(your_date_object);

I hope this helps!

I'm trying do somthing with it, but its not work.

            DateTime start = event.getStart().getDateTime();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
            String newDate = dateFormat.format(start);

Maybe someone know why i can't parse this data

            DateTime start = event.getStart().getDateTime();
            String NewDate = String.format("(%s)", start);

            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.getDefault());
            cal.setTime(sdf.parse(NewDate));

All the time i have unparseable date exception.

I tried formats:

yyyy-MM-dd'T'HH:mm:ss.SSSZ

yyyy-MM-dd'T'HH:mm:ssZZZZZ

yyyy-MM-dd'T'HH:mm:ss.SSSZZ

This work for me:

SimpleDateFormat dtExibicao = new SimpleDateFormat("dd/MM/yyyy");
Date dtInicio = new Date(event.getStart().getDateTime().getValue());
String stInicio = dtExibicao.format(dtInicio);

If you wanna a java.util.Calendar you can use a @k3v1n4ud3 sample code:

java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime(dtInicio);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM