Java Code Examples for android.text.format.DateFormat#getBestDateTimePattern()

The following examples show how to use android.text.format.DateFormat#getBestDateTimePattern() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimePickerSpinnerDelegate.java    From ticdesign with Apache License 2.0 6 votes vote down vote up
private void getHourFormatData() {
    final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mCurrentLocale,
            (mIs24HourView) ? "Hm" : "hm");
    final int lengthPattern = bestDateTimePattern.length();
    mHourWithTwoDigit = false;
    char hourFormat = '\0';
    // Check if the returned pattern is single or double 'H', 'h', 'K', 'k'. We also save
    // the hour format that we found.
    for (int i = 0; i < lengthPattern; i++) {
        final char c = bestDateTimePattern.charAt(i);
        if (c == 'H' || c == 'h' || c == 'K' || c == 'k') {
            mHourFormat = c;
            if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) {
                mHourWithTwoDigit = true;
            }
            break;
        }
    }
}
 
Example 2
Source File: SublimeTimePicker.java    From SublimePicker with Apache License 2.0 6 votes vote down vote up
private void updateHeaderAmPm() {
    if (mIs24HourView) {
        mAmPmLayout.setVisibility(View.GONE);
    } else {
        // Ensure that AM/PM layout is in the correct position.
        String timePattern;

        // Available on API >= 18
        if (SUtils.isApi_18_OrHigher()) {
            timePattern = DateFormat.getBestDateTimePattern(mCurrentLocale, "hm");
        } else {
            timePattern = DateTimePatternHelper.getBestDateTimePattern(mCurrentLocale,
                    DateTimePatternHelper.PATTERN_hm);
        }

        final boolean isAmPmAtStart = timePattern.startsWith("a");
        setAmPmAtStart(isAmPmAtStart);

        updateAmPmLabelStates(mInitialHourOfDay < 12 ? AM : PM);
    }
}
 
Example 3
Source File: TimePickerSpinnerDelegate.java    From ticdesign with Apache License 2.0 6 votes vote down vote up
/**
 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
 *
 * See http://unicode.org/cldr/trac/browser/trunk/common/main
 *
 * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the
 * separator as the character which is just after the hour marker in the returned pattern.
 */
private void setDividerText(TextView divider) {
    final String skeleton = (mIs24HourView) ? "Hm" : "hm";
    final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mCurrentLocale,
            skeleton);
    final String separatorText;
    int hourIndex = bestDateTimePattern.lastIndexOf('H');
    if (hourIndex == -1) {
        hourIndex = bestDateTimePattern.lastIndexOf('h');
    }
    if (hourIndex == -1) {
        // Default case
        separatorText = ":";
    } else {
        int minuteIndex = bestDateTimePattern.indexOf('m', hourIndex + 1);
        if  (minuteIndex == -1) {
            separatorText = Character.toString(bestDateTimePattern.charAt(hourIndex + 1));
        } else {
            separatorText = bestDateTimePattern.substring(hourIndex + 1, minuteIndex);
        }
    }
    divider.setText(separatorText);
}
 
