Example usage for android.text.format DateUtils formatDateTime

List of usage examples for android.text.format DateUtils formatDateTime

Introduction

In this page you can find the example usage for android.text.format DateUtils formatDateTime.

Prototype

public static String formatDateTime(Context context, long millis, int flags) 

Source Link

Document

Formats a date or a time according to the local conventions.

Usage

From source file:com.borax12.materialdaterangepicker.date.DatePickerDialog.java

private void setCurrentView(final int viewIndex) {
    long millis = mCalendar.getTimeInMillis();
    long millisEnd = mCalendarEnd.getTimeInMillis();

    switch (viewIndex) {
    case MONTH_AND_DAY_VIEW:
        ObjectAnimator pulseAnimator = Utils.getPulseAnimator(mMonthAndDayView, 0.9f, 1.05f);
        ObjectAnimator pulseAnimatorTwo = Utils.getPulseAnimator(mMonthAndDayViewEnd, 0.9f, 1.05f);
        if (mDelayAnimation) {
            pulseAnimator.setStartDelay(ANIMATION_DELAY);
            pulseAnimatorTwo.setStartDelay(ANIMATION_DELAY);
            mDelayAnimation = false;//from w ww .j  av  a 2s  .  c  o m
        }
        mDayPickerView.onDateChanged();
        if (mCurrentView != viewIndex) {
            mMonthAndDayView.setSelected(true);
            mMonthAndDayViewEnd.setSelected(true);
            mYearView.setSelected(false);
            mYearViewEnd.setSelected(false);
            mAnimator.setDisplayedChild(MONTH_AND_DAY_VIEW);
            mAnimatorEnd.setDisplayedChild(MONTH_AND_DAY_VIEW);
            mCurrentView = viewIndex;
        }
        pulseAnimator.start();
        pulseAnimatorTwo.start();

        int flags = DateUtils.FORMAT_SHOW_DATE;
        String dayString = DateUtils.formatDateTime(getActivity(), millis, flags);
        String dayStringEnd = DateUtils.formatDateTime(getActivity(), millisEnd, flags);
        mAnimator.setContentDescription(mDayPickerDescription + ": " + dayString);
        mAnimatorEnd.setContentDescription(mDayPickerDescription + ": " + dayStringEnd);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectDay);
        Utils.tryAccessibilityAnnounce(mAnimatorEnd, mSelectDay);
        break;
    case YEAR_VIEW:
        pulseAnimator = Utils.getPulseAnimator(mYearView, 0.85f, 1.1f);
        pulseAnimatorTwo = Utils.getPulseAnimator(mYearViewEnd, 0.85f, 1.1f);
        if (mDelayAnimation) {
            pulseAnimator.setStartDelay(ANIMATION_DELAY);
            pulseAnimatorTwo.setStartDelay(ANIMATION_DELAY);
            mDelayAnimation = false;
        }
        mYearPickerView.onDateChanged();
        mYearPickerViewEnd.onDateChanged();
        if (mCurrentView != viewIndex) {
            mMonthAndDayView.setSelected(false);
            mYearView.setSelected(true);
            mAnimator.setDisplayedChild(YEAR_VIEW);
            mCurrentView = viewIndex;

            mMonthAndDayViewEnd.setSelected(false);
            mYearViewEnd.setSelected(true);
            mAnimatorEnd.setDisplayedChild(YEAR_VIEW);
            mCurrentViewEnd = viewIndex;
        }
        pulseAnimator.start();
        pulseAnimatorTwo.start();

        CharSequence yearString = YEAR_FORMAT.format(millis);
        CharSequence yearStringEnd = YEAR_FORMAT.format(millisEnd);
        mAnimator.setContentDescription(mYearPickerDescription + ": " + yearString);
        mAnimatorEnd.setContentDescription(mYearPickerDescription + ": " + yearStringEnd);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectYear);
        Utils.tryAccessibilityAnnounce(mAnimatorEnd, mSelectYear);
        break;
    }
}

From source file:com.leavjenn.smoothdaterangepicker.date.SmoothDateRangePickerFragment.java

