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.google.samples.apps.iosched.util.TimeUtils.java

/**
 * Format a {@code date} honoring the app preference for using Conference or device timezone.
 * {@code Context} is used to lookup the shared preference settings.
 *//*  w ww .  j  a v a  2  s.com*/
public static String formatShortDate(Context context, Date date) {
    StringBuilder sb = new StringBuilder();
    Formatter formatter = new Formatter(sb);
    return DateUtils.formatDateRange(context, formatter, date.getTime(), date.getTime(),
            DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_NO_YEAR,
            SettingsUtils.getDisplayTimeZone(context).getID()).toString();
}

From source file:com.dgsd.android.ShiftTracker.Adapter.WeekPagerAdapter.java

protected String getTitleForPosition(int pos) {
    mTime.setJulianDay(getJulianDayForPosition(pos));
    if (mTime.weekDay != mWeekStartDay) {
        while (mTime.weekDay != mWeekStartDay) {
            mTime.monthDay--;/*from  w  w w .j  av  a2  s .  co m*/
            mTime.normalize(true);
        }
    }

    long startMillis = mTime.toMillis(true);
    long endMillis = startMillis + (1000 * 60 * 60 * 24 * 7);
    int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
    mStringBuilder.setLength(0);

    return DateUtils.formatDateRange(mContext, mFormatter, startMillis, endMillis, flags, mTime.timezone)
            .toString();
}

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

/**
 * Format and return the given time interval using {@link Config#CONFERENCE_TIMEZONE} (unless
 * local time was explicitly requested by the user).
 *//*from  ww w  . j a v a 2s .  com*/
public static String formatIntervalTimeString(long intervalStart, long intervalEnd, StringBuilder recycle,
        Context context) {
    if (recycle == null) {
        recycle = new StringBuilder();
    } else {
        recycle.setLength(0);
    }
    Formatter formatter = new Formatter(recycle);
    return DateUtils.formatDateRange(context, formatter, intervalStart, intervalEnd, TIME_FLAGS,
            SettingsUtils.getDisplayTimeZone(context).getID()).toString();
}

From source file:com.hyperaware.conference.android.fragment.SessionDetailFragment.java

private void updateSessionDetail() {
    tvTopic.setText(agendaItem.getTopic());
    host.setTitle(agendaItem.getTopic());

    final StringBuilder sb = new StringBuilder();
    final Formatter formatter = new Formatter(sb);

    final long start_ms = TimeUnit.SECONDS.toMillis(agendaItem.getEpochStartTime());
    final long end_ms = TimeUnit.SECONDS.toMillis(agendaItem.getEpochEndTime());

    sb.setLength(0);//from  ww  w .  j a  va2 s . c om
    DateUtils.formatDateRange(getActivity(), formatter, start_ms, end_ms,
            DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY, tz.getID());
    tvDate.setText(formatter.toString());

    sb.setLength(0);
    DateUtils.formatDateRange(getActivity(), formatter, start_ms, end_ms, DateUtils.FORMAT_SHOW_TIME,
            tz.getID());
    tvTime.setText(formatter.toString());

    final String location = agendaItem.getLocation();
    if (!Strings.isNullOrEmpty(location)) {
        tvLocation.setText(agendaItem.getLocation());
    } else {
        tvLocation.setVisibility(View.GONE);
    }

    tvDescription.setText(agendaItem.getDescription());

    // Only sessions with speakers can have feedback
    final View feedback = vgActions.findViewById(R.id.tv_session_feedback);
    if (speakerItems.size() > 0) {
        feedback.setOnClickListener(new SessionFeedbackOnClickListener());
        vgActions.setVisibility(View.VISIBLE);
    } else {
        vgActions.setVisibility(View.GONE);
    }

    if (speakerItems.size() > 0) {
        vgSpeakers.setVisibility(View.VISIBLE);
        vgSpeakers.removeAllViews();
        final LayoutInflater inflater = getActivity().getLayoutInflater();

        for (final SpeakerItem item : speakerItems) {
            final View view = inflater.inflate(R.layout.item_session_speaker, vgSpeakers, false);

            ImageView iv = (ImageView) view.findViewById(R.id.iv_pic);
            Glide.with(SessionDetailFragment.this).load(item.getImage100()).placeholder(R.drawable.nopic)
                    .into(iv);

            ((TextView) view.findViewById(R.id.tv_name)).setText(item.getName());

            if (host != null) {
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Fragment next = SpeakerDetailFragment.instantiate(item.getId());
                        host.pushFragment(next, "speaker_detail");
                    }
                });
            }

            vgSpeakers.addView(view);
        }
    }
}