Example 4
Source File: SimpleMonthView.java    From SublimePicker with Apache License 2.0 5 votes vote down vote up
private void init() {
    mContext = getContext();

    mTouchSlopSquared = ViewConfiguration.get(mContext).getScaledTouchSlop()
            * ViewConfiguration.get(mContext).getScaledTouchSlop();

    final Resources res = mContext.getResources();
    mDesiredMonthHeight = res.getDimensionPixelSize(R.dimen.sp_date_picker_month_height);
    mDesiredDayOfWeekHeight = res.getDimensionPixelSize(R.dimen.sp_date_picker_day_of_week_height);
    mDesiredDayHeight = res.getDimensionPixelSize(R.dimen.sp_date_picker_day_height);
    mDesiredCellWidth = res.getDimensionPixelSize(R.dimen.sp_date_picker_day_width);
    mDesiredDaySelectorRadius = res.getDimensionPixelSize(
            R.dimen.sp_date_picker_day_selector_radius);
    mPaddingRangeIndicator = res.getDimensionPixelSize(R.dimen.sp_month_view_range_padding);

    // Set up accessibility components.
    mTouchHelper = new MonthViewTouchHelper(this);

    ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
    ViewCompat.setImportantForAccessibility(this,
            ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);

    final Locale locale = res.getConfiguration().locale;

    String titleFormat;

    if (SUtils.isApi_18_OrHigher()) {
        titleFormat = DateFormat.getBestDateTimePattern(locale, DEFAULT_TITLE_FORMAT);
    } else {
        titleFormat = DateTimePatternHelper.getBestDateTimePattern(locale,
                DateTimePatternHelper.PATTERN_MMMMy);
    }

    mTitleFormatter = new SimpleDateFormat(titleFormat, locale);
    mDayOfWeekFormatter = new SimpleDateFormat(DAY_OF_WEEK_FORMAT, locale);
    mDayFormatter = NumberFormat.getIntegerInstance(locale);

    initPaints(res);
}
 
Example 5
Source File: DateTimeFormatUtils.java    From BottomSheetPickers with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@code true} if the AM or PM label is written before the time
 *         in the user's locale.
 */
static boolean isAmPmWrittenBeforeTime(@NonNull Context context) {
    if (Build.VERSION.SDK_INT >= 18) {
        final String dateTimePattern = DateFormat.getBestDateTimePattern(
                getPrimaryLocale(context), "hm");
        return dateTimePattern.startsWith("a");
    } else {
        // Format a dummy time string in 12-hour time, then check if the string
        // starts with a non-digit character. This should be the AM/PM label.
        final String formatted12HourTime = DateUtils.formatDateTime(context,
                System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR);
        return !Character.isDigit(formatted12HourTime.charAt(0));
    }
}
 
Example 6
Source File: DateView.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private SimpleDateFormat getBestDateTimePattern(String dateFormat) {
    if (Device.hasJellyBeanMR2Api()) {
        final Locale l = Locale.getDefault();
        final String fmt = DateFormat.getBestDateTimePattern(l, dateFormat);
        return new SimpleDateFormat(fmt, l);
    } else {
        return new SimpleDateFormat(dateFormat);
    }
}
 
Example 7
Source File: DateTimeFormatUtils.java    From NumberPadTimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@code true} if the AM or PM label is written before the time
 *         in the user's locale.
 */
static boolean isAmPmWrittenBeforeTime(@NonNull Context context) {
    if (Build.VERSION.SDK_INT >= 18) {
        final String dateTimePattern = DateFormat.getBestDateTimePattern(
                getPrimaryLocale(context), "hm");
        return dateTimePattern.startsWith("a");
    } else {
        // Format a dummy time string in 12-hour time, then check if the string
        // starts with a non-digit character. This should be the AM/PM label.
        final String formatted12HourTime = DateUtils.formatDateTime(context,
                System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR);
        return !Character.isDigit(formatted12HourTime.charAt(0));
    }
}
 
Example 8
Source File: MonthView.java    From AssistantBySDK with Apache License 2.0 5 votes vote down vote up
@NonNull
private String getMonthAndYearString() {
    Locale locale = Locale.getDefault();
    String pattern = "MMMM yyyy";

    if(Build.VERSION.SDK_INT < 18) pattern = getContext().getResources().getString(R.string.mdtp_date_v1_monthyear);
    else pattern = DateFormat.getBestDateTimePattern(locale, pattern);

    SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
    formatter.applyLocalizedPattern(pattern);
    mStringBuilder.setLength(0);
    return formatter.format(mCalendar.getTime());
}
 
Example 9
Source File: DateUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
@NonNull
public static String getHourMinute(Date date) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), FORMAT_KKMM);
        return new SimpleDateFormat(pattern).format(date);
    } else {
        return String.valueOf(DateFormat.format(FORMAT_KKMM, date));
    }
}
 
