1

What type of date format is it?

"2018-09-06T10:12:21-0300"

And how can I format it to something like that "06 Sep" ???

Lucas Moro
  • 25
  • 5
  • @TalhaTayyab Not really, a `LocalDateTime` cannot store an offset from UTC, which is given in this `String` by `-0300`. – deHaar Nov 11 '21 at 14:30
  • @deHaar yes, you are right! – Talha Tayyab Nov 11 '21 at 14:32
  • 1
    May be an `OffsetDateTime`..https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html – Talha Tayyab Nov 11 '21 at 14:36
  • Does this answer your question? [What time stamp format is this?](https://stackoverflow.com/questions/65615884/what-time-stamp-format-is-this) – Ole V.V. Nov 11 '21 at 18:48
  • See also [Generic support for ISO 8601 format in Java 6](https://stackoverflow.com/questions/13040143/generic-support-for-iso-8601-format-in-java-6). – Ole V.V. Nov 12 '21 at 09:00

2 Answers2

4

What type of date format is it?

This format is one of the ISO 8601 standard, but obviously not for the java.time.format.DateTimeFormatter which considers it a custom format consisting of ISO-standard date and time of day plus an offset from UTC without a separator (colon) between hours and minutes.

And how can I format it to something like that "06 Sep" ???

You need to define two DateTimeFormatters, one for parsing the non-standard input and the other one for outputting day of month and abbreviated month name only. Here's an example:

fun main(args: Array<String>) {
    // some non-ISO formatted String
    val inputDateTime = "2018-09-06T10:12:21-0300"
    // build up a DateTimeFormatter that can parse such a String
    val inputParser = DateTimeFormatterBuilder()
                        // date part uuuu-MM-dd
                        .append(DateTimeFormatter.ISO_LOCAL_DATE)
                        .appendLiteral('T') // the T separating date from time
                        // the time of day part
                        .append(DateTimeFormatter.ISO_LOCAL_TIME)
                        // the offset part without a separator between hours and minutes
                        .appendPattern("X")
                        // (just for completeness) a locale
                        .toFormatter(Locale.ENGLISH)
    // parse the String to an OffsetDateTime
    val offsetDateTime = OffsetDateTime.parse(inputDateTime, inputParser)
    // define another formatter for output, make it only use day of month and abbreviated month in English
    val outputFormatter = DateTimeFormatter.ofPattern("dd MMM", Locale.ENGLISH)
    // print the results
    println("$offsetDateTime ---> ${offsetDateTime.format(outputFormatter)}")
}

Example output:

2018-09-06T10:12:21-03:00 ---> 06 Sep
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • AFAIK, the `X` in the pattern is not about not having the hours-minutes separator, it's about representing the offset 0 with the letter `Z` instead of `+00:00` – Joffrey Nov 11 '21 at 14:49
  • Yes, @Joffrey, I read that, too. But unfortunately this way was the first working one in a classic *try-and-error* case where I started without a formatter at all, then used different amounts of `x` and finally found out the parsing actually works with a single `X`. – deHaar Nov 11 '21 at 14:53
  • 1
    What I mean is that both `X` or `Z` can work. A single letter represents the whole offset (with or without hours/minutes separator). It's unrelated to the fact there is no separator between hours and minutes in the offset representation of this particular formatted date. – Joffrey Nov 11 '21 at 14:56
  • 1
    ...which makes it even more surprising that `OffsetDateTime.parse(text)` doesn't work without customizing the formatter – Joffrey Nov 11 '21 at 14:59
1

This format respects the ISO 8601 standard for representing a date/time with timezone information.

There is enough information to parse this string into an OffsetDateTime, but apparently Java's formatter is a bit strict with respect to the missing separator between hours and minutes in the offset representation (which is technically allowed by the standard). This means a plain OffsetDateTime.parse(text) will throw an exception.

Therefore, you'll to define a custom DateTimeFormatter as explained by @deHaar.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • I cannot parse it without explicitly defining a non-ISO formatter… See my answer for that. Can you really parse it that way (and my system can't) or were you just expecting it to work (but didn't try)? – deHaar Nov 11 '21 at 14:44
  • 1
    Interesting, I expected Java's parser to accept all variants of the ISO spec. I guess I was wrong. – Joffrey Nov 11 '21 at 14:46
  • 1
    Yes, and your link made me aware of being wrong myself concerning a different aspect: The `String` is not of a custom format but rather ISO 8601. – deHaar Nov 11 '21 at 14:48
  • The one-arg `parse` methods of java.time only parse the most common variants of ISO 8601. There are numerous points of variation. This colon thing is probably the most often encountered of very many limitations in those one.arg `parse(CharSequence)` methods. See [the answer by Meno Hochschild here](https://stackoverflow.com/a/60008685/5772882) too. – Ole V.V. Nov 11 '21 at 19:42