我想以月和日期格式顯示日期(例如:5月26日)。但是當我嘗試使用它時顯示無法解析方法。如何在android環境中格式化日期
String dateStr = DateFormat.format("MMM dd", cal.getTime().getTime()).toString();
我想以月和日期格式顯示日期(例如:5月26日)。但是當我嘗試使用它時顯示無法解析方法。如何在android環境中格式化日期
String dateStr = DateFormat.format("MMM dd", cal.getTime().getTime()).toString();
您可以使用SimpleDateFormat
。
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd", Locale.US);
Date date = new Date(cal.getTimeInMillis());
String dateStr = dateFormat.format(date);
嘗試
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd");
Date date = sdf.parse(cal.getTime().getTime());
String dateStr = sdf.parse(date);
試試這個
SimpleDateFormat format = new SimpleDateFormat("MMM dd");
String date = format.format(Date.parse(DateFormat.getDateTimeInstance().format(new Date())));
下面是一些示例代碼:
DateFormat.getDateInstance().format(new Date(0));
DateFormat.getDateTimeInstance().format(new Date(0));
DateFormat.getTimeInstance().format(new Date(0));
以上三條線路將爲:
Dec 31, 1969
Dec 31, 1969 4:00:00 PM
4:00:00 PM 12:00:00 AM
您可以使用SimpleDateFormat。
String dat = "2017-01-29 09:49:22"
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd");
Date date = simpleDateFormat.parse(dat);
String dateString = simpleDateFormat.format(date);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date convertedDate = format.parse(selectedDate);
Calendar cal = Calendar.getInstance();
cal.setTime(convertedDate);
dateSelection.setText(String.format(Locale.US, "%1$td %1$tB, %1$tY", cal));
output: 9 May, 2017