簡體   English   中英

在Android上以自定義格式顯示本地化日期

[英]Displaying localized dates on Android with custom format

我使用了建議的方法,如票證中所述,根據Android上的用戶區域設置格式化日期: 在Android上以本地化格式顯示日期

但是,有什么方法可以適應使用的格式? 就我而言,我希望調整格式以僅顯示年份的2位數字。

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

試試這個代碼

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM