Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

android - How to get national holidays of selected country

I'm writing an app that allows the user to select a country from a list. When they pick a country, a list of national holidays of that country appears.

I've seen CalendarProvider and Google Calendar V3, but they seem to only provide the current user's calendar.

How can I get the national holidays of a specific country? Any suggestion would help me.


Update:

I've found this post which can get a list of holidays in JSON format. In this example, the country code is defined by calendar ID, for instance pt_br.brazilian#holiday@group.v.calendar.google.com or en.usa#holiday@group.v.calendar.google.com.

But I can't find any documentation about this. Is it possible to use the API this way? If yes, where can I get those calendar IDs?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem solved by using Google Calendar API V3. The idea I found from this post. The holiday can get from default holiday calendar of google.

The ID list of default holiday calendar can be found here, support to 40 country.

A piece of code that handle permission and get holiday list:-

com.google.api.services.calendar.Calendar client = null;
        credential = GoogleAccountCredential.usingOAuth2(mContext, CalendarScopes.CALENDAR);
        credential.setSelectedAccountName(mList.get(0));
        client = getCalendarService(credential);
        do {
            com.google.api.services.calendar.model.Events events;
            events = client.events().list("en.usa#holiday@group.v.calendar.google.com").setPageToken(pageToken).execute();
            onHolidayChecked(events.getItems()); //result return here (events.getItems())
            pageToken = events.getNextPageToken();
        } while (pageToken != null);

private com.google.api.services.calendar.Calendar getCalendarService(GoogleAccountCredential credential) {
    return new com.google.api.services.calendar.Calendar.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...