Example usage for java.util GregorianCalendar getDisplayName

List of usage examples for java.util GregorianCalendar getDisplayName

Introduction

In this page you can find the example usage for java.util GregorianCalendar getDisplayName.

Prototype

public String getDisplayName(int field, int style, Locale locale) 

Source Link

Document

Returns the string representation of the calendar field value in the given style and locale.

Usage

From source file:net.kw.shrdlu.grtgtfs.Activities.RiderAlertsActivity.java

/**
 * Get the latest twitter info. Some of this copied from http://www.vogella.com/articles/AndroidJSON/article.html
 *///  www  .  j av a2s .  c  o m
ArrayList<String[]> readTwitterFeed() {
    final StringBuilder builder = new StringBuilder();
    final HttpClient client = new DefaultHttpClient();
    final HttpGet httpGet = new HttpGet(TwitterURL + TwitterQry);
    httpGet.setHeader("Authorization", "Bearer " + AccessToken);

    try {
        final HttpResponse response = client.execute(httpGet);

        final StatusLine statusLine = response.getStatusLine();
        final int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            final HttpEntity entity = response.getEntity();
            final InputStream content = entity.getContent();
            final BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            reader.close();
            content.close();

            // Result is an array of tweets
            final JSONArray arr = (JSONArray) new JSONTokener(builder.toString()).nextValue();

            final ArrayList<String[]> tweets = new ArrayList<>();

            // Need to grok dates of form "created_at": "Thu, 15 Nov 2012 18:27:17 +0000"
            final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy");
            dateFormat.setLenient(true);

            for (int i = 0; i < arr.length(); i++) {
                final String text = arr.getJSONObject(i).get("text").toString();
                String tweettime = arr.getJSONObject(i).get("created_at").toString();

                // Extract & reformat the date
                Date created;
                final GregorianCalendar cal = new GregorianCalendar();
                try {
                    created = dateFormat.parse(tweettime);
                    cal.setTime(created);
                    String day, mon;

                    day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
                    mon = cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());

                    tweettime = String.format("%s %02d:%02d - %s %d", day, cal.get(Calendar.HOUR_OF_DAY),
                            cal.get(Calendar.MINUTE), mon, cal.get(Calendar.DAY_OF_MONTH));

                } catch (final Exception e) {
                    Log.d(TAG, "Exception: " + e.getMessage() + ", parsing tweet date `" + tweettime + "'");
                    tweettime = "--:--";
                }

                tweets.add(new String[] { tweettime, text });
            }

            return tweets;

        } else {
            Log.d(TAG, "Failed to download twitter info");
        }
    } catch (final ClientProtocolException e) {
        Log.d(TAG, "ClientProtocolException: " + e.getMessage() + ", Failed to download twitter info");
    } catch (final IOException e) {
        Log.d(TAG, "IOException: " + e.getMessage() + ", Failed to download twitter info");
    } catch (final Exception e) {
        Log.d(TAG, "Exception: " + e.getMessage() + ", Failed to download twitter info");
    }

    return null;
}

From source file:de.sindzinski.wetter.util.Utility.java

/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 *
 * @param context      Context to use for resource localization
 * @param timeInMillis The date in milliseconds
 * @return a user-friendly representation of the date.
 *///from w w w . ja  v  a 2s  . c  o m
@SuppressLint("StringFormatMatches")
public static String getDailyDayString(Context context, long timeInMillis, String timeZoneId) {
    // The day string for forecast uses the following logic:
    // For today: "Today, June 8"
    // For tomorrow:  "Tomorrow"
    // For the next 5 days: "Wednesday
    // " (just the day name)
    // For all days after that: "Mon Jun 8"
    //        Log.d("test timezone", timeZoneId);
    TimeZone timeZone;
    if (timeZoneId.equals("")) {
        timeZone = TimeZone.getDefault();
    } else {
        timeZone = TimeZone.getTimeZone(timeZoneId);
    }
    GregorianCalendar gregorianCalendar = new GregorianCalendar(timeZone);
    gregorianCalendar.setTimeInMillis(timeInMillis);
    //        int day = gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH);

    //code for formatting the date
    Calendar calendar = Calendar.getInstance();
    int today = calendar.get(Calendar.DAY_OF_MONTH);
    calendar.setTimeInMillis(timeInMillis);
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    // If the date we're building the String for is today's date, the format
    // is "Today, June 24"

    if (today == day) {
        int formatId = R.string.format_full_friendly_date;
        SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("dd.MMM");
        shortenedDateFormat.setTimeZone(timeZone);

        return String.format(context.getString(formatId, "Today ",
                gregorianCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()),
                shortenedDateFormat.format(timeInMillis)));
    } else if (day < today + 7) {
        // If the input date is less than a week in the future, just return the day name.
        String dayName = gregorianCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG,
                Locale.getDefault());//Locale.US);
        return dayName;
    } else {
        try {
            SimpleDateFormat shortDateFormat = new SimpleDateFormat("EEE MMM dd");
            shortDateFormat.setTimeZone(timeZone);
            String dsorig = shortDateFormat.format(timeInMillis);
            Date dconv = shortDateFormat.parse(dsorig);
            String sconv = shortDateFormat.format(dconv);
            return sconv;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}