I am getting date string from SAX parsing like this: Wed, 18 Apr 2012 07:55:29 +0000
Now, I want this string as : Apr 18, 2012 01:25 PM
How can I do this?
First Create a Calendar object using your Date object. Then build a String using date, year, month and etc you need. then you can use it. You can get data using get() method in Calendar class.
Convert date to different format with Format CellsSelect the dates you want to convert, right click to select Format Cells from context menu. 2. In the Format Cells dialog, under Number tab, select Date from Category list, and then select one format you want to convert to from the right section.
yyyy HH:mm"); String output = formatter. format(localDateTime); If this does not work with api21, you can use: SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); SimpleDateFormat formatter = new SimpleDateFormat("dd.
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
UPDATE :-
As on, Date.parse("Your date string");
is deprecated.
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);
This will do it:
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;
}
Example:
String date_before = "1970-01-01";
String date_after = formateDateFromstring("yyyy-MM-dd", "dd, MMM yyyy", date_before);
Output:
date_after = "01, Jan 1970";
From oficial documentation, in the new API (18+) you should be implement this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
String time=sdf.format(new Date());
Documentation: http://developer.android.com/reference/java/text/SimpleDateFormat.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With