Example usage for android.text.format DateUtils formatDateRange

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

Introduction

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

Prototype

public static Formatter formatDateRange(Context context, Formatter formatter, long startMillis, long endMillis,
        int flags, String timeZone) 

Source Link

Document

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

Usage

From source file:com.codetroopers.betterpickers.calendardatepicker.MonthView.java

private String getMonthAndYearString() {
    int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NO_MONTH_DAY;
    mStringBuilder.setLength(0);// w w w  . j  a v  a2 s  .com
    long millis = mCalendar.getTimeInMillis();
    String monthTitle = DateUtils
            .formatDateRange(getContext(), mFormatter, millis, millis, flags, Time.getCurrentTimezone())
            .toString();
    return monthTitle.substring(0, 1).toUpperCase() + monthTitle.substring(1).toLowerCase();
}

From source file:com.google.samples.apps.iosched.util.UIUtils.java

/**
 * @param startTime The start time of a session in millis.
 * @param context   The context to be used for getting the display timezone.
 * @return Formats a given startTime to the specific short time. example: 12:00 AM
 *///from   w  w w .j  av a 2  s.  com
public static String formatTime(long startTime, Context context) {
    StringBuilder sb = new StringBuilder();
    DateUtils.formatDateRange(context, new Formatter(sb), startTime, startTime, DateUtils.FORMAT_SHOW_TIME,
            SettingsUtils.getDisplayTimeZone(context).getID());
    return sb.toString();
}

From source file:com.gdgdevfest.android.apps.devfestbcn.ui.MapFragment.java

/**
 * Create an {@link AsyncQueryHandler} for use with the
 * {@link MapInfoWindowAdapter}.//from w  ww.ja v  a2s .com
 */
private AsyncQueryHandler createInfowindowHandler(ContentResolver contentResolver) {
    return new AsyncQueryHandler(contentResolver) {
        StringBuilder mBuffer = new StringBuilder();
        Formatter mFormatter = new Formatter(mBuffer, Locale.getDefault());

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {

            MarkerModel model = mMarkers.get(cookie);

            mInfoAdapter.clearData();

            if (model == null || cursor == null) {
                // query did not complete or incorrect data was loaded
                return;
            }

            final long time = UIUtils.getCurrentTime(getActivity());

            switch (token) {
            case SessionAfterQuery._TOKEN: {
                extractSession(cursor, model, time);
            }
                break;
            case SandboxCompaniesAtQuery._TOKEN: {
                extractSandbox(cursor, model, time);
            }
            }

            // update the displayed window
            model.marker.showInfoWindow();
        }

        private void extractSandbox(Cursor cursor, MarkerModel model, long time) {
            // get tracks data from cache: icon and color
            TrackModel track = mTracks.get(model.track);
            int color = (track != null) ? track.color : 0;
            int iconResId = 0;
            if (track != null) {
                iconResId = getResources().getIdentifier("track_" + ParserUtils.sanitizeId(track.name),
                        "drawable", getActivity().getPackageName());
            }

            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();

                StringBuilder sb = new StringBuilder();
                int count = 0;
                final int maxCompaniesDisplay = getResources()
                        .getInteger(R.integer.sandbox_company_list_max_display);
                while (!cursor.isAfterLast() && count < maxCompaniesDisplay) {
                    if (sb.length() > 0) {
                        sb.append(", ");
                    }
                    sb.append(cursor.getString(SandboxCompaniesAtQuery.COMPANY_NAME));
                    count++;
                    cursor.moveToNext();
                }
                if (count >= maxCompaniesDisplay && !cursor.isAfterLast()) {
                    // Additional sandbox companies to display
                    sb.append(", &hellip;");
                }

                mInfoAdapter.setSandbox(model.marker, model.label, color, iconResId,
                        sb.length() > 0 ? sb.toString() : null);

            } else {
                // No active sandbox companies
                mInfoAdapter.setSandbox(model.marker, model.label, color, iconResId, null);
            }

            model.marker.showInfoWindow();
        }

        private static final long SHOW_UPCOMING_TIME = 24 * 60 * 60 * 1000; // 24 hours

        private void extractSession(Cursor cursor, MarkerModel model, long time) {

            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();

                String currentTitle = null;
                String nextTitle = null;
                String nextTime = null;

                final long blockStart = cursor.getLong(SessionAfterQuery.BLOCK_START);
                final long blockEnd = cursor.getLong(SessionAfterQuery.BLOCK_END);
                boolean inProgress = time >= blockStart && time <= blockEnd;

                if (inProgress) {
                    // A session is running, display its name and optionally
                    // the next session
                    currentTitle = cursor.getString(SessionAfterQuery.SESSION_TITLE);

                    //move to the next entry
                    cursor.moveToNext();
                }

                if (!cursor.isAfterLast()) {
                    //There is a session coming up next, display only it if it's within 24 hours of the current time
                    final long nextStart = cursor.getLong(SessionAfterQuery.BLOCK_START);

                    if (nextStart < time + SHOW_UPCOMING_TIME) {
                        nextTitle = cursor.getString(SessionAfterQuery.SESSION_TITLE);
                        mBuffer.setLength(0);

                        boolean showWeekday = !DateUtils.isToday(blockStart)
                                && !UIUtils.isSameDayDisplay(UIUtils.getCurrentTime(getActivity()), blockStart,
                                        getActivity());

                        nextTime = DateUtils.formatDateRange(getActivity(), mFormatter, blockStart, blockStart,
                                DateUtils.FORMAT_SHOW_TIME | (showWeekday
                                        ? DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY
                                        : 0),
                                PrefUtils.getDisplayTimeZone(getActivity()).getID()).toString();
                    }
                }

                // populate the info window adapter
                mInfoAdapter.setSessionData(model.marker, model.label, currentTitle, nextTitle, nextTime,
                        inProgress);

            } else {
                // No entries, display name of room only
                mInfoAdapter.setMarker(model.marker, model.label);
            }
        }

    };
}

From source file:org.voidsink.anewjkuapp.utils.AppUtils.java

public static String getTimeString(Context c, Date dtStart, Date dtEnd, boolean allDay) {
    int flags = 0;
    String tzString = TimeZone.getDefault().getID();
    if (allDay) {
        tzString = "UTC";
    } else {/*from w w  w . j  a  v a  2  s. c om*/
        flags = DateUtils.FORMAT_SHOW_TIME;
    }

    return DateUtils.formatDateRange(c, new Formatter(Locale.getDefault()), dtStart.getTime(), dtEnd.getTime(),
            flags, tzString).toString();
}