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!
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.
Gooye if it's possible to use Joda Time in your project then this code works for me:
Java 7 added support for time zone descriptors according to ISO 8601. This can be use in Java 7.
java.time
We have new technology for this problem: the java.time framework built into Java 8 and later.
Your input string is in standard ISO 8601 format. That standard is used by default in the java.time classes for parsing/generating textual representations of date-time values.
Your Question suggests you want to truncate to a whole second.
It seems you want to omit the offset and time zone info. The pre-defined formatter
DateTimeFormatter.ISO_LOCAL_DATE_TIME
does that.And apparently you want to use a space in the middle rather than the standard
T
. You could define your own formatter for this, but I would just do a string manipulation to replace theT
with a SPACE.Simply String Manipulations
As the comments on the Question suggest, strictly speaking you can accomplish your goal by working only with strings and not converting to any date-time objects. But I provide this Answer here assuming you may have other business logic to work with these date-time values.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.If you really gotta be fast (not that I believe you do):
I was trying to format the date string received from a JSON response e.g. 2016-03-09T04:50:00-0800 to yyyy-MM-dd. So here's what I tried and it worked and helped me assign the formatted date string a calendar widget.
This worked. Thanks.