「6日7日…など」で日を取得したい 日付文字列の。
SimpleDateFormaterを試してみました & DateFormatSymbolsも試してみました。String Required を取得していません。
回避策はありますか?
「6日7日…など」で日を取得したい 日付文字列の。
SimpleDateFormaterを試してみました & DateFormatSymbolsも試してみました。String Required を取得していません。
回避策はありますか?
SimpleDateFormat format = new SimpleDateFormat("d");
String date = format.format(new Date());
if(date.endsWith("1") && !date.endsWith("11"))
format = new SimpleDateFormat("EE MMM d'st', yyyy");
else if(date.endsWith("2") && !date.endsWith("12"))
format = new SimpleDateFormat("EE MMM d'nd', yyyy");
else if(date.endsWith("3") && !date.endsWith("13"))
format = new SimpleDateFormat("EE MMM d'rd', yyyy");
else
format = new SimpleDateFormat("EE MMM d'th', yyyy");
String yourDate = format.format(new Date());
これを試してみてください。これは静的なもののように見えますが、正常に動作します...
どうぞ:
/**
* Converts Date object into string format as for e.g. <b>April 25th, 2012</b>
* @param date date object
* @return string format of provided date object
*/
public static String getCustomDateString(Date date){
SimpleDateFormat tmp = new SimpleDateFormat("MMMM d");
String str = tmp.format(date);
str = str.substring(0, 1).toUpperCase() + str.substring(1);
if(date.getDate()>10 && date.getDate()<14)
str = str + "th, ";
else{
if(str.endsWith("1")) str = str + "st, ";
else if(str.endsWith("2")) str = str + "nd, ";
else if(str.endsWith("3")) str = str + "rd, ";
else str = str + "th, ";
}
tmp = new SimpleDateFormat("yyyy");
str = str + tmp.format(date);
return str;
}
サンプル:
Log.i("myDate", getCustomDateString(new Date()));
2012 年 4 月 25 日
次のメソッドを使用して、渡された日付のフォーマットされた文字列を取得できます。JavaでSimpleDateFormatを使用して、日付を1日、2日、3日、4日とフォーマットします。例: - 2015 年 9 月 1 日
public String getFormattedDate(Date date){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
//2nd of march 2015
int day=cal.get(Calendar.DATE);
switch (day % 10) {
case 1:
return new SimpleDateFormat("MMMM d'st', yyyy").format(date);
case 2:
return new SimpleDateFormat("MMMM d'nd', yyyy").format(date);
case 3:
return new SimpleDateFormat("MMMM d'rd', yyyy").format(date);
default:
return new SimpleDateFormat("MMMM d'th', yyyy").format(date);
}
SimpleDateFormatをサブクラス化し、フォーマットをオーバーライドし、String または Integer を受け取り、"nd" または "st" が付加された String を返す単純なユーティリティ関数を使用できます...次のようなものです。
if (initialDate.equals("2") || initialDate.equals("22"){
return initialDate += "nd";
}else if {initialDate.equals("3") || initialDate.equals("23"){
return initialDate += "rd";
}else{
return initialDate += "th";
}