Tôi nhận được chuỗi ngày từ SAX phân tích như thế này: Wed, 18 Apr 2012 07:55:29 +0000Thay đổi định dạng ngày tháng trong chuỗi android
Bây giờ, tôi muốn chuỗi này như: Tháng Tư 18, 2012 01:25 PM
Tôi làm cách nào để thực hiện việc này?
Tôi nhận được chuỗi ngày từ SAX phân tích như thế này: Wed, 18 Apr 2012 07:55:29 +0000Thay đổi định dạng ngày tháng trong chuỗi android
Bây giờ, tôi muốn chuỗi này như: Tháng Tư 18, 2012 01:25 PM
Tôi làm cách nào để thực hiện việc này?
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
UPDATE: -
Như trên, Date.parse("Your date string");
bị phản đối.
String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
Date newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);
này sẽ làm điều đó:
public static String formateDateFromstring(String inputFormat, String outputFormat, String inputDate){
Date parsed = null;
String outputDate = "";
SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);
} catch (ParseException e) {
LOGE(TAG, "ParseException - dateFormat");
}
return outputDate;
}
Ví dụ:
String date_before = "1970-01-01";
String date_after = formateDateFromstring("yyyy-MM-dd", "dd, MMM yyyy", date_before);
Output:
date_after = "01, Jan 1970";
Từ tài liệu oficial, trong API mới (18+) bạn nên triển khai điều này:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
String time=sdf.format(new Date());
Tài liệu: http://developer.android.com/reference/java/text/SimpleDateFormat.html
Giải pháp đơn giản nhất là sử dụng DateFormat 's phương pháp getter: Các định dạng đối tượng getDateTimeInstance DateFormat hẹn hò như thế: ngày 31 tháng 12 năm 1969 04:00:00, tuy nhiên có là những zero thêm sau 4:00
Date date = new Date();
String fDate = DateFormat.getDateTimeInstance().format(date);
System.out.println(fDate);
Đây là công trình đối với tôi, sử dụng SimpleDateFormat và Lịch
String originDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat formatIn = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
SimpleDateFormat formatOut = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatIn.parse(originDate));
String newDate = formatOut.format(calendar.getTime());
Tôi đang cố đánh dấu câu trả lời của bạn đã được chấp nhận. Nhưng nó không cho phép tôi ...: P –
nó sẽ cho phép sau 15 phút thử sau 15 phút .. –
Date.parse() không được chấp nhận: ( – Jorgesys