I'm trying to format a date in yyyy-MM-dd'T'HH:mm:ss.SSSz format to yyyy-mm-dd HH:mm:ss, which should be easy but I can't get it to work.
A date that has to be parsed is in the form of: 2012-10-01T09:45:00.000+02:00
Now i use this simple date formatter to format it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.FRANCE);
yet this gives an output similar to 2012-10-01T09:45:00.000UTC+00:00.
I've also tried to use "yyyy-MM-dd'T'HH:mm:ss.SSSZ" as pattern and "yyyy-MM-ddHH:mm:ss". The latter returns a date in the form of 2012-10-01T09:45:00 close, but not there yet.
I figured substringing the T away would be a bit messy and creates overhead for no reason, thus what would be the proper way to format these dates?
To illustrate I would like to convert 2012-10-01T09:45:00.000+02:00 into 2012-10-01 09:45:00
Cheers!
Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss.
Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.
Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST.
You are correct in that this does not appear to match any of the default formats, so you would need to build your format with a java.time.format.DateTimeFormatterBuilder. You should never be bothered by those annoying date-format. There has an new library dateparser.
Since we have used the Format and SimpleDateFormat class above, therefore import the following packages. With that, we have also used the Date − import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;
I am not sure this is your expected answer. The given input format is equivalent to ISO_DATE_TIME format after removing 'Z' from the given pattern yyyy-MM-dd'T'HH:mm:ss.SSSZ You are correct in that this does not appear to match any of the default formats, so you would need to build your format with a java.time.format.DateTimeFormatterBuilder.
With that, we have also used the Date − import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; Today's date and time = Mon, 26 Nov 2018 09:34:11 +0000 Current Date in MM-dd-yyyy hh:mm:ss format = 11-26-2018 09:34:11 Current Date = 26/November/2018 Current Time = 09.34.11 +0000
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = sdf.parse(time); String formattedTime = output.format(d);
This works. You have to use two SimpleDateFormats, one for input and one for output, but it will give you just what you are wanting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With