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:de.incoherent.suseconferenceclient.app.SocialWrapper.java

public static ArrayList<SocialItem> getGooglePlusItems(Context context, String tag, int maximum) {
    String twitterSearch = "https://www.googleapis.com/plus/v1/activities?orderBy=recent&query=" + tag + "&key="
            + Config.PLUS_KEY;/*ww  w .  j  a v a  2s.  c o m*/
    Log.d("SUSEConferences", "Google search: " + twitterSearch);
    ArrayList<SocialItem> socialItems = new ArrayList<SocialItem>();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

    Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.google_icon);

    try {
        JSONObject result = HTTPWrapper.get(twitterSearch);
        JSONArray items = result.getJSONArray("items");
        int len = items.length();
        if ((len > 0) && (maximum > 0) && (len > maximum))
            len = maximum;

        for (int i = 0; i < len; i++) {
            JSONObject jsonItem = items.getJSONObject(i);
            JSONObject actorItem = jsonItem.getJSONObject("actor");
            JSONObject imageItem = actorItem.getJSONObject("image");
            JSONObject objectItem = jsonItem.getJSONObject("object");
            Bitmap image = HTTPWrapper.getImage(imageItem.getString("url"));
            String content = Html.fromHtml(objectItem.getString("content")).toString();
            Date formattedDate = new Date();
            try {
                formattedDate = formatter.parse(jsonItem.getString("published"));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            SocialItem newItem = new SocialItem(SocialItem.GOOGLE, actorItem.getString("displayName"), content,
                    formattedDate,
                    DateUtils
                            .formatDateTime(context, formattedDate.getTime(),
                                    DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_NUMERIC_DATE
                                            | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE),
                    image, icon);
            newItem.setLink(jsonItem.getString("url"));
            socialItems.add(newItem);
        }
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return socialItems;
}

From source file:com.adam.aslfms.util.Util.java

public static String timeFromLocalMillis(Context ctx, long millis) {
    return DateUtils.formatDateTime(ctx, millis,
            DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE);
}

From source file:com.socialdisasters.other.MessageAdapter.java

public static String formatTimeStampString(Context context, long when, boolean fullFormat) {
    Time then = new Time();
    then.set(when);/*from  w ww  .  ja  va  2 s .  co  m*/
    Time now = new Time();
    now.setToNow();

    // Basic settings for formatDateTime() we want for all cases.
    int format_flags = DateUtils.FORMAT_ABBREV_ALL;

    // 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);
    }

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

From source file:org.jraf.android.cinetoday.mobile.app.main.MainActivity.java

private void updateLastUpdateDateLabel() {
    MainPrefs prefs = MainPrefs.get(this);
    Long lastUpdateDate = prefs.getLastUpdateDate();
    if (lastUpdateDate == null) {
        mBinding.txtStatus.setText(R.string.main_lastUpdateDate_none);
    } else {/*from   ww w  .  j av  a2s . co m*/
        String dateStr = DateUtils.formatDateTime(this, lastUpdateDate,
                DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);
        mBinding.txtStatus.setText(getString(R.string.main_lastUpdateDate, dateStr));
    }
}

From source file:com.numenta.core.utils.NotificationUtils.java

/**
 * Format Notification description based on instance name and timestamp
 *
 * @param instanceName/*from  w  w w .j a  v a 2  s  . com*/
 * @param timestamp
 * @return Formatted notification description using
 */
public static String formatSimpleNotificationDescription(String instanceName, long timestamp) {
    // Formatted Notification description:
    // $1=metric.instanceName,
    // $2=timestamp

    // Gets the standard date formatter for the current locale of
    // the device
    String formattedCurrentDate = DateUtils.formatDateTime(HTMApplication.getContext(), timestamp,
            DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);

    return String.format(
            HTMApplication.getContext().getString(R.string.simple_notification_description_template),
            instanceName, formattedCurrentDate);
}

From source file:net.abcdroid.devfest12.ui.MyScheduleFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (getActivity() == null) {
        return;//from w ww . j av a 2s  .  c om
    }

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

    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.isSameDay(previousBlockStart, blockStart)) {
            sections.add(new SimpleSectionedListAdapter.Section(cursor.getPosition(),
                    DateUtils.formatDateTime(getActivity(), blockStart, DateUtils.FORMAT_ABBREV_MONTH
                            | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY)));
        }
        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.changeCursor(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:com.google.android.apps.iosched.ui.MyScheduleFragment.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (getActivity() == null || cursor == null) {
        return;/*from   w w w  .ja  v  a2 s  . c om*/
    }

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

    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.isSameDay(previousBlockStart, blockStart)) {
            sections.add(new SimpleSectionedListAdapter.Section(cursor.getPosition(),
                    DateUtils.formatDateTime(getActivity(), blockStart, DateUtils.FORMAT_ABBREV_MONTH
                            | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY)));
        }
        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.changeCursor(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.level28.android.moca.ui.schedule.SessionDetailFragment.java

/**
 * Update the fragment UI whenever a cursor change happens
 *//*  w  ww .ja v a2  s  .  c o m*/
private void updateUi(final boolean animate) {
    if (!isUsable()) {
        return;
    }

    // Sanity check
    if (mCursor != null && !mCursor.isClosed() && mCursor.moveToFirst()) {
        final CharSequence startTime = DateUtils.formatDateTime(getActivity(),
                mCursor.getLong(SessionDetailQuery.START), DateUtils.FORMAT_SHOW_TIME);
        final CharSequence endTime = DateUtils.formatDateTime(getActivity(),
                mCursor.getLong(SessionDetailQuery.END), DateUtils.FORMAT_SHOW_TIME);

        final String timeSpec = new StringBuilder(startTime).append(" - ").append(endTime).toString();

        mTime.setText(timeSpec);
        // Set the language flag as a compound drawable of mTime.
        // This is a very common layout optimization which shaves a bit of
        // memory and *a lot* of CPU time required to compute the final
        // layout
        final int flagResId = getFlagResId(mCursor.getString(SessionDetailQuery.LANG));
        mTime.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, flagResId);

        mTitle.setText(mCursor.getString(SessionDetailQuery.TITLE));
        mHosts.setText(mCursor.getString(SessionDetailQuery.HOSTS));

        // Check for abstract presence
        if (mCursor.isNull(SessionDetailQuery.ABSTRACT)) {
            // No abstract, hide it
            ViewUtils.setGone(mAbstractContainer, true);
        } else {
            // We have an abstract, display it!
            mAbstract.setText(mCursor.getString(SessionDetailQuery.ABSTRACT));
            mAbstractContainer.scrollTo(0, 0);
            ViewUtils.setGone(mAbstractContainer, false);
        }
        // Display session details
        ViewUtils.setGone(mEmptyText, true);
        ViewUtils.fadeIn(getActivity(), mScheduleContainer, animate);
        ViewUtils.setGone(mScheduleContainer, false);
        mScheduleVisible = true;
    } else if (mScheduleVisible) {
        // Display the "select session" message
        ViewUtils.setGone(mScheduleContainer, true);
        ViewUtils.fadeIn(getActivity(), mEmptyText, animate);
        ViewUtils.setGone(mEmptyText, false);
        mScheduleVisible = false;
    }
}

