Does the Android API support APIs for defining weekend days that locales know?

I want to provide some styles for an application that knows about weekdays and weekends, but I need it to be local. I know Calendar#getFirstDayOfWeek , but this only returns the first day of the week, not the first day of the work week.

For example, in English (Canada) Calendar#getFirstDayOfWeek returns Calendar.SUNDAY , which is the first day of our week. Our work week starts with Calendar.MONDAY , and our weekends are Calendar.SATURDAY and Calendar.SUNDAY .

In English (UK), Calendar#getFirstDayOfWeek returns Calendar.MONDAY , and the first day of their work week is Monday. Their weekends are Calendar.SATURDAY and Calendar.SUNDAY .

Things are becoming complicated for locales such as Hebrew. The first day of their week is Calendar.SUNDAY , but instead of the first day of their working week, Calendar.MONDAY , it is Calendar.SUNDAY . Their weekends are Calendar.FRIDAY and Calendar.SATURDAY . Egypt is the same.

There is only one day off in Iran, Calendar.FRIDAY .

Is there any method in Android for determining weekend days? We have some logic that covers most Western cases, but it is fragile, and a language-compatible platform API would be ideal, but I don't know about that.

Please note that I am not asking for a recommendation on any β€œbetter” library or something like that. I ask if there is any such local data on the Android platform at all, and if so, what API calls exist to access that data.

+5
source share
1 answer

The recently released Snapshot and Fence APIs have appeared in the Awareness API for your use case for the language-oriented API for time.

Please take a look at Awareness.SnapshotApi.getTimeIntervals , which returns TimeIntervalsResult. You can call the getTimeIntervals () method to return an object that has information about the properties of the current time in the current locale (in particular, the weekends and holidays that you requested).

For example, the following code snippet checks if this is a day off in the device locale when the api calls the snapshot file.

 Awareness.SnapshotApi.getTimeIntervalsmGoogleApiClient) .setResultCallback(new ResultCallback<TimeIntervalsResult>() { @Override public void onResult(@NonNull TimeIntervalsResult result) { if (!result.getStatus().isSuccess()) { Log.e(TAG, "Could not get the time intervals"); return; } if (result.getTimeIntervals().hasTimeInterval( TimeFence.TIME_INTERVAL_WEEKEND)) { // do your custom action, this means it is weekend at the // device locale at this time } } 

If you want your application to receive a callback next weekend or holiday in the device locale, you can use the new TimeFence Api inTimeInterval () . For example, borrowing a piece of code from the documentation of the developer,

 // Create TimeFence AwarenessFence weekendHolidayFence = Awareness.or( TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_WEEKEND), TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_HOLIDAY)) Awareness.FenceApi.updateFences( mGoogleApiClient, new FenceUpdateRequest.Builder() .addFence("fenceKey", weekendHolidayFence, myPendingIntent) .build()) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(@NonNull Status status) { if (status.isSuccess()) { Log.i(TAG, "Fence was successfully registered."); } else { Log.e(TAG, "Fence could not be registered: " + status); } } }); 
+1
source

All Articles