SimpleDateFormat and locale based format string
SimpleDateFormat dateFormat = new SimpleDateFormat(“EEEE dd MMM yyyy”, Locale.ENGLISH); String formatted = dateFormat.format(the_date_you_want_here);
SimpleDateFormat dateFormat = new SimpleDateFormat(“EEEE dd MMM yyyy”, Locale.ENGLISH); String formatted = dateFormat.format(the_date_you_want_here);
You haven’t set the timezone only added a Z to the end of the date/time, so it will look like a GMT date/time but this doesn’t change the value. Set the timezone to GMT and it will be correct. SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss’Z'”); sdf.setTimeZone(TimeZone.getTimeZone(“GMT”));
Your pattern does not correspond to the input string at all… It is not surprising that it does not work. This would probably work better: SimpleDateFormat sdf = new SimpleDateFormat(“EE MMM dd HH:mm:ss z yyyy”, Locale.ENGLISH); Then to print with your required format you need a second SimpleDateFormat: Date parsedDate = sdf.parse(date); SimpleDateFormat print = … Read more
SimpleDateFormat stores intermediate results in instance fields. So if one instance is used by two threads they can mess each other’s results. Looking at the source code reveals that there is a Calendar instance field, which is used by operations on DateFormat / SimpleDateFormat. For example parse(..) calls calendar.clear() initially and then calendar.add(..). If another … Read more
The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”, Locale.US); format.setTimeZone(TimeZone.getTimeZone(“UTC”)); Or using Joda Time, you can use ISODateTimeFormat.dateTime().
Update for Java 8 and higher You can now simply do Instant.parse(“2015-04-28T14:23:38.521Z”) and get the correct thing now, especially since you should be using Instant instead of the broken java.util.Date with the most recent versions of Java. You should be using DateTimeFormatter instead of SimpleDateFormatter as well. Original Answer: The explanation below is still valid … Read more
You’ll need to use a different SimpleDateFormat object for each different pattern. That said, you don’t need that many different ones, thanks to this: Number: For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored … Read more
// https://github.com/google/guava import static com.google.common.base.Preconditions.*; String getDayOfMonthSuffix(final int n) { checkArgument(n >= 1 && n <= 31, “illegal day of month: ” + n); if (n >= 11 && n <= 13) { return “th”; } switch (n % 10) { case 1: return “st”; case 2: return “nd”; case 3: return “rd”; default: return … Read more
week year and year. From javadoc A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values. For example, January 1, 1998 is a Thursday. … Read more
It’s probably because of the default locale on your computer which is not english. You should use: new SimpleDateFormat(“EEE, dd MMM yyyy HH:mm:ss”, Locale.ENGLISH); instead.