I am having a problem with formating a date using Java. The date formatter seems to parse my string using BST (British Summer Time) and not GMT+0 as defined by +0000 in the dateStr string in the code below.
String dateStr = "Tue Oct 02 01:06:00 +0000 2012";
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
Date date = df.parse(dateStr);
// Test the date
System.out.println(date.toString());
When I run the above code I get:
Tue Oct 02 02:06:00 BST 2012
Which is evidently not what I want. A useful point of information might be that when I run the following:
System.out.println(TimeZone.getDefault().getDisplayName());
I get:
Greenwich Mean Time
The output I'm trying to get is
Tue Oct 02 01:06:00 GMT 2012
I've already tried .setTimeZone on df but to no avail.
Note that Tue Oct 02 02:06:00 BST 2012 is the same time as Tue Oct 02 01:06:00 +0000 2012, just expressed in a different time zone. java.util.Date doesn't really deal with time zones, so as far as its concerned, there is no difference. The Date object you've constructed represents the correct date and time you want.
The Date.toString() method is essentially just a formatter that uses your JRE default TimeZone in its formatting. If you want to test that the Date object is correct, then you should build a java.util.Date object (using java.util.Calendar - most of the Date constructors have been deprecated) to test against. If you want to create a display string, then you should use the SimpleDateFormatter instance you've already created to format the Date object.
If you do need an object that stores TimeZone information, then you should use java.util.Calendar. java.util.Date is almost deprecated to the point of being unusable anyway. Or, do the same as I am and wait for the new Data and Time API coming out in Java 8.
The .toString() method is using your locale when printing the date.
Use:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(date));
I understand that BST is already deprecated in SE 7.
you can try like here:
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",
Locale.CANADA);
add Local args to SimpleDateFormat constructor
Related
This question already has an answer here:
Joda DateTimeFormatter.parseDateTime is failing for General time zone('z')
(1 answer)
Closed 2 years ago.
I'm trying to compare current date with an input date, received in this format "EEE MMM dd HH:mm:ss zzz yyyy"
This piece of code works when my input string is Wed Apr 01 09:00:00 GMT 2020 but doesn't work if its Wed Apr 01 09:00:00 BST 2020 .
DateTime currentTime =DateTime.now().withZone(DateTimeZone.forID("Europe/London"));
DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zzz yyyy");
DateTime inputDateTime = fmt.parseDateTime("Wed Apr 01 09:00:00 BST 2020");
if (inputDateTime.isBefore(currentTime))
Log.d(TAG, "if ");
else
Log.d(TAG, "else ");
Any idea on what am I doing wrong? Also, feel free to suggest if there's a better way to do it (can't use new Java date and time library, since we support android API 19+ )
If you use ThreeTen Android Backport instead of Joda-Time, and you should, then it parses fine:
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
ZonedDateTime inputDateTime = ZonedDateTime.parse("Wed Apr 01 09:00:00 BST 2020", fmt);
System.out.println(inputDateTime);
Output
2020-04-01T09:00+01:00[Europe/Isle_of_Man]
Docs has mentioned that Time zone names cannot be parsed in joda DateTimeFormat because time zone abbreviations are ambiguous and the parser can't know which time zone it is exactly
Zone names: Time zone names ('z') cannot be parsed.
For example
BST can be British Summer Time or Bangladesh Standard Time or Bougainville Standard Time
PST can be Pacific Standard Time or Pakistan Standard
Time
CST could be Central Standard Time (USA), China Standard Time, or Cuba Standard Time
EST could be Eastern Standard Time (USA), or Eastern Standard Time (Australia).
And the best way to is to replace BST with appropriate iso timezone code here (for example Europe/Isle_of_Man), and then simple use "EEE MMM dd HH:mm:ss ZZZ yyyy" DateTimeFormat
DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss ZZZ yyyy");
DateTime inputDateTime = fmt.parseDateTime("Wed Apr 01 09:00:00 Europe/Isle_of_Man 2020");
System.out.println(inputDateTime); //2020-04-01T09:00:00.000+01:00
I'm not sure if this meets your requirements for the Andriod API 19+, but here is something for the date formatting with SimpleDateFormatter
String pattern = "EEEEE MMMMM yyyy HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
Hope this solves your problem!
I am working with expiration date of card. I have a API where I will get expiration date in "yyMM" format as "String". Here I am trying to use
SimpleDateFormat with TimeZone.getTimeZone("UTC")
So my code is like
String a= "2011";
SimpleDateFormat formatter = new SimpleDateFormat("yyMM");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(a);
System.out.println(date);
Now problem is, when I am passing 2011 the out it gives is Sat Oct 31 17:00:00 PDT 2020
Here you can see I am passing 11 as month but it is converting it to Oct instead of Nov.
Why?
And what other options I can use to convert string with yyMM to Date with Timezone?
You should use the Java 8 YearMonth class.
String a = "2011";
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyMM");
YearMonth yearMonth = YearMonth.parse(a, inputFormat);
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MMMM yyyy");
System.out.println(yearMonth.format(outputFormat));
Output
November 2020
You parsed it fine, but it's printed in PDT, your local timezone.
Sat Oct 31 17:00:00 PDT 2020
Well, Date doesn't track timezones. The Calendar class does, which is internal to the formatter. But still, default print behavior is current timezone.
If you logically convert this output back to UTC, and it will be November 1 since PDT is UTC-7.
Basically, use java.time classes. See additional information here How can I get the current date and time in UTC or GMT in Java?
I am a little confused with the unix time stamp conversion to java.util.Date involving the time zones.
I have a unix time stamp that is "1367832568". it is a UTC date (Mon May 06 17:29:28 GMT+00:00 2013). when i do following :
Date d = new Date(1367832568 * 1000);
it gives me Mon May 06 17:29:28 GMT+08:00 2013 which is not correct. it should give me:
Mon May 07 01:29:28 GMT+08:00 2013
because the unix time stamp is actually a UTC date. so this is confusing. what should i do to convert the UTC unix time stamp to correctly convert to a UTC Date object.
I'm not sure exactely how are you formatting your date object, but by setting the timezone to the dateformat object (to GMT) I'm able to get
Mon May 06 09:29:28 GMT 2013 AM
which is the same date you are looking for.
long time = 1367832568 * (long) 1000;
Date date = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy a");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.d("date", format.format(date));
i have a date object in the following format:
Sun Jan 20 10:12:27 GMT+02:00 2013
the above time appears in microsoft outlook correctly:
Sun 1/20/2013 12:12 PM (this is the time in GMT+2 >> client timezone)
when trying to format the date object with SimpleDateFormat to appear as in the outlook, using the following code:
SimpleDateFormat sdf=new SimpleDateFormat(
"EEE M/d/yyyy hh:mm a");
String receivedDate = sdf.format(email.getDateTimeReceived());
the result of formatting is:
Sun 1/20/2013 10:12 AM
so the two hours of the timezone difference are missing.
please advise how to fix that, thanks.
If I understand correctly, you want to format the date using the GMT time zone.
DateFormat dateFormat = new SimpleDateFormat("EEE M/d/yyyy hh:mm a");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String formattedDate = dateFormat.format(date);
You forgot to tell to the SimpleDateFormat to include the timezone information.
This will do the trick:
SimpleDateFormat sdf=new SimpleDateFormat(
"EEE M/d/yyyy hh:mm a zzzZ yyyy");
Note the Uppercase Z at the end to display the time diff
This will print:
Sun 1/20/2013 12:09 PM CET+0100 2013
if you need it to be GMT you can force that like this:
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Now it will print:
Sun 1/20/2013 11:11 AM GMT+0000 2013
if you don't need the AM/PM just remove the a
How to convert the date Tue Nov 13 14:35:04 +0000 2012 String format to date in Java?
I know of Date.parse(String) but I don't know which format I should use for the date. Do I have to modify the string so that it can be parsed into date, and if yes then how?
Use SimpleDateFormat, with a format string of
"E MMM dd HH:mm:ss Z yyyy"
You should explicitly use Locale.US assuming these will definitely use English month/day names. (You don't want to be trying to parse French names just because the default locale is French, for example.)
Also, don't forget that the Date value returned will have no knowledge of the original time zone - it will have the right value for the instant represented in the original text, but don't expect the result of calling toString() to use the same zone - Date.toString() always uses the default time zone.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date test = sdf.parse("Tue Nov 13 14:35:04 +0000 2012");
System.out.print(test.toString());