Java SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss’Z'”) gives timezone as IST

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”));

java.text.ParseException: Unparseable date

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

Why is Java’s SimpleDateFormat not thread-safe? [duplicate]

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

What is this date format? 2011-08-12T20:17:46.384Z

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().

Illegal pattern character ‘T’ when parsing a date string to java.util.Date

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

How to parse dates in multiple formats using SimpleDateFormat

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

How do you format the day of the month to say “11th”, “21st” or “23rd” (ordinal indicator)?

// 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