Example usage for android.text.format DateFormat getBestDateTimePattern

List of usage examples for android.text.format DateFormat getBestDateTimePattern

Introduction

In this page you can find the example usage for android.text.format DateFormat getBestDateTimePattern.

Prototype

public static String getBestDateTimePattern(Locale locale, String skeleton) 

Source Link

Document

Returns the best possible localized form of the given skeleton for the given locale.

Usage

From source file:com.android.deskclock.Utils.java

/**
 * @param context - context used to get time format string resource
 * @param showAmPm - include the am/pm string if true
 * @return format string for 12 hours mode time
 *//*ww w  .ja  v  a 2 s  . c  o  m*/
public static CharSequence get12ModeFormat(Context context, boolean showAmPm) {
    String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "hma");
    if (!showAmPm) {
        pattern = pattern.replaceAll("a", "").trim();
    }

    // Replace spaces with "Hair Space"
    pattern = pattern.replaceAll(" ", "\u200A");
    // Build a spannable so that the am/pm will be formatted
    int amPmPos = pattern.indexOf('a');
    if (amPmPos == -1) {
        return pattern;
    }

    final Resources resources = context.getResources();
    final float amPmProportion = resources.getFraction(R.fraction.ampm_font_size_scale, 1, 1);
    final Spannable sp = new SpannableString(pattern);
    sp.setSpan(new RelativeSizeSpan(amPmProportion), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sp.setSpan(new StyleSpan(Typeface.NORMAL), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sp.setSpan(new TypefaceSpan("sans-serif"), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Make the font smaller for locales with long am/pm strings.
    if (Utils.isAmPmStringLong()) {
        final float proportion = resources.getFraction(R.fraction.reduced_clock_font_size_scale, 1, 1);
        sp.setSpan(new RelativeSizeSpan(proportion), 0, pattern.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return sp;
}

From source file:com.android.deskclock.Utils.java

public static CharSequence get24ModeFormat() {
    return DateFormat.getBestDateTimePattern(Locale.getDefault(), "Hm");
}

From source file:com.appeaser.sublimepickerlibrary.timepicker.SublimeTimePicker.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void updateHeaderHour(int value, boolean announce) {
    String timePattern;/* w w w . j  a  v a2s.c  o  m*/

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

From source file:com.appeaser.sublimepickerlibrary.timepicker.SublimeTimePicker.java

/**
 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":".
 * <p/>//w w w .j a  v a  2  s  .  c  o m
 * See http://unicode.org/cldr/trac/browser/trunk/common/main
 * <p/>
 * 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() {
    String timePattern;

    // Available on API >= 18
    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 String separatorText;
    // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats
    final char[] hourFormats = { 'H', 'h', 'K', 'k' };
    int hIndex = lastIndexOfAny(timePattern, hourFormats);
    if (hIndex == -1) {
        // Default case
        separatorText = ":";
    } else {
        separatorText = Character.toString(timePattern.charAt(hIndex + 1));
    }
    mSeparatorView.setText(separatorText);
}

From source file:io.doist.datetimepicker.time.TimePickerClockDelegate.java

private static String getBestTimePattern(Locale locale, boolean is24Hour) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return DateFormat.getBestDateTimePattern(locale, (is24Hour) ? "Hm" : "hm");
    } else {//www .jav  a  2 s  . com
        return DateTimeUtilsCompat.getBestTimePattern(locale, is24Hour);
    }
}