SimpleDateFormat with Timezone names in different languages - android

I have encountered this strange problem when trying to parse date strings, which I have nailed down to this:
Device with some English locale (the emulator):
Date.toString() gives "Thu Feb 18 13:25:22 GMT 2016"
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
d1 = newDateFormat.parse("Thu Feb 18 13:25:22 GMT 2016"); //works fine
Device with German locale:
Date.toString() gives "Thu Feb 18 13:25:22 MEZ 2016" (note the timezone in German, while Thursday is still Thursday)
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
d1 = newDateFormat.parse("Thu Feb 18 13:25:22 MEZ 2016"); //does not parse
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.GERMAN);
d1 = newDateFormat.parse("Thu Feb 18 13:25:22 MEZ 2016"); //does not parse
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
String s = "Thu Feb 18 13:25:22 MEZ 2016";
s.replace("MEZ", "CET");
d1 = newDateFormat.parse(s); //works fine, but doesn't seem very portable
So I am looking for a way to either get SimpleDateFormat to ignore the time zone completely (that would be fine for my app, the only thing I am trying to do here is give a time difference to "last time", which will be given in hours if it's less than a day, but in days or weeks if it's more, so the time zone won't matter that much for this use case),
or for a way to get SimpleDateFormat to understand timezones in the device language.
Bonus points for a good way to save the date in a language-independent way, while still retaining information such as timezone and time of day in that time zone. (for when I upgrade my database next time)

I did it in this way to get the current date in YYYY-MM-DD format.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String CurrentDate = formatter.format(new Date());
Date current_date = formatter.parse(CurrentDate); //In case need the entire Date value
Hope this helps!

To get your desired result (which i believe looks something like: Mi. März 15:15:58 02-03-16 MEZ)
I used to the following code:
public static final String DATE_FORMAT = "EEE MMM HH:mm:ss dd-MM-yy z";
private DateTimeFormatter mDateFormatter = DateTimeFormat.forPattern(DATE_FORMAT).withLocale(Locale.GERMAN);
/**
* Get formatted Date as Human Readable String
* #param dateTime the time to print
* #return Date as String
*/
public String getFormattedDate(#NonNull DateTime dateTime) {
return mDateFormatter.print(dateTime);
}
If you want a Time difference, you might wanna take look at JodaTime (http://www.joda.org/joda-time/). I've used it myself, it provides simple interfaces for time differences (Days, Hours, Minutes, Months, Seconds, Weeks, Years).
Save your Dates in Unix Time (ISO 8601), its all the information you need.

When you are instantiating SimpleDateFormat,
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat has a constructor that takes in a locale, SimpleDateFormat(String) constructor has been deprecated now, you should use the constructor that takes in the locale too like so,
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.getDefault());
Now if you use this df.parse(myDateString) you don't need to do string replace from MEZ to CET because the SimpleDateFormat knows your locale will parse the date properly

Related

Cannot convert String date into Date - Unparseable date error

I have date String in the following format - eg.:
Thu, 17 Mar 2016 19:30:25 +0000
Sun, 06 Mar 2016 12:43:13 +0000
I want to convert this date into a more readable format:
Thu, 17 Mar 2016
Sun, 06 Mar 2016
public static String getMoreReadableDateFormat(String dateStringToConvert) {
SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
Date convertedDate;
try {
convertedDate = dateFormat.parse(dateStringToConvert);
} catch (ParseException e) {
// could not convert date, return the initial form
return dateStringToConvert;
}
String formattedDate = new SimpleDateFormat("E, dd MMM yyyy").format(convertedDate);
return formattedDate;
}
Strange is that this code works fine for me (I get the simplified date version), but for some other peoples from other countries is not working and cannot convert date string into the simplified version. I know it has to be related to Locale, but don't know how to fix this.
You can try to set your phone's date format to french, or something else to reproduce the behavior you mentioned.
Are you sure that dateStringToConvert parameter is always in a correct format ?
I would suggest you change the first line to:
SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
It's important to note here the difference between DateFormats like Date, Time, and Date and Time as listed in the link below. Given your block of code, it looks like you expect a Date and Time string to be passed in and you expect to return just a Date. I'm guessing that it's not following the format that you declare as
new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z")
Also, it doesn't look like that string matches the Predefined Formats. You can try reformatting it to use
DateFormat dateAndTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, currentLocale);
This would accept a string like
"Tuesday, June 30, 2009 7:03:47 AM PDT"
parse the parameter using the formatter above and then convert a different dateFormatter
DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
you can get the default local from
Locale.getDefault()
So your code would look something like this
public static String convertDate(String dateStringToConvert) throws ParseException {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.getDefault());
Date convertedDate = dateFormat.parse(dateStringToConvert);
return DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault()).format(convertedDate);
}
Check here for more information related to using predefined formats.
https://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html