private void updateDisplay(boolean announce) {
    if (mDayOfWeekView != null && mDayOfWeekViewEnd != null) {
        mDayOfWeekView/*  w ww.j  a va2 s. com*/
                .setText(mCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault())
                        .toUpperCase(Locale.getDefault()));
        mDayOfWeekViewEnd
                .setText(mCalendarEnd.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault())
                        .toUpperCase(Locale.getDefault()));
    }

    mSelectedMonthTextView.setText(mCalendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault())
            .toUpperCase(Locale.getDefault()));
    mSelectedMonthTextViewEnd
            .setText(mCalendarEnd.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault())
                    .toUpperCase(Locale.getDefault()));
    mSelectedDayTextView.setText(DAY_FORMAT.format(mCalendar.getTime()));
    mSelectedDayTextViewEnd.setText(DAY_FORMAT.format(mCalendarEnd.getTime()));
    mYearView.setText(YEAR_FORMAT.format(mCalendar.getTime()));
    mYearViewEnd.setText(YEAR_FORMAT.format(mCalendarEnd.getTime()));
    mDuration = Utils.daysBetween(mCalendar, mCalendarEnd);
    mDurationTextView.setText(String.valueOf(mDuration));
    mDurationDayTextView.setText(mDuration > 1 ? getString(R.string.days) : getString(R.string.day));

    // Accessibility.
    long millis = mCalendar.getTimeInMillis();
    long millisEnd = mCalendarEnd.getTimeInMillis();
    mAnimator.setDateMillis(millis);
    int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR;
    String monthAndDayText = DateUtils.formatDateTime(getActivity(), millis, flags);
    String monthAndDayTextEnd = DateUtils.formatDateTime(getActivity(), millisEnd, flags);
    mMonthAndDayView.setContentDescription(monthAndDayText);
    mMonthAndDayViewEnd.setContentDescription(monthAndDayTextEnd);

    if (announce) {
        flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
        String fullDateText = DateUtils.formatDateTime(getActivity(), millis, flags);
        //            String fullDateTextEnd = DateUtils.formatDateTime(getActivity(), millisEnd, flags);
        Utils.tryAccessibilityAnnounce(mAnimator, fullDateText);
    }
}

From source file:com.android.mms.ui.MessageUtils.java

public static String formatTimeStampString(Context context, long when, boolean fullFormat) {
    Time then = new Time();
    then.set(when);/*from w  w w .  j a v a2 s  . c o  m*/
    Time now = new Time();
    now.setToNow();

    // Basic settings for formatDateTime() we want for all cases.
    int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT |
    /// M: Fix ALPS00419488 to show 12:00, so mark DateUtils.FORMAT_ABBREV_ALL
    //DateUtils.FORMAT_ABBREV_ALL |
            DateUtils.FORMAT_CAP_AMPM;

    // If the message is from a different year, show the date and year.
    if (then.year != now.year) {
        format_flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
    } else if (then.yearDay != now.yearDay) {
        // If it is from a different day than today, show only the date.
        format_flags |= DateUtils.FORMAT_SHOW_DATE;
    } else {
        // Otherwise, if the message is from today, show the time.
        format_flags |= DateUtils.FORMAT_SHOW_TIME;
    }

    // If the caller has asked for full details, make sure to show the date
    // and time no matter what we've determined above (but still make showing
    // the year only happen if it is a different year from today).
    if (fullFormat) {
        format_flags |= (DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);
    }

    String dateTime = sOpMessageUtilsExt.formatTimeStampString(context, when, format_flags);
    if (dateTime != null) {
        return dateTime;
    }
    return DateUtils.formatDateTime(context, when, format_flags);
}

From source file:com.borax12.materialdaterangepicker.date.DatePickerDialog.java