From source file:org.jraf.android.piclabel.app.form.FormActivity.java

protected ImageInfo extractImageInfo(File file) {
    ImageInfo res = new ImageInfo();
    ExifInterface exifInterface = null;//www.j av  a  2s.  c o m
    try {
        exifInterface = new ExifInterface(file.getPath());
    } catch (IOException e) {
        Log.e(TAG, "extractImageInfo Could not read exif", e);
    }

    // Date
    String dateTimeStr = null;
    if (exifInterface != null)
        dateTimeStr = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
    if (TextUtils.isEmpty(dateTimeStr)) {
        // No date in exif: use 'local' date
        res.dateTime = DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
        res.isLocalDateTime = true;
    } else {
        res.dateTime = parseExifDateTime(dateTimeStr);
        if (res.dateTime == null) {
            // Date in exif could not be parsed: use 'local' date
            DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_DATE
                    | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
            res.isLocalDateTime = true;
        }
    }

    // Location
    float[] latLon = new float[2];
    boolean latLonPresent = exifInterface != null && exifInterface.getLatLong(latLon);
    if (!latLonPresent) {
        // No location in exif: use 'local' location
        res.isLocalLocation = true;
        latLonPresent = getLatestLocalLocation(latLon);
        if (latLonPresent)
            res.location = reverseGeocode(latLon[0], latLon[1]);
    } else {
        res.location = reverseGeocode(latLon[0], latLon[1]);
    }
    if (res.location == null) {
        res.reverseGeocodeProblem = true;
        res.location = "";
    }
    return res;
}

From source file:tw.idv.gasolin.pycontw2012.ui.ScheduleFragment.java

private void setupDay(LayoutInflater inflater, long startMillis) {
    Day day = new Day();

    // Setup data
    day.index = mDays.size();//  w  w  w. j  a v  a 2 s  . c om
    day.timeStart = startMillis;
    day.timeEnd = startMillis + DateUtils.DAY_IN_MILLIS;
    day.blocksUri = CoscupContract.Blocks.buildBlocksBetweenDirUri(day.timeStart, day.timeEnd);

    // Setup views
    day.rootView = (ViewGroup) inflater.inflate(R.layout.blocks_content, null);

    day.scrollView = (ObservableScrollView) day.rootView.findViewById(R.id.blocks_scroll);
    day.scrollView.setOnScrollListener(this);

    day.blocksView = (BlocksLayout) day.rootView.findViewById(R.id.blocks);
    day.nowView = day.rootView.findViewById(R.id.blocks_now);

    day.blocksView.setDrawingCacheEnabled(true);
    day.blocksView.setAlwaysDrawnWithCacheEnabled(true);

    TimeZone.setDefault(UIUtils.CONFERENCE_TIME_ZONE);
    day.label = DateUtils.formatDateTime(getActivity(), startMillis, TIME_FLAGS);

    mWorkspace.addView(day.rootView);
    mDays.add(day);
}