Example usage for android.text.format DateFormat is24HourFormat

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

Introduction

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

Prototype

public static boolean is24HourFormat(Context context) 

Source Link

Document

Returns true if times should be formatted as 24 hour times, false if times should be formatted as 12 hour (AM/PM) times.

Usage

From source file:com.sean.takeastand.util.Utils.java

public static String getFormattedTimeString(String time, Context context) {
    if (!DateFormat.is24HourFormat(context)) {
        int hour = readHourFromString(time, context);
        String minutes = correctMinuteFormat(Integer.toString((readMinutesFromString(time, context))));
        if (hour >= 12) {
            if (hour == 12) {
                //Noon
                return "12:" + minutes + " " + context.getString(R.string.PM);
            } else {
                //Afternoon
                return Integer.toString(hour - 12) + ":" + minutes + " " + context.getString(R.string.PM);
            }/*from   w w w.j a  v a  2 s  . c  o m*/
        } else {
            if (hour == 0) {
                //12 am
                return "12:" + minutes + " " + context.getString(R.string.AM);
            } else {
                //Morning, string is ready to go after adding am
                return time + " " + context.getString(R.string.AM);
            }
        }
    } else {
        //Device clock is 24-hour
        return time;
    }
}

From source file:com.geecko.QuickLyric.fragment.SettingsFragment.java

@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
    switch (pref.getKey()) {
    case "pref_theme":
        if (!newValue.equals(pref.getSharedPreferences().getString("pref_theme", "0"))) {
            getActivity().finish();//from  w  ww .  j  av a 2 s .c o  m
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction("android.intent.action.MAIN");
            startActivity(intent);
        }
        return true;
    case "pref_opendyslexic":
        if (!newValue.equals(pref.getSharedPreferences().getBoolean("pref_opendyslexic", false))) {
            getActivity().finish();
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction("android.intent.action.MAIN");
            startActivity(intent);
        }
        return true;
    case "pref_notifications":
        if (newValue.equals("0")) {
            ((NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
        } else {
            SharedPreferences current = getActivity().getSharedPreferences("current_music",
                    Context.MODE_PRIVATE);
            Intent intent = new Intent();
            intent.setAction("com.geecko.QuickLyric.SHOW_NOTIFICATION");
            intent.putExtra("artist", current.getString("artist", "Michael Jackson"));
            intent.putExtra("track", current.getString("track", "Bad"));
            intent.putExtra("playing", current.getBoolean("playing", false));
            getActivity().sendBroadcast(intent);
        }
        return true;
    case "pref_night_mode":
        if ((Boolean) newValue) {
            boolean twentyFourHourStyle = DateFormat.is24HourFormat(getActivity());
            TimePickerDialog tpd = TimePickerDialog.newInstance(this, 21, 0, twentyFourHourStyle);
            tpd.setCancelable(false);
            tpd.show(getFragmentManager(), NIGHT_START_TIME_DIALOG_TAG);
        } else if (NightTimeVerifier.check(getActivity())) {
            getActivity().finish();
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction("android.intent.action.MAIN");
            startActivity(intent);
        }
        return true;
    default:
        return true;
    }
}

From source file:net.xisberto.work_schedule.alarm.AlarmMessageActivity.java

private void snoozeAlarm() {
    settings = Settings.getInstance(getApplicationContext());

    period.addTime(settings.getCalendar(R.string.key_snooze_increment));
    period.enabled = true;/*from w  w w.  jav  a2s  .  com*/
    period.setAlarm(this, true);
    period.persist(this);

    Toast.makeText(this, getResources().getString(R.string.snooze_set_to) + " "
            + period.formatTime(DateFormat.is24HourFormat(this)), Toast.LENGTH_SHORT).show();
    stopSoundVibrator();
    finish();
}

From source file:ticwear.design.app.DatetimePickerDialog.java

/**
 * @param context The context the dialog is to run in.
 * @param theme the theme to apply to this dialog
 * @param pageFlag Witch page will show.
 * @param defaultCalendar The initial datetime of the dialog.
 *///from  w ww  . j a v a 2  s. c  o  m
public DatetimePickerDialog(Context context, @StyleRes int theme, int pageFlag, Calendar defaultCalendar) {
    super(context, resolveDialogTheme(context, theme));

    // Use getContext to use wrapper context.
    context = getContext();

    mCurrentCalendar = defaultCalendar;
    mCurrentCalendar.clear(Calendar.SECOND);

    boolean hasDateView = (pageFlag & PAGE_FLAG_DATE) == PAGE_FLAG_DATE;
    boolean hasTimeView = (pageFlag & PAGE_FLAG_TIME) == PAGE_FLAG_TIME;

    int year = defaultCalendar.get(Calendar.YEAR);
    int month = defaultCalendar.get(Calendar.MONTH);
    int day = defaultCalendar.get(Calendar.DAY_OF_MONTH);
    int hour = defaultCalendar.get(Calendar.HOUR_OF_DAY);
    int minute = defaultCalendar.get(Calendar.MINUTE);

    ValidationCallback validationCallback = new ValidationCallback() {
        @Override
        public void onValidationChanged(boolean valid) {
            final Button positive = getButton(BUTTON_POSITIVE);
            if (positive != null) {
                positive.setEnabled(valid);
            }
        }
    };

    final Context themeContext = getContext();
    final LayoutInflater inflater = LayoutInflater.from(themeContext);
    TicklableFrameLayout container = (TicklableFrameLayout) inflater.inflate(R.layout.dialog_datetime_picker,
            null);

    container.setSidePanelEventDispatcher(this);

    mViewPager = (ViewPager) container.findViewById(R.id.tic_datetimeContainer);

    List<View> pages = new ArrayList<>(Integer.bitCount(pageFlag));
    if (hasDateView) {
        mDatePickerViewHolder = new DatePickerViewHolder(context);
        DatePicker dateView = mDatePickerViewHolder.init(mViewPager, year, month, day, this,
                validationCallback);
        dateView.setMultiPickerClient(this);
        dateView.setTag(R.id.title_template, R.string.date_picker_dialog_title);
        pages.add(dateView);
    }
    if (hasTimeView) {
        mTimePickerViewHolder = new TimePickerViewHolder(context);
        TimePicker timeView = mTimePickerViewHolder.init(mViewPager, hour, minute,
                DateFormat.is24HourFormat(context), this, validationCallback);
        timeView.setMultiPickerClient(this);
        timeView.setTag(R.id.title_template, R.string.time_picker_dialog_title);
        pages.add(timeView);
    }
    mPagerAdapter = new PickerPagerAdapter(pages);
    mViewPager.setAdapter(mPagerAdapter);

    mPageIndicator = (CirclePageIndicator) container.findViewById(R.id.tic_datetimeIndicator);
    mPageIndicator.setViewPager(mViewPager);
    mPageIndicator.setOnPageChangeListener(this);

    if (mPagerAdapter.getCount() < 2) {
        mPageIndicator.setVisibility(View.GONE);
    }

    setView(container);
    setButton(BUTTON_POSITIVE, getContext().getDrawable(R.drawable.tic_ic_btn_next), this);
    setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);

    setTitle(mPagerAdapter.getPageTitle(0));
}

