Reputation: 3086
I'm trying to display date which I already have to a new format, i'm using SimpleDateFormat
for this.
Android Code
String date = "2013-08-11 20:38 EDT";
SimpleDateFormat sf = new SimpleDateFormat("dd MMMM yyyy hh:mm a");
try {
newDate = sf.format(new SimpleDateFormat("yyyy-MM-dd kk:mm z").parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
It displays : 11 August 2013 08:38 PM
However if I run the same code in JAVA
(as a normal JAVA console application)
JAVA
String date = "2013-08-11 20:38 EDT";
SimpleDateFormat sf = new SimpleDateFormat("dd MMMM yyyy hh:mm a");
String lDate = sf.format(new SimpleDateFormat("yyyy-MM-dd kk:mm z").parse(date));
System.out.println(lDate);
It displays : 12 August 2013 06:08 AM
This is the format which I need to display.
P.S. : I got a warning in android saying
To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.
so I tried adding Locale.US
to SimpleDateFormat
it again show the 11 August 2013 08:38 PM and not 12 August 2013 06:08 AM
My Question is : how to display date as 12 August 2013 06:08 AM in android.
Upvotes: 1
Views: 1654
Reputation: 21452
try this code
SimpleDateFormat simple = new SimpleDateFormat("dd MMMM yyyy hh:mm a" , java.util.Locale.getDefault());
Upvotes: 0
Reputation: 1
new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.S", Locale.US)
You should specify the time zone
Upvotes: 0
Reputation: 152787
Your local time IST is UTC+05:30, your Android time EDT is UTC-04:00. Together they add to 9h30min of difference explaining the difference in output.
Set your Android device to IST timezone to get the same output.
Alternatively, you can call setTimeZone()
on the DateFormat
to explicitly set a timezone to use.
It is also helpful to explicitly print timezone information to make datetime stamps less ambiguous.
Upvotes: 2