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");
Related
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
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
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.
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());
I am using smack api for instance messages.I want to send the current time with the messages.So i have set the current time as message subject.and then get that time on receiver side.But the problem is that message subject should be string so i have convert the date time to string at sender side and then again convert from string to date time on receiver side.I want the sender's date time should be convert as per receiver's Timezone.I have wrote the code as below but i can't convert the date time to receiver's Timezone's date time.
//sender side
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
String strdt= formatter.format(c.getTime());
message.setSubject(strdt);
receiver side
TimeZone tz = TimeZone.getDefault();
String strzone=tz.getDisplayName(false, TimeZone.SHORT);
String str=message.getSubject();
Date expiry = null;
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
formatter.setTimeZone(TimeZone.getTimeZone(strzone));
try {
expiry = formatter.parse(str);
}
catch (Exception e)
{e.printStackTrace();}
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
df.setTimeZone(TimeZone.getTimeZone(strzone));
String formattedDate1= df.format(expiry);
Log.i("receiving time",formattedDate1);
i got one output like
message subject=> Wed Aug 22 13:35:13 GMT+00:00 2012
after convert from string to date => Wed Aug 22 07:35:13 MDT 2012
after set the time zone of receiver => Wed Aug 22 06:35:13 GMT-07:00 2012
actual receiver side time => Wed Aug 22 06:39:56 MDT 2012
Edit
Actually i want to send the sender's time to receiver and on receiver side the should be convert as per receiver's time zone.as describe above the receiver's actual time is different than the converting time.So please if you have different code then please post here.
Use plain longs for time. (Long.toString(new Date().getTime()). It does not contain (nor need) any TZ info, and is therefore more flexible.