gpt4 book ai didi

java - 以特定格式打印java日期

转载 作者:行者123 更新时间:2023-12-01 18:35:32 25 4
gpt4 key购买 nike

嗨,我想在 android textview 中以“4 月 23 日星期一”的格式打印日期,我也想打印前几天的日期。例如:“4 月 22 日星期日”、“4 月 21 日星期六”。我发现了很多,但无法得到任何可以以相同格式工作的东西。提前致谢。

最佳答案

看看: SimpleDateFormat

PS:回答乔恩·斯基特的问题:

private static final String DATE_FORMAT_ND  = "EEE d'nd' MMMM";
private static final String DATE_FORMAT_RD = "EEE d'rd' MMMM";
private static final String DATE_FORMAT_ST = "EEE d'st' MMMM";
private static final String DATE_FORMAT_TH = "EEE d'th' MMMM";
private static final String DATE_DAY_NUMBER = "d";

public static String FormatDate( Date date_ )
{
String format = null;
SimpleDateFormat dayNumberFormat = new SimpleDateFormat( DATE_DAY_NUMBER );
String daySource = dayNumberFormat.format( date_ );
int day = Integer.parseInt( daySource );

switch( day )
{
case 1:
case 21:
case 31:
format = DATE_FORMAT_ST;
break;

case 2:
case 22:
format = DATE_FORMAT_ND;
break;

case 3:
case 23:
format = DATE_FORMAT_RD;
break;

case 4:
case 24:
case 5:
case 25:
case 6:
case 26:
case 7:
case 27:
case 8:
case 28:
case 9:
case 29:
case 10:
case 30:
format = DATE_FORMAT_TH;
break;

}

SimpleDateFormat dateFormat = new SimpleDateFormat( format );
String result = dateFormat.format( date_ );

return result;

}

只有简单的日期格式和一点点编码......

Call it like this for yesterdays date :- date.setText(FormatDate(new Date(new Date().getTime()-86400000)));

关于java - 以特定格式打印java日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22190690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com