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
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.
Read more... Read less...
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.
OffsetDateTime odt = OffsetDateTime.parse( "2012-10-01T09:45:00.000+02:00" );
Your Question suggests you want to truncate to a whole second.
OffsetDateTime odtTruncatedToWholeSecond = odt.truncatedTo( ChronoUnit.SECONDS );
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 the
T with a SPACE.
String output = odtTruncatedToWholeSecond.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ).replace( "T" , " " );
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.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
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
YearQuarter, and more.
Gooye if it's possible to use Joda Time in your project then this code works for me:
String dateStr = "2012-10-01T09:45:00.000+02:00"; String customFormat = "yyyy-MM-dd HH:mm:ss"; DateTimeFormatter dtf = ISODateTimeFormat.dateTime(); LocalDateTime parsedDate = dtf.parseLocalDateTime(dateStr); String dateWithCustomFormat = parsedDate.toString(DateTimeFormat.forPattern(customFormat)); System.out.println(dateWithCustomFormat);
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.
String DATE_FORMAT_I = "yyyy-MM-dd'T'HH:mm:ss"; String DATE_FORMAT_O = "yyyy-MM-dd"; SimpleDateFormat formatInput = new SimpleDateFormat(DATE_FORMAT_I); SimpleDateFormat formatOutput = new SimpleDateFormat(DATE_FORMAT_O); Date date = formatInput.parse(member.getString("date")); String dateString = formatOutput.format(date);
This worked. Thanks.
If you really gotta be fast (not that I believe you do):
char chars = sourceDate.toCharArray(); chars = ' '; String targetDate = new String(chars, 0, 19);
String dateStr = "2016-09-17T08:14:03+00:00"; String s = dateStr.replace("Z", "+00:00"); s = s.substring(0, 22) + s.substring(23); Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(s); Timestamp createdOn = new Timestamp(date.getTime()); mcList.setCreated_on(createdOn);
Java 7 added support for time zone descriptors according to ISO 8601. This can be use in Java 7.