private void updateDisplay(boolean announce) {
    if (mDayOfWeekView != null) {
        mDayOfWeekView/*from  w  w w .j  av  a  2  s.  com*/
                .setText(mCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault())
                        .toUpperCase(Locale.getDefault()));
    }

    mSelectedMonthTextView.setText(mCalendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault())
            .toUpperCase(Locale.getDefault()));
    mSelectedMonthTextViewEnd
            .setText(mCalendarEnd.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault())
                    .toUpperCase(Locale.getDefault()));
    mSelectedDayTextView.setText(DAY_FORMAT.format(mCalendar.getTime()));
    mSelectedDayTextViewEnd.setText(DAY_FORMAT.format(mCalendarEnd.getTime()));
    mYearView.setText(YEAR_FORMAT.format(mCalendar.getTime()));
    mYearViewEnd.setText(YEAR_FORMAT.format(mCalendarEnd.getTime()));

    // Accessibility.
    long millis = mCalendar.getTimeInMillis();
    long millisEnd = mCalendarEnd.getTimeInMillis();
    mAnimator.setDateMillis(millis);
    mAnimatorEnd.setDateMillis(millisEnd);
    int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR;
    String monthAndDayText = DateUtils.formatDateTime(getActivity(), millis, flags);
    String monthAndDayTextEnd = DateUtils.formatDateTime(getActivity(), millisEnd, flags);
    mMonthAndDayView.setContentDescription(monthAndDayText);
    mMonthAndDayViewEnd.setContentDescription(monthAndDayTextEnd);

    if (announce) {
        flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
        String fullDateText = DateUtils.formatDateTime(getActivity(), millis, flags);
        String fullDateTextEnd = DateUtils.formatDateTime(getActivity(), millisEnd, flags);
        Utils.tryAccessibilityAnnounce(mAnimator, fullDateText);
        Utils.tryAccessibilityAnnounce(mAnimatorEnd, fullDateTextEnd);
    }
}

From source file:com.kenmeidearu.materialdatetimepicker.date.DatePickerDialog.java