SimpleDateFormat conversion - parse error at time zone NZDT

I have following string I want to convert to date using Android:
Wed Mar 11 13:06:22 NZDT 2015
I am using following format to parse above string, but it doesn't work, as it says date is unparsable at offset 20 (time zone).
private static SimpleDateFormat date = new
SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
Any idea why? It works, If I set time zone to GMT, but not the other way.
As your time zone is in short form (NZDT) so you have to use zzz.
Actually zzzz will return full form New Zealand Daylight Time
So, correct format is:
private static SimpleDateFormat romDateFormat = new
SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

What is the date format String for 29 Sep, 30 Sep, 02 Oct etc

I tried the following code for this.
private SimpleDateFormat dateFormatter = new SimpleDateFormat("d MMM");
dateFormatter.parse("13 Jan");
but it raising parse exception invalid date.
This is the dd MMM format. So you need to use it as
new SimpleDateFormat("dd MMM");
Read Date and Time Patterns documentation for more details.
There might another issue. For your current input, format "d MMM" will also work.
Try it as:
final Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM"); // dd for date, MMM for Month
Toast.makeText(getApplicationContext(), formatter.format(calendar.getTime()), Toast.LENGTH_SHORT).show();
Hope it helps.

"Fri, 12 Sep 2014 05:00:23 GMT",what's wrong with SimpleDateFormat?

"Fri, 12 Sep 2014 05:00:23 GMT",what's wrong with SimpleDateFormat?
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.parse(dateString);
format: "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
dateString:"Fri, 12 Sep 2014 05:00:23 GMT"
what's wrong ?
Assuming your problem is that the date is coming out wrong, the SimpleDateFormat conversion specifier for timezone is z rather than 'GMT' (although you can use Z and X for the other two variants, RFC822 and ISO8601 respectively).
With your specifier, I get a local time of 5am. If I use the correct specifier in my code, it works fine:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main (String[] args) {
String format = "EEE, dd MMM yyyy HH:mm:ss z";
String date = "Fri, 12 Sep 2014 05:00:23 GMT";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date dt = sdf.parse(date);
System.out.println(dt);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The output of that program (for Perth, Western Australia, which is UTC+8) is, as expected:
Fri Sep 12 13:00:23 WST 2014
See the SimpleDateFormat online documentation for more detail.
If you still have troubles, try removing the individual items from your format and date strings until it starts working. Once you've established the problematic format specifier, it will be easier to track down.
Use "z" in your pattern because GMT is not just a literal but has to be interpreted as timezone offset UTC+00:00. And set the locale to English because you have english abbreviations of day-of-week and month:
String format = "EEE, dd MMM yyyy HH:mm:ss z";
String date = "Fri, 12 Sep 2014 05:00:23 GMT";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);

formatting date in android

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.
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.
new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.S", Locale.US)
You should specify the time zone
try this code
SimpleDateFormat simple = new SimpleDateFormat("dd MMMM yyyy hh:mm a" , java.util.Locale.getDefault());

Categories

Resources