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?
Asked
Active
Viewed 1,729 times
4 Answers
6
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.

Knossos
- 15,802
- 10
- 54
- 91
1
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());

Keyur Lakhani
- 4,321
- 1
- 24
- 35
1
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;
}

Bidhan
- 10,607
- 3
- 39
- 50
-
3Careful, if this gets used in a loop or similar, it has the possibility of creating a lot of SimpleDateFormat objects, when it is preferable to create only one. – Knossos Jun 11 '15 at 10:27
0
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);

Android Dev
- 421
- 8
- 26