Example usage for java.util Calendar LONG

List of usage examples for java.util Calendar LONG

Introduction

In this page you can find the example usage for java.util Calendar LONG.

Prototype

int LONG

To view the source code for java.util Calendar LONG.

Click Source Link

Document

A style specifier for #getDisplayName(int,int,Locale) getDisplayName and #getDisplayNames(int,int,Locale) getDisplayNames equivalent to #LONG_FORMAT .

Usage

From source file:com.antsdb.saltedfish.sql.vdm.FuncDateFormat.java

private String format(String format, Timestamp time) {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < format.length(); i++) {
        char ch = format.charAt(i);
        if (ch != '%') {
            buf.append(ch);//from w w w. j av  a  2s.c o m
            continue;
        }
        if (i >= (format.length() - 1)) {
            buf.append(ch);
            continue;
        }
        char specifier = format.charAt(++i);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time.getTime());
        if (specifier == 'a') {
            throw new NotImplementedException();
        } else if (specifier == 'b') {
            throw new NotImplementedException();
        } else if (specifier == 'c') {
            buf.append(calendar.get(Calendar.MONTH + 1));
        } else if (specifier == 'd') {
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            if (day < 10) {
                buf.append('0');
            }
            buf.append(day);
        } else if (specifier == 'D') {
            throw new NotImplementedException();
        } else if (specifier == 'e') {
            buf.append(calendar.get(Calendar.DAY_OF_MONTH));
        } else if (specifier == 'f') {
            buf.append(calendar.get(Calendar.MILLISECOND * 1000));
        } else if (specifier == 'H') {
            buf.append(calendar.get(Calendar.HOUR));
        } else if (specifier == 'h') {
            buf.append(calendar.get(Calendar.HOUR) % 13);
        } else if (specifier == 'i') {
            buf.append(calendar.get(Calendar.MINUTE));
        } else if (specifier == 'I') {
            buf.append(calendar.get(Calendar.HOUR) % 13);
        } else if (specifier == 'j') {
            buf.append(calendar.get(Calendar.DAY_OF_YEAR));
        } else if (specifier == 'k') {
            buf.append(calendar.get(Calendar.HOUR));
        } else if (specifier == 'l') {
            buf.append(calendar.get(Calendar.HOUR) % 13);
        } else if (specifier == 'm') {
            int month = calendar.get(Calendar.MONTH) + 1;
            if (month < 10) {
                buf.append('0');
            }
            buf.append(calendar.get(Calendar.MONTH) + 1);
        } else if (specifier == 'M') {
            buf.append(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()));
        } else if (specifier == 'p') {
            int hour = calendar.get(Calendar.HOUR);
            buf.append(hour < 12 ? "AM" : "PM");
        } else if (specifier == 'r') {
            int hour = calendar.get(Calendar.HOUR);
            hour = hour % 13;
            if (hour < 10) {
                buf.append('0');
            }
            buf.append(hour);
            buf.append(':');
            int minute = calendar.get(Calendar.MINUTE);
            if (minute < 10) {
                buf.append('0');
            }
            buf.append(minute);
            buf.append(':');
            int second = calendar.get(Calendar.SECOND);
            if (second < 10) {
                buf.append('0');
            }
            buf.append(second);
            buf.append(hour < 12 ? " AM" : " PM");
        } else if (specifier == 's') {
            buf.append(calendar.get(Calendar.SECOND));
        } else if (specifier == 'S') {
            buf.append(calendar.get(Calendar.SECOND));
        } else if (specifier == 'T') {
            throw new NotImplementedException();
        } else if (specifier == 'u') {
            buf.append(calendar.get(Calendar.WEEK_OF_YEAR));
        } else if (specifier == 'U') {
            throw new NotImplementedException();
        } else if (specifier == 'v') {
            throw new NotImplementedException();
        } else if (specifier == 'V') {
            throw new NotImplementedException();
        } else if (specifier == 'w') {
            throw new NotImplementedException();
        } else if (specifier == 'W') {
            throw new NotImplementedException();
        } else if (specifier == 'x') {
            throw new NotImplementedException();
        } else if (specifier == 'X') {
            throw new NotImplementedException();
        } else if (specifier == 'y') {
            buf.append(calendar.get(Calendar.YEAR) % 100);
        } else if (specifier == 'Y') {
            buf.append(calendar.get(Calendar.YEAR));
        } else if (specifier == '%') {
            buf.append('%');
        } else {
            buf.append(specifier);
        }
    }
    return buf.toString();
}

From source file:org.aksw.simba.bengal.triple2nl.converter.LiteralConverter.java

