spykeburn spykeburn - 1 year ago 74
Java Question

Can't convert int month to month name android java

I am trying to convert int month to month name(string). I am using simple date format but I get only "Jan" for all months. why is this happening?

public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

String sdf = new SimpleDateFormat("LLL", Locale.getDefault()).format(monthOfYear);

String date = +dayOfMonth+" "+(sdf);

Answer Source

I get only "Jan" for all months

Because String sdf = new SimpleDateFormat("LLL", Locale.getDefault()).format(monthOfYear); here, monthOfYear is Number Object so, DateFormate class will convert it to Date object from that number, which will be between 1 Jan, 1970 to 12 Jan, 1970 so you are always getting Jan for all month.


Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, monthOfYear);
sdf  = new SimpleDateFormat("LLL", Locale.getDefault()).format(calendar.getTime());


sdf = new DateFormatSymbols().getShortMonths()[monthOfYear];

DateFormat code snippet:

     * Formats the specified object as a string using the pattern of this date
     * format and appends the string to the specified string buffer.
     * <p>
     * If the {@code field} member of {@code field} contains a value specifying
     * a format field, then its {@code beginIndex} and {@code endIndex} members
     * will be updated with the position of the first occurrence of this field
     * in the formatted text.
     * @param object
     *            the source object to format, must be a {@code Date} or a
     *            {@code Number}. If {@code object} is a number then a date is
     *            constructed using the {@code longValue()} of the number.
     * @param buffer
     *            the target string buffer to append the formatted date/time to.
     * @param field
     *            on input: an optional alignment field; on output: the offsets
     *            of the alignment field in the formatted text.
     * @return the string buffer.
     * @throws IllegalArgumentException
     *            if {@code object} is neither a {@code Date} nor a
     *            {@code Number} instance.
    public final StringBuffer format(Object object, StringBuffer buffer, FieldPosition field) {
        if (object instanceof Date) {
            return format((Date) object, buffer, field);
        if (object instanceof Number) {
            return format(new Date(((Number) object).longValue()), buffer, field);
        throw new IllegalArgumentException("Bad class: " + object.getClass());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download