private void setCurrentView(final int viewIndex) {
    long millis = mCalendar.getTimeInMillis();
    switch (viewIndex) {
    case MONTH_AND_DAY_VIEW:
        ObjectAnimator pulseAnimator = Utils.getPulseAnimator(mDayOfWeekView, 0.9f, 1.05f);
        if (mDelayAnimation) {
            pulseAnimator.setStartDelay(ANIMATION_DELAY);
            mDelayAnimation = false;//from  ww w . j ava 2  s  .  c  o m
        }
        mDayPickerView.onDateChanged();
        if (mCurrentView != viewIndex) {
            mMonthAndDayView.setSelected(true);
            mSelectedDayTextView.setSelected(true);//tadinya month and day view
            mSelectedMonthTextView.setSelected(false);
            mYearView.setSelected(false);
            mHourView.setSelected(false);
            mMinuteView.setSelected(false);
            mSecondView.setSelected(false);
            mAmPmTextView.setSelected(false);
            mHourView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mMinuteView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mSecondView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mAnimator.setDisplayedChild(MONTH_AND_DAY_VIEW);
            mCurrentView = viewIndex;
        }
        pulseAnimator.start();

        int flags = DateUtils.FORMAT_SHOW_DATE;
        String dayString = DateUtils.formatDateTime(getActivity(), millis, flags);
        mAnimator.setContentDescription(mDayPickerDescription + ": " + dayString);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectDay);
        break;
    case MONTH_VIEW:
        pulseAnimator = Utils.getPulseAnimator(mSelectedMonthTextView, 0.85f, 1.1f);
        if (mDelayAnimation) {
            pulseAnimator.setStartDelay(ANIMATION_DELAY);
            mDelayAnimation = false;
        }
        mMonthPickerView.onDateChanged();
        if (mCurrentView != viewIndex) {
            mMonthAndDayView.setSelected(true);
            mSelectedDayTextView.setSelected(false);
            mSelectedMonthTextView.setSelected(true);
            mYearView.setSelected(false);
            mHourView.setSelected(false);
            mMinuteView.setSelected(false);
            mSecondView.setSelected(false);
            mAmPmTextView.setSelected(false);
            mHourView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mMinuteView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mSecondView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mAnimator.setDisplayedChild(MONTH_VIEW);
            mCurrentView = viewIndex;
        }
        pulseAnimator.start();

        CharSequence monthString = MONTH_FORMAT.format(millis);
        mAnimator.setContentDescription(mMonthPickerDescription + ": " + monthString);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectMonth);
        break;
    case YEAR_VIEW:
        pulseAnimator = Utils.getPulseAnimator(mYearView, 0.85f, 1.1f);
        if (mDelayAnimation) {
            pulseAnimator.setStartDelay(ANIMATION_DELAY);
            mDelayAnimation = false;
        }
        mYearPickerView.onDateChanged();
        if (mCurrentView != viewIndex) {
            mMonthAndDayView.setSelected(false);
            mSelectedDayTextView.setSelected(false);
            mSelectedMonthTextView.setSelected(false);
            mYearView.setSelected(true);
            mHourView.setSelected(false);
            mMinuteView.setSelected(false);
            mSecondView.setSelected(false);
            mAmPmTextView.setSelected(false);
            mHourView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mMinuteView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mSecondView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mAnimator.setDisplayedChild(YEAR_VIEW);
            mCurrentView = viewIndex;
        }
        pulseAnimator.start();

        CharSequence yearString = YEAR_FORMAT.format(millis);
        mAnimator.setContentDescription(mYearPickerDescription + ": " + yearString);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectYear);
        break;
    case HOUR_INDEX:
        pulseAnimator = Utils.getPulseAnimator(mHourView, 0.85f, 1.1f);
        if (mDelayAnimation) {
            pulseAnimator.setStartDelay(ANIMATION_DELAY);
            mDelayAnimation = false;
        }
        mHourPickerView.onDateChanged();
        if (mCurrentView != viewIndex) {
            mMonthAndDayView.setSelected(true);
            mSelectedDayTextView.setSelected(false);
            mSelectedMonthTextView.setSelected(false);
            mYearView.setSelected(false);
            mHourView.setSelected(true);
            mMinuteView.setSelected(false);
            mSecondView.setSelected(false);
            mAmPmTextView.setSelected(false);
            mHourView.setTextColor(getResources().getColor(R.color.mdtp_white));
            mMinuteView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mSecondView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mAnimator.setDisplayedChild(HOUR_INDEX);
            mCurrentView = viewIndex;
        }
        pulseAnimator.start();

        CharSequence hourString = MONTH_FORMAT.format(millis);
        mAnimator.setContentDescription(mHourPickerDescription + ": " + hourString);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectHours);
        break;
    case MINUTE_INDEX:
        pulseAnimator = Utils.getPulseAnimator(mMinuteView, 0.85f, 1.1f);
        if (mDelayAnimation) {
            pulseAnimator.setStartDelay(ANIMATION_DELAY);
            mDelayAnimation = false;
        }
        mMinutePickerView.onDateChanged();
        if (mCurrentView != viewIndex) {
            mMonthAndDayView.setSelected(true);
            mSelectedDayTextView.setSelected(false);
            mSelectedMonthTextView.setSelected(false);
            mYearView.setSelected(false);
            mHourView.setSelected(false);
            mMinuteView.setSelected(true);
            mSecondView.setSelected(false);
            mAmPmTextView.setSelected(false);
            mHourView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mMinuteView.setTextColor(getResources().getColor(R.color.mdtp_white));
            mSecondView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mAnimator.setDisplayedChild(MINUTE_INDEX);
            mCurrentView = viewIndex;
        }
        pulseAnimator.start();

        CharSequence minuteString = MONTH_FORMAT.format(millis);
        mAnimator.setContentDescription(mMinutePickerDescription + ": " + minuteString);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectMinutes);
        break;
    case SECOND_INDEX:
        pulseAnimator = Utils.getPulseAnimator(mSecondView, 0.85f, 1.1f);
        if (mDelayAnimation) {
            pulseAnimator.setStartDelay(ANIMATION_DELAY);
            mDelayAnimation = false;
        }
        mSecondPickerView.onDateChanged();
        if (mCurrentView != viewIndex) {
            mMonthAndDayView.setSelected(true);
            mSelectedDayTextView.setSelected(false);
            mSelectedMonthTextView.setSelected(false);
            mYearView.setSelected(false);
            mHourView.setSelected(false);
            mMinuteView.setSelected(false);
            mSecondView.setSelected(true);
            mAmPmTextView.setSelected(false);
            mHourView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mMinuteView.setTextColor(getResources().getColor(R.color.mdtp_accent_color_focused));
            mSecondView.setTextColor(getResources().getColor(R.color.mdtp_white));
            mAnimator.setDisplayedChild(SECOND_INDEX);
            mCurrentView = viewIndex;
        }
        pulseAnimator.start();

        CharSequence secondString = MONTH_FORMAT.format(millis);
        mAnimator.setContentDescription(mSecondPickerDescription + ": " + secondString);
        Utils.tryAccessibilityAnnounce(mAnimator, mSelectSeconds);
        break;
    }
}

From source file:com.a.mirko.android.datetimepicker.time.RadialPickerLayout.java