private String convertDateLiteral(LiteralLabel lit) {
    RDFDatatype dt = lit.getDatatype();/*w  ww  .  ja va2 s .c o  m*/
    String s = lit.getLexicalForm();

    try {
        Object value = lit.getValue();
        if (value instanceof XSDDateTime) {
            Calendar calendar = ((XSDDateTime) value).asCalendar();
            if (dt.equals(XSDDatatype.XSDgMonth)) {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL);
            } else if (dt.equals(XSDDatatype.XSDgMonthDay)) {
                s = calendar.get(Calendar.DAY_OF_MONTH)
                        + calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL);
            } else if (dt.equals(XSDDatatype.XSDgYearMonth)) {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL) + " "
                        + calendar.get(Calendar.YEAR);
            } else if (dt.equals(XSDDatatype.XSDgYear)) {
                s = "" + calendar.get(Calendar.YEAR);
            } else {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL) + " "
                        + calendar.get(Calendar.DAY_OF_MONTH) + ", " + calendar.get(Calendar.YEAR);
                // dateFormat.format(calendar.getTime());
            }
        }
    } catch (DatatypeFormatException | IllegalDateTimeFieldException e) {
        logger.error("Conversion of date literal " + lit + " failed. Reason: " + e.getMessage());
        // fallback
        // DateTime time = ISODateTimeFormat.dateTime
        DateTime time;
        try {
            time = ISODateTimeFormat.dateTimeParser().parseDateTime(lit.getLexicalForm());
            s = time.toString("MMMM dd, yyyy");
        } catch (Exception e1) {
            try {
                time = ISODateTimeFormat.localDateParser().parseDateTime(lit.getLexicalForm());
                s = time.toString("MMMM dd, yyyy");
            } catch (Exception e2) {
                e2.printStackTrace();
                time = ISODateTimeFormat.dateParser().parseDateTime(lit.getLexicalForm());
                s = time.toString("MMMM dd, yyyy");
            }
        }
    }
    return s;
}

From source file:br.msf.commons.util.AbstractDateUtils.java

/**
 * Returns the month name for the given date.
 *
 * @param date   The date to be evaluated.
 * @param locale The desired locale./* w w w.  ja v  a 2  s.  com*/
 * @return The month name for the given date.
 */
public static String monthName(final Object date, final Locale locale) {
    ArgumentUtils.rejectIfNull(date);
    return castToCalendar(date).getDisplayName(Calendar.MONTH, Calendar.LONG,
            LocaleUtils.getNullSafeLocale(locale));
}

From source file:br.msf.commons.util.AbstractDateUtils.java

/**
 * Returns the month name for the given month index.
 * <p/>/* ww w  .  j a  va  2  s.  c  om*/
 * Beware! its zero indexed, so JANUARY = 0, FEBRUARY = 1 and so on...
 *
 * @param month  The month index.
 * @param locale The desired locale.
 * @return The month name for the given index.
 */
public static String monthName(final int month, final Locale locale) {
    if (month < Calendar.JANUARY || month > Calendar.DECEMBER) {
        throw new IllegalArgumentException("Month must be between [Calendar.JANUARY, Calendar.DECEMBER].");
    }
    return (new GregorianCalendar(year(), month, 1)).getDisplayName(Calendar.MONTH, Calendar.LONG,
            LocaleUtils.getNullSafeLocale(locale));
}

From source file:net.menthor.editor.v2.util.Util.java

public static String getCompilationDateMessage() {
    DateFormat dateFormat = new SimpleDateFormat("d, yyyy");
    Date date = new Date();
    Calendar c = Calendar.getInstance();
    return c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + " " + dateFormat.format(date);
}

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
 *//*ww w  .ja va  2  s  .  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.codesourcery.eve.skills.ui.utils.CalendarWidget.java

private String getWeekDayName(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/*from   w w  w .  j av  a  2  s  .  c o m*/
    return calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
}

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.
 *//*ww  w .  java 2 s  .  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 "";
        }
    }

}

From source file:com.hacktx.android.activities.EventDetailActivity.java

private void setupEventDetails() {
    int eventIconId;
    switch (event.getType()) {
    case TALK:/*from   w  ww  .  ja v a  2 s.co  m*/
        eventIconId = R.drawable.ic_event_talk;
        break;
    case EDUCATION:
        eventIconId = R.drawable.ic_event_education;
        break;
    case BUS:
        eventIconId = R.drawable.ic_event_bus;
        break;
    case FOOD:
        eventIconId = R.drawable.ic_event_food;
        break;
    case DEV:
        eventIconId = R.drawable.ic_event_dev;
        break;
    default:
        eventIconId = R.drawable.ic_event_default;
        break;
    }

    ImageView eventIcon = (ImageView) findViewById(R.id.eventIcon);
    eventIcon.setImageResource(eventIconId);

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    Calendar start = Calendar.getInstance();

    try {
        start.setTime(formatter.parse(event.getStartDate()));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    ((TextView) findViewById(R.id.eventLocation)).setText(event.getLocation().getLocationDetails());
    ((TextView) findViewById(R.id.eventDateTime))
            .setText(start.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()) + " "
                    + start.get(Calendar.DAY_OF_MONTH) + " | " + event.getEventTimes());
}

From source file:com.android.datetimepicker.date.DatePickerDialog.java

private void updateDisplay(boolean announce) {
    if (mDayOfWeekView != null) {
        mDayOfWeekView// w w w  .jav  a 2 s.c o m
                .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()));
    mSelectedDayTextView.setText(DAY_FORMAT.format(mCalendar.getTime()));
    mYearView.setText(YEAR_FORMAT.format(mCalendar.getTime()));

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

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