Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
Solution 1
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.
Solution 2
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.
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.
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?
-
Java SE 8, Java SE 9, and later
- Built-in.
- 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.
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
Solution 3
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);
Solution 4
Try This
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat output = new SimpleDateFormat("yyyyMMddHHmmss");
Date d = sdf.parse("2021-09-23T22:05:18.884Z");
String formattedTime = output.format(d);
System.out.println(formattedTime);
Results : 20210923220518
Solution 5
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.
Comments
-
Gooey almost 2 years
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!
-
Aubin about 11 yearsAdding on more dependency for nothing. It exist in the JDK, see Chris Tate answer
-
Basil Bourque about 8 yearsThis answer does not address the specifics of the Question. And it duplicates the ideas of the 3-year-old accepted answer. So I do not see any value be added. Glad to see that you want to participate in Stack Overflow. But your answers must address the Question’s specifics and not be a duplicate.
-
Sangram Badi over 7 yearsonly for java 8 or higher
-
Basil Bourque about 7 years@Sangram Actually, no, not just Java 8. Much of the java.time functionality is back-ported to Java 6 and Java 7 in the ThreeTen-Backport project and further adapted to Android in the ThreeTenABP project.
-
Sangram Badi about 7 yearsi think it will work in java 8, not for java 7 or below.
-
moleksyuk about 7 years@Sam, It will work in any java version because it uses joda-time library. Just import it into your project and import required dependncies.
-
VVB about 7 yearsIt appends 12 at start... I expect date 02/05/2017 but it gives me 122/05/2017
-
Ole V.V. over 2 yearsPlease don’t teach the young ones to use the long outdated and notoriously troublesome
SimpleDateFormat
class. At least not as the first option. And not without any reservation. We have so much better injava.time
, the modern Java date and time API, and itsDateTimeFormatter
. -
Ole V.V. over 2 yearsI tried, as you encouraged me to. From your code I would have expected
20210922072818
because the original string is in UTC and my time zone is 2 hours ahead of UTC. I got2021092650509884
. There are two digits too many, and neither day of month, hour, minute nor second is correct. -
codeAman over 2 years@OleV.V. I have edited my code, Now you will get your expected results from it. The problem was The result format which you were expected that would be achieved from this format yyyyMMddHHmmss.
-
Ole V.V. over 2 yearsCloser. It still does not give me the time in my time zone.