下面列出了android.text.format.DateFormat#is24HourFormat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.date:
DatePickerDialog dpd = new DatePickerDialog(this, this, cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dpd.show();
break;
case R.id.time:
TimePickerDialog dtp = new TimePickerDialog(this, this,
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
DateFormat.is24HourFormat(this));
dtp.show();
break;
default:
break;
}
}
@Override
public void onTimeSet(RadialPickerLayout radialPickerLayout, int h, int min) {
if (nightTimeStart[0] >= 25) {
nightTimeStart = new int[]{h, min};
boolean twentyFourHourStyle = DateFormat.is24HourFormat(getActivity());
TimePickerDialog tpd = TimePickerDialog
.newInstance(this, 6, 0, twentyFourHourStyle);
tpd.setOnCancelListener(this);
tpd.setTitle(getActivity().getString(R.string.nighttime_end_dialog_title));
tpd.show(getFragmentManager(), NIGHT_END_TIME_DIALOG_TAG);
} else {
SharedPreferences current = getActivity().getSharedPreferences("night_time", Context.MODE_PRIVATE);
current.edit().putInt("startHour", nightTimeStart[0])
.putInt("startMinute", nightTimeStart[1])
.putInt("endHour", h)
.putInt("endMinute", min)
.apply();
nightTimeStart[0] = 42;
}
}
@Override
protected int getIndicatorWidth() {
int width = 62;
if(includeYear) {
if(includeDay) {
width += 14;
}
width += 56;
}
if(includeMonth) {
width += 43;
}
if(includeDay) {
width += 28;
}
if(includeTime) {
if(DateFormat.is24HourFormat(context)) {
width += 70;
} else {
width += 115;
}
}
return width;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
timePicker = view.findViewById(R.id.timePicker);
calendar = Calendar.getInstance();
Long timeInMillis = null;
DialogPreference preference = getPreference();
if (preference instanceof TimePreference) {
TimePreference timePreference = (TimePreference) preference;
timeInMillis = timePreference.getTimeInMillis();
}
if (timeInMillis != null) {
calendar.setTimeInMillis(timeInMillis);
boolean is24hour = DateFormat.is24HourFormat(getContext());
timePicker.setIs24HourView(is24hour);
timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
}
}
void showPickTimeDialog (final TimePickerDialog.OnTimeSetListener listener)
{
final DatePickerDialog.OnDateSetListener dateListener = (DatePickerDialog.OnDateSetListener) listener;
final Calendar calendar = Calendar.getInstance();
TimePickerDialog d = new TimePickerDialog(MainActivity.mainActivity, listener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), DateFormat.is24HourFormat(MainActivity.mainActivity));
//
d.setCancelable(false);
d.setCanceledOnTouchOutside(false);
//
d.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Back", new DialogInterface.OnClickListener()
{
@Override
public void onClick (DialogInterface dialog, int which)
{
if (screenType == ScreenType.SET_DATE_SCREEN)
{
showPickDateDialog(dateListener, null);
}
else
{
MainActivity.log("OBSetupMenu:showPickTimeDialog:cancelled!");
}
}
});
//
LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setCustomTitle(linearLayout);
//
d.show();
}
static public String timeFormatString(Context context, int hour, int minute) {
SimpleDateFormat timeFormat24 = new SimpleDateFormat("HH:mm");
String selected = hour+":" + ((minute<10)?"0":"") + minute;
if (!DateFormat.is24HourFormat(context)) {
try {
Date date = timeFormat24.parse(selected);
SimpleDateFormat timeFormat12 = new SimpleDateFormat("hh:mm aa");
return timeFormat12.format(date);
} catch (final ParseException e) {
e.printStackTrace();
}
}
return selected;
}
protected void onResume()
{
super.onResume();
checkOnceAlarmIfExpired();
new DateFormat();
q = DateFormat.is24HourFormat(this);
if (i != null)
{
i.notifyDataSetChanged();
}
UmengAnalytics.startPage("PageAlarm");
UmengAnalytics.startSession(this);
}
public String timeFormatString(int Hour, int Minute) {
SimpleDateFormat timeFormat24 = new SimpleDateFormat("HH:mm");
String selected = Hour+":"+Minute;
if (!DateFormat.is24HourFormat(mContext)) {
try {
Date date = timeFormat24.parse(selected);
SimpleDateFormat timeFormat12 = new SimpleDateFormat("hh:mm aa");
return timeFormat12.format(date);
} catch (final ParseException e) {
e.printStackTrace();
}
}
return selected;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
public static String toStringReadableTime(Calendar calendar, Context context) {
if (DateFormat.is24HourFormat(context)) {
return READABLE_TIME_24_FORMAT.format(calendar.getTime());
} else {
return READABLE_TIME_FORMAT.format(calendar.getTime());
}
}
private boolean is24HourFormat(java.text.DateFormat timeFormat) {
String pattern;
try {
pattern = ((SimpleDateFormat) timeFormat).toLocalizedPattern();
} catch (ClassCastException e) {
// we cannot get the pattern, use the default setting for out context:
return DateFormat.is24HourFormat(getContext());
}
// if pattern does not contain the 12 hour formats, we return true (regardless of any 'a' (am/pm) modifier)
return !(pattern.contains("h") || pattern.contains("K"));
}
public PrayerListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.view_default_prayer, this, true);
ButterKnife.bind(this);
mDateFormat = DateFormat.is24HourFormat(context) ? FORMAT_24 : FORMAT_12;
}
private void initialize(Context context, MapboxNavigation mapboxNavigation) {
this.mapboxNavigation = mapboxNavigation;
etaFormat = context.getString(R.string.eta_format);
initializeDistanceFormatter(context, mapboxNavigation);
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
isTwentyFourHourFormat = DateFormat.is24HourFormat(context);
createNotificationChannel(context);
buildNotification(context);
registerReceiver(context);
}
public boolean retrive24Format() { // return true if changed
boolean old = is24hours;
is24hours = DateFormat.is24HourFormat(mTimeView.getContext());
return old != is24hours;
}
@Before
public void setup() {
mLocaleModel = new LocaleModel(mActivityTestRule.getActivity());
mInitiallyIn24HourMode = DateFormat.is24HourFormat(mActivityTestRule.getActivity());
}
private String getFormattedDate(Date date) {
String f = DateFormat.is24HourFormat(this) ? FORMAT_24 : FORMAT_12;
return DateFormat.format(f, date).toString();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new RCTTimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
public static boolean is12Hour(Context context) {
return !DateFormat.is24HourFormat(context);
}
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
switch (pref.getKey()) {
case "pref_theme":
findPreference("pref_theme").setSummary(themes[Integer.valueOf((String) newValue)]);
break;
case "pref_notifications":
if (newValue.equals("0")) {
((NotificationManager) getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
} else
broadcast();
break;
case "pref_night_mode":
if ((Boolean) newValue) {
boolean twentyFourHourStyle = DateFormat.is24HourFormat(getActivity());
TimePickerDialog tpd = TimePickerDialog
.newInstance(this, 21, 0, twentyFourHourStyle);
tpd.setOnCancelListener(this);
tpd.setTitle(getActivity().getString(R.string.nighttime_start_dialog_title));
tpd.show(getFragmentManager(), NIGHT_START_TIME_DIALOG_TAG);
} else {
this.onCancel(null);
}
break;
case "pref_overlay":
findPreference("pref_overlay_behavior").setEnabled((Boolean) newValue);
findPreference("pref_notifications").setEnabled(!((Boolean) newValue));
findPreference("pref_notifications").setSummary((Boolean) newValue ?
R.string.pref_notification_sum_disabled : R.string.pref_notifications_sum);
if ((Boolean) newValue) {
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putBoolean(pref.getKey(), (Boolean) newValue).apply();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(getActivity())) {
broadcast();
} else {
final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getActivity().getPackageName()));
getActivity().startActivity(intent);
}
PermissionsChecker.displayMIUIPopupPermission(getActivity());
} else {
LyricsOverlayService.removeCustomFloatingView(getActivity());
}
break;
case "pref_overlay_behavior":
LyricsOverlayService.removeCustomFloatingView(getActivity());
break;
}
return true;
}
/**
* Indicates whether the system is currently using the 24-hour mode.
*
* When the system is in 24-hour mode, this view will use the pattern
* returned by {@link #getFormat24Hour()}. In 12-hour mode, the pattern
* returned by {@link #getFormat12Hour()} is used instead.
*
* If either one of the formats is null, the other format is used. If
* both formats are null, the default formats for the current locale are used.
*
* @return true if time should be displayed in 24-hour format, false if it
* should be displayed in 12-hour format.
*
* @see #setFormat12Hour(CharSequence)
* @see #getFormat12Hour()
* @see #setFormat24Hour(CharSequence)
* @see #getFormat24Hour()
*/
public boolean is24HourModeEnabled() {
if (mShowCurrentUserTime) {
return DateFormat.is24HourFormat(getContext(), ActivityManager.getCurrentUser());
} else {
return DateFormat.is24HourFormat(getContext());
}
}