-1

I have one incoming string which is in this format, "20210526T150239", double quotes signify that this is String.

How to convert it into something in this format 2021-05-06T12:56:21.826?

After the T values in the 2021-05-26T12:56:21.826 is not exactly equal to 150239? But its clear I want the format to be changed in such a way that Hour value then : then minute value then : and second value then . and its followed by millisecond.

How to change the format in Java from 20210526T150239 to this format 2021-05-06T12:56:21.826?

I tried something like this

            long unixSeconds = Long.parseLong(incomingTimeStamp);
            Date date = new Date(unixSeconds * 1000L);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            incomingTimeStamp = sdf.format(date)

But the T in the string is causing problem, it can not be converted to Long just by multiplying with 1000L. How to do it in Java?

John Doe
  • 2,752
  • 5
  • 40
  • 58
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 14 '21 at 17:39
  • 1
    Both formats are variants of [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). The `150239` in the string that you have got means 15:02:39 (3:02:39 PM). – Ole V.V. Jul 14 '21 at 18:01

1 Answers1

3

There is some problem with your question. you are not passing the millisecond info in Date String but expecting this should be the part of your output date thats not possible.

Anyway here is my solution:

String dateStr = "20210526T150239";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss");

LocalDateTime outputDate = LocalDateTime.parse(dateStr ,formatter);

If your input date contains millisecond info like 20210526T150239867 you can change you date pattern like DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssSSS") accordingly and parse to LocalDateTime.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • After parsing, you need to format `outputDate` using another `DateTimeFormatter` having the pattern, `uuuu-MM-dd'T'HH:mm:ss.SSS`. – Arvind Kumar Avinash Jul 14 '21 at 11:44
  • Input date may contain milliseconds or may not, that I don't know beforehand, can I not do something during runtime to get to check and accordingly modify the answer depending on the nature of the input, I am sure we can do something like this? Please throw some more light on this? – John Doe Jul 14 '21 at 11:45
  • @Arvind jee please provide some more details, better give your own answer, with all the details. – John Doe Jul 14 '21 at 11:47
  • 1
    @JohnDoe - You need to use `parseDefaulting`. Check https://stackoverflow.com/q/65269707/10819573 – Arvind Kumar Avinash Jul 14 '21 at 11:48
  • 1
    @JohnDoe - I am 100% sure that you should be able to solve it after going through all the links. In case you still have any problems, feel free to comment and I will try to help you with more information. – Arvind Kumar Avinash Jul 14 '21 at 12:56