From source file:com.hyperaware.conference.android.fragment.SpeakerDetailFragment.java

private void updateSpeaker() {
    tvName.setText(speakerItem.getName());
    host.setTitle(speakerItem.getName());

    final String company = speakerItem.getCompanyName();
    tvCompany.setVisibility(Strings.isNullOrEmpty(company) ? View.GONE : View.VISIBLE);
    tvCompany.setText(company);/*from   w w  w .ja  v  a  2 s .c om*/

    final String title = speakerItem.getTitle();
    tvTitle.setVisibility(Strings.isNullOrEmpty(title) ? View.GONE : View.VISIBLE);
    tvTitle.setText(title);

    Glide.with(SpeakerDetailFragment.this).load(speakerItem.getImage100()).fitCenter()
            .placeholder(R.drawable.nopic).into(ivPic);

    boolean links_visible = false;
    final String website = speakerItem.getWebsite();
    if (!Strings.isNullOrEmpty(website)) {
        links_visible = true;
        tvWebsite.setVisibility(View.VISIBLE);
        tvWebsite.setText(website);
    }
    final String twitter = speakerItem.getTwitter();
    if (!Strings.isNullOrEmpty(twitter)) {
        links_visible = true;
        tvTwitter.setVisibility(View.VISIBLE);
        tvTwitter.setText(twitter);
    }
    final String facebook = speakerItem.getFacebook();
    if (!Strings.isNullOrEmpty(facebook)) {
        links_visible = true;
        tvFacebook.setVisibility(View.VISIBLE);
        tvFacebook.setText(facebook);
    }
    final String linkedin = speakerItem.getLinkedin();
    if (!Strings.isNullOrEmpty(linkedin)) {
        links_visible = true;
        tvLinkedin.setVisibility(View.VISIBLE);
        tvLinkedin.setText(linkedin);
    }
    vgDetailLinks.setVisibility(links_visible ? View.VISIBLE : View.GONE);

    tvAbout.setText(speakerItem.getAbout());

    if (agendaItems.size() > 0) {
        vgSessions.setVisibility(View.VISIBLE);
        vgSessions.removeAllViews();
        final StringBuilder sb = new StringBuilder();
        final Formatter formatter = new Formatter(sb);
        final LayoutInflater inflater = getActivity().getLayoutInflater();
        for (final AgendaItem item : agendaItems) {
            final View view = inflater.inflate(R.layout.item_speaker_session, vgSessions, false);

            ((TextView) view.findViewById(R.id.tv_topic)).setText(item.getTopic());

            sb.setLength(0);
            DateUtils.formatDateRange(getActivity(), formatter, item.getEpochStartTime() * 1000,
                    item.getEpochEndTime() * 1000, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY,
                    tz.getID());
            ((TextView) view.findViewById(R.id.tv_date)).setText(formatter.toString());

            sb.setLength(0);
            DateUtils.formatDateRange(getActivity(), formatter, item.getEpochStartTime() * 1000,
                    item.getEpochEndTime() * 1000, DateUtils.FORMAT_SHOW_TIME, tz.getID());
            ((TextView) view.findViewById(R.id.tv_time)).setText(formatter.toString());

            final String session_id = item.getId();
            final ImageButton ib_favorite = (ImageButton) view.findViewById(R.id.button_favorite_session);
            favSessionButtonManager.attach(ib_favorite, session_id);

            if (host != null) {
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Fragment next = SessionDetailFragment.instantiate(item.getId());
                        host.pushFragment(next, "session_detail");
                    }
                });
            }

            vgSessions.addView(view);
        }
    }
}

From source file:com.hyperaware.conference.android.fragment.HomeFragment.java

private void populateTimeGroups(SortedMap<DateRange, List<AgendaItem>> groups, ViewGroup time_groups) {
    final LayoutInflater inflater = getActivity().getLayoutInflater();

    for (Map.Entry<DateRange, List<AgendaItem>> entry : groups.entrySet()) {
        final ViewGroup sessions_group = (ViewGroup) inflater.inflate(R.layout.item_time_group_sessions,
                time_groups, false);/*  w  ww . j  a  va2s .  co  m*/
        time_groups.addView(sessions_group);

        final TextView tv_time = (TextView) sessions_group.findViewById(R.id.tv_time);
        final DateRange range = entry.getKey();
        sb.setLength(0);
        DateUtils.formatDateRange(tv_time.getContext(), formatter, range.start, range.end,
                DateUtils.FORMAT_SHOW_TIME, event.getTimezoneName());
        tv_time.setText(formatter.toString());

        final ViewGroup vg_sessions = (ViewGroup) sessions_group.findViewById(R.id.vg_sessions);
        vg_sessions.removeAllViews();
        for (final AgendaItem item : entry.getValue()) {
            final View session = inflater.inflate(R.layout.item_time_group_session, vg_sessions, false);
            vg_sessions.addView(session);
            final TextView tv_topic = (TextView) session.findViewById(R.id.tv_topic);
            tv_topic.setText(item.getTopic());
        }
    }
}

