diff options
Diffstat (limited to 'src/com/android/datetimepicker/Utils.java')
-rw-r--r-- | src/com/android/datetimepicker/Utils.java | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/src/com/android/datetimepicker/Utils.java b/src/com/android/datetimepicker/Utils.java index 8772fb8..9bc5952 100644 --- a/src/com/android/datetimepicker/Utils.java +++ b/src/com/android/datetimepicker/Utils.java @@ -17,9 +17,79 @@ package com.android.datetimepicker; import android.os.Build; +import android.text.format.Time; +import java.util.Calendar; + +/** + * Utility helper functions for time and date pickers. + */ public class Utils { + + public static final int MONDAY_BEFORE_JULIAN_EPOCH = Time.EPOCH_JULIAN_DAY - 3; + + static final String SHARED_PREFS_NAME = "com.android.calendar_preferences"; + public static boolean isJellybeanOrLater() { - return Build.VERSION.SDK_INT >= 16; + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; + } + + public static int getDaysInMonth(int month, int year) { + switch (month) { + case Calendar.JANUARY: + case Calendar.MARCH: + case Calendar.MAY: + case Calendar.JULY: + case Calendar.AUGUST: + case Calendar.OCTOBER: + case Calendar.DECEMBER: + return 31; + case Calendar.APRIL: + case Calendar.JUNE: + case Calendar.SEPTEMBER: + case Calendar.NOVEMBER: + return 30; + case Calendar.FEBRUARY: + return (year % 4 == 0) ? 29 : 28; + default: + throw new IllegalArgumentException("Invalid Month"); + } + } + + /** + * Takes a number of weeks since the epoch and calculates the Julian day of + * the Monday for that week. + * + * This assumes that the week containing the {@link Time#EPOCH_JULIAN_DAY} + * is considered week 0. It returns the Julian day for the Monday + * {@code week} weeks after the Monday of the week containing the epoch. + * + * @param week Number of weeks since the epoch + * @return The julian day for the Monday of the given week since the epoch + */ + public static int getJulianMondayFromWeeksSinceEpoch(int week) { + return MONDAY_BEFORE_JULIAN_EPOCH + week * 7; + } + + /** + * Returns the week since {@link Time#EPOCH_JULIAN_DAY} (Jan 1, 1970) + * adjusted for first day of week. + * + * This takes a julian day and the week start day and calculates which + * week since {@link Time#EPOCH_JULIAN_DAY} that day occurs in, starting + * at 0. *Do not* use this to compute the ISO week number for the year. + * + * @param julianDay The julian day to calculate the week number for + * @param firstDayOfWeek Which week day is the first day of the week, + * see {@link Time#SUNDAY} + * @return Weeks since the epoch + */ + public static int getWeeksSinceEpochFromJulianDay(int julianDay, int firstDayOfWeek) { + int diff = Time.THURSDAY - firstDayOfWeek; + if (diff < 0) { + diff += 7; + } + int refDay = Time.EPOCH_JULIAN_DAY - diff; + return (julianDay - refDay) / 7; } } |