0

I used the proposed way to format dates according to the user locale on Android as described in this ticket: Displaying dates in localized format on Android

However, is there any way to adapt the used format? In my case I wanted to adapt the formatting to show only 2 digits for the year.

//displays 4 digits for year
String dateF = android.text.format.DateFormat.getDateFormat(context).format(date);
Community
  • 1
  • 1
Display name
  • 2,697
  • 2
  • 31
  • 49

2 Answers2

0

Try this code

Calendar cal = new GregorianCalendar(2013, 11, 20);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String date = df.format(cal.getTime());
Elango
  • 412
  • 4
  • 24
0

Using the SimpleDateFormat

String dateString=null;
SimpleDateFormat originalFormat = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
try
{
    Date parsedDate  = originalFormat.parse(dateF);
    SimpleDateFormat  targetFormat = new SimpleDateFormat("dd-MM-yy",Locale.ENGLISH);
    dateString = targetFormat.format(parsedDate);
}
catch(Exception e)
{
    Log.e(TAG, "error while parsing");
}
Log.d(TAG, "converted date "+dateString);
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93