From source file:com.conferenceengineer.android.iosched.ui.ScheduleFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (!isAdded()) {
        return;/*from w w  w  .j ava2s  .  c o m*/
    }

    Context context = getActivity();
    long currentTime = UIUtils.getCurrentTime(getActivity());
    int firstNowPosition = ListView.INVALID_POSITION;

    String displayTimeZone = PrefUtils.getDisplayTimeZone(context).getID();

    List<SimpleSectionedListAdapter.Section> sections = new ArrayList<SimpleSectionedListAdapter.Section>();
    cursor.moveToFirst();
    long previousBlockStart = -1;
    long blockStart, blockEnd;
    while (!cursor.isAfterLast()) {
        blockStart = cursor.getLong(BlocksQuery.BLOCK_START);
        blockEnd = cursor.getLong(BlocksQuery.BLOCK_END);
        if (!UIUtils.isSameDayDisplay(previousBlockStart, blockStart, context)) {
            mBuffer.setLength(0);
            sections.add(
                    new SimpleSectionedListAdapter.Section(cursor.getPosition(),
                            DateUtils
                                    .formatDateRange(context, mFormatter, blockStart, blockStart,
                                            DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_SHOW_DATE
                                                    | DateUtils.FORMAT_SHOW_WEEKDAY,
                                            displayTimeZone)
                                    .toString()));
        }
        if (mScrollToNow && firstNowPosition == ListView.INVALID_POSITION
        // if we're currently in this block, or we're not in a block
        // and this block is in the future, then this is the scroll position
                && ((blockStart < currentTime && currentTime < blockEnd) || blockStart > currentTime)) {
            firstNowPosition = cursor.getPosition();
        }
        previousBlockStart = blockStart;
        cursor.moveToNext();
    }

    mScheduleAdapter.swapCursor(cursor);

    SimpleSectionedListAdapter.Section[] dummy = new SimpleSectionedListAdapter.Section[sections.size()];
    mAdapter.setSections(sections.toArray(dummy));

    if (mScrollToNow && firstNowPosition != ListView.INVALID_POSITION) {
        firstNowPosition = mAdapter.positionToSectionedPosition(firstNowPosition);
        getListView().setSelectionFromTop(firstNowPosition,
                getResources().getDimensionPixelSize(R.dimen.list_scroll_top_offset));
        mScrollToNow = false;
    }
}

From source file:org.holoeverywhere.widget.datetimepicker.date.SimpleMonthView.java

private String getMonthAndYearString() {
    int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NO_MONTH_DAY;
    mStringBuilder.setLength(0);//from   www  .ja v a2  s .  c  om
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        long millis = mCalendar.getTimeInMillis();
        return DateUtils
                .formatDateRange(getContext(), mFormatter, millis, millis, flags, Time.getCurrentTimezone())
                .toString();
    } else {
        if (mMonthAndYearFormatter == null) {
            mMonthAndYearFormatter = new SimpleDateFormat("MMMM yyyy", Locale.getDefault());
            mMonthAndYearFormatter.setTimeZone(TimeZone.getDefault());
        }
        return mMonthAndYearFormatter.format(mCalendar.getTime());
    }
}

From source file:app.android.datetimepicker.date.SimpleMonthView.java

private String getMonthAndYearString() {
    int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NO_MONTH_DAY;
    mStringBuilder.setLength(0);//ww w  . ja  v  a2  s .  co  m
    long millis = mCalendar.getTimeInMillis();
    return DateUtils.formatDateRange(getContext(), mFormatter, millis, millis, flags, Time.getCurrentTimezone())
            .toString();
}

From source file:com.borax12.materialdaterangepicker.date.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  va2  s  .  c  om*/
    long millis = mCalendar.getTimeInMillis();
    return DateUtils.formatDateRange(getContext(), mFormatter, millis, millis, flags, null).toString();
}