Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

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!

like image 993
Gooey Avatar asked Mar 31 '13 14:03

Gooey


People also ask

What is SSSZ in date format?

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.

How do you format a date in Java?

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.

How do I add time zones to a date format?

Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST.

Is there a way to make a date format in Java?

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.

What are the packages to import when using format and simpledateformat?

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;

Is there a format equivalent to ISO_date_time in Java?

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.

How to get the current date and time in Java?

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


1 Answers

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.

like image 99
ctlevi Avatar answered Sep 28 '22 05:09

ctlevi