Example 10
Source File: DateUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
@NonNull
public static String getLongFormatDate(Date date, Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), FORMAT_YYYYMMDDKKMM);
        return new SimpleDateFormat(pattern).format(date);
    } else {
        java.text.DateFormat dayOfWeekFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG);
        java.text.DateFormat shortTimeFormat = java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT);
        dayOfWeekFormat.setTimeZone(LocaleUtil.getDisplayTimeZone(context));
        shortTimeFormat.setTimeZone(LocaleUtil.getDisplayTimeZone(context));
        return dayOfWeekFormat.format(date) + " " + shortTimeFormat.format(date);
    }
}
 
Example 11
Source File: DatePickerSpinnerDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Reorders the spinners according to the date format that is
 * explicitly set by the user and if no such is set fall back
 * to the current locale's default format.
 */
private void reorderSpinners() {
    mSpinners.removeAllViews();
    // We use numeric spinners for year and day, but textual months. Ask icu4c what
    // order the user's locale uses for that combination. http://b/7207103.
    String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "yyyyMMMdd");
    char[] order = ICU.getDateFormatOrder(pattern);
    final int spinnerCount = order.length;
    for (int i = 0; i < spinnerCount; i++) {
        switch (order[i]) {
            case 'd':
                mSpinners.addView(mDaySpinner);
                setImeOptions(mDaySpinner, spinnerCount, i);
                break;
            case 'M':
                mSpinners.addView(mMonthSpinner);
                setImeOptions(mMonthSpinner, spinnerCount, i);
                break;
            case 'y':
                mSpinners.addView(mYearSpinner);
                setImeOptions(mYearSpinner, spinnerCount, i);
                break;
            default:
                throw new IllegalArgumentException(Arrays.toString(order));
        }
    }
}
 
Example 12
Source File: TimePickerClockDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void updateHeaderAmPm() {
    if (mIs24Hour) {
        mAmPmLayout.setVisibility(View.GONE);
    } else {
        // Find the location of AM/PM based on locale information.
        final String dateTimePattern = DateFormat.getBestDateTimePattern(mLocale, "hm");
        final boolean isAmPmAtStart = dateTimePattern.startsWith("a");
        setAmPmStart(isAmPmAtStart);
        updateAmPmLabelStates(mCurrentHour < 12 ? AM : PM);
    }
}
 
Example 13
Source File: TimePickerSpinnerDelegate.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private boolean isAmPmAtStart() {
    final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale,
            "hm" /* skeleton */);

    return bestDateTimePattern.startsWith("a");
}
 
Example 14
Source File: SimpleMonthView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void updateMonthYearLabel() {
    final String format = DateFormat.getBestDateTimePattern(mLocale, MONTH_YEAR_FORMAT);
    final SimpleDateFormat formatter = new SimpleDateFormat(format, mLocale);
    formatter.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
    mMonthYearLabel = formatter.format(mCalendar.getTime());
}
 
Example 15
Source File: FileDateUtil.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static String getDateFormat(Locale locale) {
    return DateFormat.getBestDateTimePattern(locale, "MM/dd/yyyy hh:mm:ss aa");
}
 
