Java android上两个字符串的日期
我有两个日期字符串(使用当前年份):Java android上两个字符串的日期,java,android,date-parsing,Java,Android,Date Parsing,我有两个日期字符串(使用当前年份): a day=“15” a月=“7” 有了这些数据,我想要一个字符串日期,格式如下:7月15日星期二 可能吗 我正在尝试使用此代码,但它不起作用: public void dateFormatMoth(String day, String month){ Date date = null; String newDateStr=""; String dateReceived="day/month"; SimpleDateForm
7月15日星期二
可能吗
我正在尝试使用此代码,但它不起作用:
public void dateFormatMoth(String day, String month){
Date date = null;
String newDateStr="";
String dateReceived="day/month";
SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM");
SimpleDateFormat myFormat = new SimpleDateFormat("EEEE-MM-dd");
try {
reformattedStr = myFormat.format(fromUser.parse(dateReceived));
} catch (ParseException e) {
e.printStackTrace();
}
Log.d(LOG_TAG,"day formatted"+newDateStr);
}
只需稍微更改
myFormat
即可实现此目的MM
将为您提供月份编号,MMMM
将为您提供全名:
public void dateFormatMonth(String day, String month) throws ParseException {
SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM");
SimpleDateFormat myFormat = new SimpleDateFormat("EEEE MMMM dd", Locale.US);
try {
reformattedStr = myFormat.format(fromUser.parse(day + "/" + month));
} catch (ParseException e) {
e.printStackTrace();
}
Log.d(LOG_TAG,"day formatted"+newDateStr);
}
您可以在中阅读有关不同格式的更多信息
请注意,我明确指定了区域设置,以确保无论计算机的区域设置如何,都能获得所请求的输出。请尝试:
public static String format(String input_date, String input_format, String output_format) {
SimpleDateFormat simple_date_input_format;
SimpleDateFormat simple_date_output_format;
simple_date_input_format = new SimpleDateFormat(input_format, Locale.US);
simple_date_output_format = new SimpleDateFormat(output_format, Locale.US);
try {
Date input_date_formatted = simple_date_input_format.parse(input_date);
output_date_formatted = simple_date_output_format.format(input_date_formatted);
} catch (ParseException e) {
output_date_formatted = null;
e.printStackTrace();
}
return output_date_formatted;
您的日志语句记录了
newDateStr
,除了空字符串,您从未将其设置为任何值。您肯定是忘记了解析date@user1071777当然