In android, Is it possible to format a date object to mm-dd-yy? If yes, then how?

1.7k views Asked by At

I have to display the date in dd-mm-yy format as per the UI design for our Android app. can any one help me, how to achieve it?

4

There are 4 answers

0
Knossos On BEST ANSWER

Use SimpleDateFormat:

http://developer.android.com/reference/java/text/SimpleDateFormat.html

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
Log.i("DATE", sdf.format(new Date()));

You should store your instance of sdf, if you are planning to be formatting your dates repeatedly. Outside a loop, or as a class member variable, for example.

0
Android Dev On

try this code , it may help you

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

    String abs= m3_DateDisplay.getText().toString();
                Date testDate = null;
                 try {
                    testDate = sdf.parse(abs);
                } catch (ParseException e) {

                    e.printStackTrace();
                }
                 SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
                String dateValue=formatter.format(testDate);
0
Keyur Lakhani On

You can done it by SimpleDateFormat

For Example,

android.text.format.DateFormat dateformat = new android.text.format.DateFormat();
dateformat.format("MM-dd-yyyy hh:mm:ss", new java.util.Date());

or

android.text.format.DateFormat.format("MM-dd-yyyy hh:mm:ss", new java.util.Date());
1
Bidhan On

You can change your date object to MM-ddy-yy using SimpleDateFormat. You can create a helper method like the following

public String formatDate(Date date) {
    DateFormat formatter = new SimpleDateFormat("MM-dd-yy");
    String formattedDate = formatter.format(date);
    return formattedDate;
}