From source file:com.mattprecious.smsfix.library.FixOld.java

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog;/*from  w  w  w .  j ava2s.  c  o m*/
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    switch (id) {
    case DIALOG_ID_START_DATE_PICKER:
        dialog = new DatePickerDialog(this, new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                startCalendar.set(year, monthOfYear, dayOfMonth);

                updateButtons();
            }
        }, startCalendar.get(Calendar.YEAR), startCalendar.get(Calendar.MONTH),
                startCalendar.get(Calendar.DAY_OF_MONTH));
        break;
    case DIALOG_ID_START_TIME_PICKER:
        dialog = new TimePickerDialog(this, new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                startCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                startCalendar.set(Calendar.MINUTE, minute);

                updateButtons();
            }
        }, startCalendar.get(Calendar.HOUR_OF_DAY), startCalendar.get(Calendar.MINUTE),
                DateFormat.is24HourFormat(this));
        break;
    case DIALOG_ID_END_DATE_PICKER:
        dialog = new DatePickerDialog(this, new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                endCalendar.set(year, monthOfYear, dayOfMonth);

                updateButtons();
            }
        }, endCalendar.get(Calendar.YEAR), endCalendar.get(Calendar.MONTH),
                endCalendar.get(Calendar.DAY_OF_MONTH));
        break;
    case DIALOG_ID_END_TIME_PICKER:
        dialog = new TimePickerDialog(this, new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                endCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                endCalendar.set(Calendar.MINUTE, minute);

                updateButtons();
            }
        }, endCalendar.get(Calendar.HOUR_OF_DAY), endCalendar.get(Calendar.MINUTE),
                DateFormat.is24HourFormat(this));
        break;
    case DIALOG_ID_OFFSET_PICKER:
        builder.setTitle(R.string.offset_hours);

        final EditText editText = new EditText(this);

        DecimalFormat df = new DecimalFormat("#.###");
        editText.setText(df.format(Math.abs(offset) / 3600000f));
        editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED
                | InputType.TYPE_NUMBER_FLAG_DECIMAL);

        builder.setView(editText);

        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                offset = (long) (Double.parseDouble(editText.getText().toString()) * 3600000);
                updateButtons();
            }
        });

        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        dialog = builder.create();
        break;
    case DIALOG_ID_CONFIRM:
        builder.setTitle(R.string.fix_old_confirm_title);
        builder.setMessage(R.string.fix_old_confirm_message);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                fixMessages();

            }
        });

        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();

            }
        });

        dialog = builder.create();
        break;
    default:
        dialog = null;
    }

    return dialog;
}