Example 16
Source File: TwoFieldDatePicker.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Reorder the date picker spinners to match the order suggested by the locale.
 * Assumes that the order of month and year in the locale is also the right order
 * for the spinner columns.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void reorderSpinners() {
    boolean posInserted = false;
    boolean yearInserted = false;

    LinearLayout pickers = (LinearLayout) findViewById(R.id.pickers);

    pickers.removeView(mPositionInYearSpinner);
    pickers.removeView(mYearSpinner);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        // logic duplicated from android.widget.DatePicker
        String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "yyyyMMMdd");

        for (int i = 0; i < pattern.length(); ++i) {
            char ch = pattern.charAt(i);
            if (ch == '\'') {
                i = pattern.indexOf('\'', i + 1);
                if (i == -1) {
                    throw new IllegalArgumentException("Bad quoting in " + pattern);
                }
            } else if ((ch == 'M' || ch == 'L') && !posInserted) {
                pickers.addView(mPositionInYearSpinner);
                posInserted = true;
            } else if (ch == 'y' && !yearInserted) {
                pickers.addView(mYearSpinner);
                yearInserted = true;
            }
        }
    } else {
        // This method was used to order android.widget.DatePicker
        // fields in JB prior to the availability of
        // getBestDateTimePattern.
        char[] order = DateFormat.getDateFormatOrder(getContext());
        for (int i = 0; i < order.length; ++i) {
            if (order[i] == 'M') {
                pickers.addView(mPositionInYearSpinner);
                posInserted = true;
            } else if (order[i] == 'y') {
                pickers.addView(mYearSpinner);
                yearInserted = true;
            }
        }
    }

    if (!posInserted) pickers.addView(mPositionInYearSpinner);
    if (!yearInserted) pickers.addView(mYearSpinner);
}
 
Example 17
Source File: SublimeTimePicker.java    From SublimePicker with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void updateHeaderHour(int value, boolean announce) {
    String timePattern;

    if (SUtils.isApi_18_OrHigher()) {
        timePattern = DateFormat.getBestDateTimePattern(mCurrentLocale,
                (mIs24HourView) ? "Hm" : "hm");
    } else {
        timePattern = DateTimePatternHelper.getBestDateTimePattern(mCurrentLocale,
                (mIs24HourView) ? DateTimePatternHelper.PATTERN_Hm
                        : DateTimePatternHelper.PATTERN_hm);
    }

    final int lengthPattern = timePattern.length();
    boolean hourWithTwoDigit = false;
    char hourFormat = '\0';
    // Check if the returned pattern is single or double 'H', 'h', 'K', 'k'. We also save
    // the hour format that we found.
    for (int i = 0; i < lengthPattern; i++) {
        final char c = timePattern.charAt(i);
        if (c == 'H' || c == 'h' || c == 'K' || c == 'k') {
            hourFormat = c;
            if (i + 1 < lengthPattern && c == timePattern.charAt(i + 1)) {
                hourWithTwoDigit = true;
            }
            break;
        }
    }
    final String format;
    if (hourWithTwoDigit) {
        format = "%02d";
    } else {
        format = "%d";
    }
    if (mIs24HourView) {
        // 'k' means 1-24 hour
        if (hourFormat == 'k' && value == 0) {
            value = 24;
        }
    } else {
        // 'K' means 0-11 hour
        value = modulo12(value, hourFormat == 'K');
    }
    CharSequence text = String.format(format, value);
    mHourView.setText(text);
    if (announce) {
        tryAnnounceForAccessibility(text, true);
    }
}
 
Example 18
Source File: DateUtils.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private static String getLocalizedPattern(String template, Locale locale) {
  return DateFormat.getBestDateTimePattern(locale, template);
}
 
Example 19
Source File: TimePickerSpinnerDelegate.java    From ticdesign with Apache License 2.0 4 votes vote down vote up
private boolean isAmPmAtStart() {
    final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mCurrentLocale,
            "hm" /* skeleton */);

    return bestDateTimePattern.startsWith("a");
}
 
Example 20
Source File: TimePickerClockDelegate.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
 *
 * See http://unicode.org/cldr/trac/browser/trunk/common/main
 *
 * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the
 * separator as the character which is just after the hour marker in the returned pattern.
 */
private void updateHeaderSeparator() {
    final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale,
            (mIs24Hour) ? "Hm" : "hm");
    final String separatorText = getHourMinSeparatorFromPattern(bestDateTimePattern);
    mSeparatorView.setText(separatorText);
    mTextInputPickerView.updateSeparator(separatorText);
}