Convert date value in yyyy-MM-dd HH:mm:ss format to Month And Day - Android java.util

Android examples for java.util:Day

Description

Convert date value in yyyy-MM-dd HH:mm:ss format to Month And Day

Demo Code

import android.annotation.SuppressLint;
import android.widget.TextView;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{

    public static String getMonthAndDay(String dateStr) {
        try {//  w  w  w . j  av a  2s .c o m
            Calendar c = Calendar.getInstance();
            c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .parse(dateStr));
            return String.format("%02d", c.get(Calendar.MONTH) + 1) + "-"
                    + c.get(Calendar.DAY_OF_MONTH) + " "
                    + String.format("%02d", c.get(Calendar.HOUR_OF_DAY))
                    + ":" + String.format("%02d", c.get(Calendar.MINUTE));
        } catch (ParseException e) {
            return getMonthAndDay(getDate());
        }
    }

}

Related Tutorials