如何获得所选国家的国定假日 [英] How to get national holidays of selected country

查看:48
本文介绍了如何获得所选国家的国定假日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个允许用户从列表中选择国家的应用程序.当他们选择一个国家时,会出现该国家的国定假日列表.

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.

我见过 CalendarProviderGoogle Calendar V3,但它们似乎只提供当前用户的日历.

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.

更新:

我发现这篇文章可以得到JSON 格式的假期列表.在此示例中,国家/地区代码由日历 ID 定义,例如 pt_br.brazilian#holiday@group.v.calendar.google.com 或 en.usa#holiday@group.v.calendar.google.com.

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.

但我找不到任何关于此的文档.是否可以通过这种方式使用 API?如果是,我在哪里可以获得这些日历 ID?

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?

推荐答案

使用 谷歌日历 API V3.我从这篇文章中找到的想法.假期可以从谷歌的默认假期日历中获取.

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.

默认假日日历的ID列表可以在这里找到,支持40个国家.

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();
}

这篇关于如何获得所选国家的国定假日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