/**
 * Announce the currently-selected time when launched.
 *///from www  .j  a v a  2  s .co m
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        // Clear the event's current text so that only the current time will be spoken.
        event.getText().clear();
        Time time = new Time();
        time.hour = getHours();
        time.minute = getMinutes();
        long millis = time.normalize(true);
        int flags = DateUtils.FORMAT_SHOW_TIME;
        if (mIs24HourMode) {
            flags |= DateUtils.FORMAT_24HOUR;
        }
        String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
        event.getText().add(timeString);
        return true;
    }
    return super.dispatchPopulateAccessibilityEvent(event);
}

From source file:com.redinput.datetimepickercompat.time.RadialPickerLayout.java

private void installAccessibilityDelegate() {
    // The accessibility delegate enables customizing accessibility behavior
    // via composition as opposed as inheritance. The main benefit is that
    // one can write a backwards compatible application by setting the delegate
    // only if the API level is high enough i.e. the delegate is part of the APIs.
    // The easiest way to achieve that is by using the support library which
    // takes the burden of checking API version and knowing which API version
    // introduced the delegate off the developer.
    ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegateCompat() {

        @Override// ww  w  .j  a va2  s .  co  m
        public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
            super.onInitializeAccessibilityNodeInfo(host, info);
            // Note that View.onInitializeAccessibilityNodeInfo was introduced in
            // ICS and we would like to tweak a bit the text that is reported to
            // accessibility services via the AccessibilityNodeInfo.
            info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
            info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
        }

        @Override
        public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
            if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
                // Clear the event's current text so that only the current time will be spoken.
                event.getText().clear();
                Time time = new Time();
                time.hour = getHours();
                time.minute = getMinutes();
                long millis = time.normalize(true);
                int flags = DateUtils.FORMAT_SHOW_TIME;
                if (mIs24HourMode) {
                    flags |= DateUtils.FORMAT_24HOUR;
                }
                String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
                event.getText().add(timeString);
                return true;
            }

            return super.dispatchPopulateAccessibilityEvent(host, event);
        }

        @Override
        public boolean performAccessibilityAction(View host, int action, Bundle args) {
            if (super.performAccessibilityAction(host, action, args)) {
                return true;
            }

            int changeMultiplier = 0;
            if (action == AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD) {
                changeMultiplier = 1;
            } else if (action == AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD) {
                changeMultiplier = -1;
            }
            if (changeMultiplier != 0) {
                int value = getCurrentlyShowingValue();
                int stepSize = 0;
                int currentItemShowing = getCurrentItemShowing();
                if (currentItemShowing == HOUR_INDEX) {
                    stepSize = HOUR_VALUE_TO_DEGREES_STEP_SIZE;
                    value %= 12;
                } else if (currentItemShowing == MINUTE_INDEX) {
                    stepSize = MINUTE_VALUE_TO_DEGREES_STEP_SIZE;
                }

                int degrees = value * stepSize;
                degrees = snapOnly30s(degrees, changeMultiplier);
                value = degrees / stepSize;
                int maxValue = 0;
                int minValue = 0;
                if (currentItemShowing == HOUR_INDEX) {
                    if (mIs24HourMode) {
                        maxValue = 23;
                    } else {
                        maxValue = 12;
                        minValue = 1;
                    }
                } else {
                    maxValue = 55;
                }
                if (value > maxValue) {
                    // If we scrolled forward past the highest number, wrap around to the
                    // lowest.
                    value = minValue;
                } else if (value < minValue) {
                    // If we scrolled backward past the lowest number, wrap around to the
                    // highest.
                    value = maxValue;
                }
                setItem(currentItemShowing, value);
                mListener.onValueSelected(currentItemShowing, value, false);
                return true;
            }

            return false;
        }
    });
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static String formatSameDayTime(final Context context, final long timestamp) {
    if (context == null)
        return null;
    if (DateUtils.isToday(timestamp))
        return DateUtils.formatDateTime(context, timestamp,
                DateFormat.is24HourFormat(context) ? DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR
                        : DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR);
    return DateUtils.formatDateTime(context, timestamp, DateUtils.FORMAT_SHOW_DATE);
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static String formatTimeStampString(final Context context, final long timestamp) {
    if (context == null)
        return null;
    final Time then = new Time();
    then.set(timestamp);//from w  w  w  .  jav  a 2s .  co m
    final Time now = new Time();
    now.setToNow();

    int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT | DateUtils.FORMAT_ABBREV_ALL
            | DateUtils.FORMAT_CAP_AMPM;

    if (then.year != now.year) {
        format_flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
    } else if (then.yearDay != now.yearDay) {
        format_flags |= DateUtils.FORMAT_SHOW_DATE;
    } else {
        format_flags |= DateUtils.FORMAT_SHOW_TIME;
    }

    return DateUtils.formatDateTime(context, timestamp, format_flags);
}

From source file:be.billington.calendar.recurrencepicker.RecurrencePickerDialog.java

public void updateDialog() {
    // Interval//from w w  w .  j  a v  a  2  s  . c  om
    // Checking before setting because this causes infinite recursion
    // in afterTextWatcher
    final String intervalStr = Integer.toString(mModel.interval);
    if (!intervalStr.equals(mInterval.getText().toString())) {
        mInterval.setText(intervalStr);
    }

    mFreqSpinner.setSelection(mModel.freq);
    mWeekGroup.setVisibility(mModel.freq == RecurrenceModel.FREQ_WEEKLY ? View.VISIBLE : View.GONE);
    mWeekGroup2.setVisibility(mModel.freq == RecurrenceModel.FREQ_WEEKLY ? View.VISIBLE : View.GONE);
    mMonthGroup.setVisibility(mModel.freq == RecurrenceModel.FREQ_MONTHLY ? View.VISIBLE : View.GONE);

    switch (mModel.freq) {
    case RecurrenceModel.FREQ_DAILY:
        mIntervalResId = R.plurals.recurrence_interval_daily;
        break;

    case RecurrenceModel.FREQ_WEEKLY:
        mIntervalResId = R.plurals.recurrence_interval_weekly;
        for (int i = 0; i < 7; i++) {
            mWeekByDayButtons[i].setChecked(mModel.weeklyByDayOfWeek[i]);
        }
        break;

    case RecurrenceModel.FREQ_MONTHLY:
        mIntervalResId = R.plurals.recurrence_interval_monthly;

        if (mModel.monthlyRepeat == RecurrenceModel.MONTHLY_BY_DATE) {
            mMonthRepeatByRadioGroup.check(R.id.repeatMonthlyByNthDayOfMonth);
        } else if (mModel.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
            mMonthRepeatByRadioGroup.check(R.id.repeatMonthlyByNthDayOfTheWeek);
        }

        if (mMonthRepeatByDayOfWeekStr == null) {
            if (mModel.monthlyByNthDayOfWeek == 0) {
                mModel.monthlyByNthDayOfWeek = (mTime.monthDay + 6) / 7;
                // Since not all months have 5 weeks, we convert 5th NthDayOfWeek to
                // -1 for last monthly day of the week
                if (mModel.monthlyByNthDayOfWeek >= FIFTH_WEEK_IN_A_MONTH) {
                    mModel.monthlyByNthDayOfWeek = LAST_NTH_DAY_OF_WEEK;
                }
                mModel.monthlyByDayOfWeek = mTime.weekDay;
            }

            String[] monthlyByNthDayOfWeekStrs = mMonthRepeatByDayOfWeekStrs[mModel.monthlyByDayOfWeek];

            // TODO(psliwowski): Find a better way handle -1 indexes
            int msgIndex = mModel.monthlyByNthDayOfWeek < 0 ? FIFTH_WEEK_IN_A_MONTH
                    : mModel.monthlyByNthDayOfWeek;
            mMonthRepeatByDayOfWeekStr = monthlyByNthDayOfWeekStrs[msgIndex - 1];
            mRepeatMonthlyByNthDayOfWeek.setText(mMonthRepeatByDayOfWeekStr);
        }
        break;

    case RecurrenceModel.FREQ_YEARLY:
        mIntervalResId = R.plurals.recurrence_interval_yearly;
        break;
    }
    updateIntervalText();
    updateDoneButtonState();

    mEndSpinner.setSelection(mModel.end);
    if (mModel.end == RecurrenceModel.END_BY_DATE) {
        final String dateStr = DateUtils.formatDateTime(getActivity(), mModel.endDate.toMillis(false),
                DateUtils.FORMAT_NUMERIC_DATE);
        mEndDateTextView.setText(dateStr);
    } else {
        if (mModel.end == RecurrenceModel.END_BY_COUNT) {
            // Checking before setting because this causes infinite
            // recursion
            // in afterTextWatcher
            final String countStr = Integer.toString(mModel.endCount);
            if (!countStr.equals(mEndCount.getText().toString())) {
                mEndCount.setText(countStr);
            }
        }
    }
}