使用自定义格式在Android上显示本地化日期

时间:2014-08-14 07:47:18

标签: java android date-formatting

我使用建议的方式根据Android上的用户区域设置格式化日期,如此票证中所述:Displaying dates in localized format on Android

但是,有没有办法调整使用的格式?在我的情况下,我想调整格式,以显示一年中只有2位数。

//displays 4 digits for year
String dateF = android.text.format.DateFormat.getDateFormat(context).format(date);

2 个答案:

答案 0 :(得分:0)

试试此代码

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

答案 1 :(得分:0)

使用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);