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

9

87

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!

Uraninite answered 31/3, 2013 at 14:13 Comment(4)
Substringing is much faster/simpler than converting to/from a date object. You just break the original into two substrings (fixed offsets) and then concatenate together with a blank between.Propylite
Why is this? I might be parsing up to 100+ dates in a single go.Uraninite
You don't think that two substring operations with hard-coded offsets plus a couple of concatenation ops would be faster than constructing and executing two date formatters?Propylite
FYI, the troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleTextFormat are now legacy, supplanted by the java.time classes.Paraphernalia
A
115
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.

Aday answered 31/3, 2013 at 15:1 Comment(1)
It appends 12 at start... I expect date 02/05/2017 but it gives me 122/05/2017Plenary
P
44

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.


Table of date-time types in Java, both modern and legacy


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?

Table of which java.time library to use with which version of Java or Android

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.

Paraphernalia answered 17/3, 2016 at 10:44 Comment(3)
only for java 8 or higherMetrorrhagia
@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.Paraphernalia
you can use standard java.time (without ThreeTen) on Android with lower APIs when desugaring enabled check this answer: https://mcmap.net/q/243274/-call-requires-api-level-26-current-min-is-23-java-time-instant-nowUrbannal
U
12

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);
Ubiety answered 31/3, 2013 at 14:58 Comment(4)
Adding on more dependency for nothing. It exist in the JDK, see Chris Tate answerBelted
i think it will work in java 8, not for java 7 or below.Metrorrhagia
@Sam, It will work in any java version because it uses joda-time library. Just import it into your project and import required dependncies.Ubiety
How to Split below format String todayDate = currentDate1.toString("yyyy-MMMM-dd HH:mm")Bowlds
C
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

Chromogenic answered 31/3, 2013 at 14:13 Comment(4)
Please 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 in java.time, the modern Java date and time API, and its DateTimeFormatter.Jewbaiting
I 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 got 2021092650509884. There are two digits too many, and neither day of month, hour, minute nor second is correct.Jewbaiting
@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.Chromogenic
Closer. It still does not give me the time in my time zone.Jewbaiting
P
2

If you really gotta be fast (not that I believe you do):

char[] chars = sourceDate.toCharArray();
chars[10] = ' ';
String targetDate = new String(chars, 0, 19);
Propylite answered 31/3, 2013 at 15:11 Comment(0)
L
2

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.

Lawmaker answered 17/3, 2016 at 7:22 Comment(1)
This 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.Paraphernalia
M
2
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.

Metrorrhagia answered 17/2, 2017 at 9:5 Comment(0)
M
1

I have a case where I am transforming a legacy DB2 z/os database timestamp (formatted as: "yyyy-MM-dd'T'HH:mm:ss.SSSZ") into a SqlServer 2016 datetime2 (formatted as: "yyyy-MM-dd HH:mm:ss.SSS) field that is handled by our Spring/Hibernate Entity Manager instance (in this case, the OldRevision Table). In the Class, I define the date as the java.util type, and write the setter and getter in the normal way. Then, When handling the data, the code I have handling the data related to this question looks like this:

OldRevision revision = new OldRevision();
String oldRevisionDateString= oldRevisionData.getString("originalRevisionDate", "");
Date oldDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
oldDateFormat.parse(oldRevisionDateString);
SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
newDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
Date finalFormattedDate= newDateFormat.parse(newDateFormat.format(oldDateFormat));
revision.setOriginalRevisionDate(finalFormattedDate);
em.persist(revision);

A simpler way to do the same case is:

SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
newDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
revision.setOriginalRevisionDate(
        newDateFormat.parse(newDateFormat.format(
                new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ")
                        .parse(rs.getString("originalRevisionDate", "")))));
Michelsen answered 8/1, 2021 at 18:21 Comment(0)
A
0

Try The following code.

public static void main(String[] args) {
    String str = "18-09-22 02:40:16";
    DateTimeFormatter df = DateTimeFormatter.ofPattern("dd-MM-yy HH:mm:ss", Locale.UK);
    LocalDateTime parse = LocalDateTime.parse(str, df);
    System.out.println("old output = " + parse);
    String output = parse.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ).replace( "T" , " " );
    System.out.println("output = " + output);
}

This worked.

OUTPUT:

enter image description here

Airplane answered 29/9, 2022 at 11:35 Comment(3)
You do not need a Locale there as nothing in the input string is localized. Not that it hurts.Paraphernalia
You have not addressed the Question. The Question asks about input strings with an offset-from-UTC. Your Answer ignores offsets.Paraphernalia
you are right BasilAirplane

© 2022 - 2025 — McMap. All rights reserved.