From source file:util.android.util.DateUtils.java

public static SimpleDateFormat getLocalizedHHMMStamp(Context context) {

    // According to users preferences the OS clock is displayed in 24 hour format
    if (DateFormat.is24HourFormat(context)) {
        return formatter24;
    }//from  w  w  w  .  j a va 2  s.  c o  m

    return formatter12;
}

From source file:net.xisberto.work_schedule.MainActivity.java

private void showTimePickerDialog(Period period) {
    waiting_for = period.getId();
    Calendar time = Calendar.getInstance();
    if (timePickerDialog != null && timePickerDialog.isVisible()) {
        timePickerDialog.dismiss();//  w ww  . j a v  a 2s.c o m
    }
    timePickerDialog = RadialTimePickerDialog.newInstance(this, time.get(Calendar.HOUR_OF_DAY),
            time.get(Calendar.MINUTE), DateFormat.is24HourFormat(this));
    // Originally, RadialTimePickerDialog has no title and use no arguments
    // When using the original from Better Pickers, there's no difference
    // But I'm using a lightly different version that adds a title to the
    // dialog and uses the "title" key from arguments to set the dialog's
    // title
    Bundle args = new Bundle();
    args.putString("title", getString(period.getLabelId()));
    timePickerDialog.setArguments(args);
    timePickerDialog.show(getSupportFragmentManager(), "time_picker");
}

From source file:com.geecko.QuickLyric.fragment.SettingsFragment.java

@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.setCancelable(false);/*from  w  w  w.  j  a  v  a2s .  c o m*/
        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;

        if (NightTimeVerifier.check(getActivity())) {
            getActivity().finish();
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction("android.intent.action.MAIN");
            startActivity(intent);
        }
    }
}

From source file:org.zapto.samhippiemiddlepoolchecker.Values.java

public void save() {
    //stores everything as sharedpreferences so the they will be shown when the app is opened
    SharedPreferences.Editor editor = settings.edit();
    editor.putFloat("accepted", accepted);
    editor.putFloat("rejected", rejected);
    editor.putFloat("immature", immature);
    editor.putFloat("unexchanged", unexchanged);
    editor.putFloat("balance", balance);
    editor.putFloat("paid", paid);

    //have to make sure that the user's time and date preferences are respected
    Date date = new Date();
    java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);

    Time time = new Time();
    time.setToNow();//ww  w. ja v a2 s.c o  m
    String formattedTime;
    if (DateFormat.is24HourFormat(context)) {
        formattedTime = time.format("%k:%M");
    } else {
        formattedTime = time.format("%l:%M %p");
    }

    editor.putString("lastUpdatedTime", dateFormat.format(date) + " " + formattedTime);
    editor.commit();

    //update widget here
    MainActivity.updateWidget(context);
}

From source file:net.xisberto.work_schedule.alarm.AlarmMessageActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_close_exit);

    getWindow().addFlags(/* w  ww. j  a v  a2 s  . c o  m*/
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    setContentView(R.layout.activity_alarm_message);

    period_pref_id = getIntent().getIntExtra(EXTRA_PERIOD_ID, R.string.fstp_entrance);
    Log.d("AlarmMessage", "showing alarm for " + period_pref_id);
    period = Period.getPeriod(this, period_pref_id);
    period.enabled = false;
    period.persist(this);
    Log.d("AlarmMessage", "time is " + period.formatTime(true));

    settings = Settings.getInstance(getApplicationContext());
    String time = period.formatTime(DateFormat.is24HourFormat(this));

    ((TextView) findViewById(R.id.txt_alarm_label)).setText(period.getLabelId());
    ((TextView) findViewById(R.id.txt_alarm_time)).setText(time);

    initialPoint = 0f;
    currentPoint = 0f;
    moving = false;

    findViewById(R.id.frame_top).setOnTouchListener(this);
    findViewById(R.id.frame_bottom).setOnTouchListener(this);

    setOrientation();

    prepareSound(getApplicationContext(), getAlarmUri());

    if (settings.getVibrate()) {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(new long[] { 500, 500 }, 0);
    }

    showNotification();

}