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
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";
The modern answer to this question is overdue. When this question was asked in 2012, using SimpleDateFormat
and/or DateFormat
as the other answers do, was right even though those classes had always been troublesome. They were replaced just a couple of years later by java.time, the modern Java date and time API, which I frankly find much nicer to work with. So so I am doing.
I suggest you separate your conversion into two operations. In you program don’t keep date and time as a string. Keep them as a proper date-time object. So when parsing from SAX also parse the date-time string into an OffsetDateTime
immediately. When at a later point you need to show the date and time to the user (or transmit it to another system), convert it into the user’s time zone and format it into an appropriate string for that purpose.
Parse into an OffsetDateTime
The string you got conforms with RFC 1123 format. java.time has a built-in formatter for that, which is good because it saves us from constructing our own formatter.
String stringFromSaxParsing = "Wed, 18 Apr 2012 07:55:29 +0000";
OffsetDateTime dateTime = OffsetDateTime.parse(
stringFromSaxParsing, DateTimeFormatter.RFC_1123_DATE_TIME);
System.out.println(dateTime);
Output from this snippet is:
2012-04-18T07:55:29Z
Convert to user’s time zone and format
DateTimeFormatter wantedFormatter = DateTimeFormatter.ofPattern("MMM dd, uuuu hh:mm a", Locale.ENGLISH);
String formatted = dateTime.atZoneSameInstant(ZoneId.systemDefault())
.format(wantedFormatter);
System.out.println(formatted);
I ran this in Asia/Colombo time zone and got the desired:
Apr 18, 2012 01:25 PM
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).The easiest solution is to use DateFormat's getter methods: The getDateTimeInstance DateFormat object formats date like that: Dec 31, 1969 4:00:00 PM, however there are those extra zeroes after 4:00
Date date = new Date();
String fDate = DateFormat.getDateTimeInstance().format(date);
System.out.println(fDate);
This is works for me, using SimpleDateFormat and Calendar
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());
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);
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