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());
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 am trying to parse a String to a Date and it giving me right date where as time is wrong.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
try {
Date date = formatter.parse("2015-08-20 05:00 AM");
Log.e("date", date+""); // Logcat printing Thu Aug 20 00:00:00 GMT+05:30 2015
} catch (ParseException e) {
Log.e("Error",e.toString());
}
As you can see irrespective of the time every time parsed date time showing 00:00:00
What I want is Thu Aug 20 05:00:00 GMT+05:30 2015
It seems the problem was that your pattern String specified am/pm, but was using uppercase H's for the hour characters. These indicate a 24-hour clock, which obviously doesn't use am/pm. Change the hour characters to lowercase h's, which indicate the hour in am/pm (0-11).
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
The docs for SimpleDateFormat explain the various acceptable pattern characters.
Try below code
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");
String dateToStr = format.format(new Date());
System.out.println("dateToStr=>" + dateToStr);
try {
Date strToDate = format.parse(dateToStr);
System.out.println("strToDate=>" + strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
Maybe its related to time zone issues, at what time zone is your input?, i'd suggest to make sure your paramater is on UTC timezone and then using formatter like this :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
formatter .setTimeZone(TimeZone.getTimeZone("UTC"));
Try this code you missed some lines of code
SimpleDateFormat mFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aa", Locale.getDefault());
mFormatter.setTimeZone(TimeZone.getTimeZone("India"));
Date startDate = mFormatter.parse("2015-08-20 05:00 AM");
This code working fine for me.
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.
In the following code myLeg.TimeStamp is a String that has "Feb 26 2014 12:31:23 PM"
myRTleg.Tstamp is a Date.
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy HH:mm:ss a");
myRTleg.TStamp = formatter.parse(myLeg.TimeStamp);
String debugStr = myRTleg.TStamp.toString();
DebugStr has the same exact date in it but it has AM instead of PM !!
Why is it doing this?
Thanks,
Dean
From
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
H Hour in day (0-23)
...
h Hour in am/pm (1-12)
so try
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy hh:mm:ss a");
The AM/PM is probably defaulting to AM, and being ignored because the hours are HH (military) instead of hh
You didn't provide the full input and output dates. Check the timezone of your input date and output date to see if they are different. It could just be the same date in time but formatted in using different time zones.
You need to use hh for hours instead of HH. HH is used for Military time.