0

I have a simple question. How can I convert an ISO-8601 Date to an String? I'm using a Date with the following format: 2019-02-05T08:21:15.000+01:00 and want to convert this Date Object to an String. I tried the following:

String startString = (String) jsonObjectMap2.get("created_on"); 
//startString = "2019-02-21T09:47:58.699004+00:00"`
DateTime start = ISODateTimeFormat.dateTimeParser().parseDateTime(startString);
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss.SSSZ+|-hh:mm"); 
String formatedStartString = dateFormat.format(start);`

But Iam getting the following error:

java.lang.IllegalArgumentException: Cannot format given Object as a Date.

What is the corrrect form of the DateFormat? I hope you can help me, thank you in advance.

josepdecid
  • 1,737
  • 14
  • 25
Linko1232
  • 15
  • 7
  • 3
    Please note that when asking about exceptions you should _always_ post the entire stacktrace (nicely formatted please). – Thomas Feb 21 '19 at 14:48
  • 1
    As for your question: `DateFormat.format()` doesn't accept `DateTime` instances, just `Date` or `Number`. `ISODateTimeFormat` indicates you are using JodaTime and thus you'd need to use JodaTime to format the parsed date. Try using JoadTime's `DateTimeFormat.forPattern(/*your pattern*/).print(start)`. – Thomas Feb 21 '19 at 14:53
  • Possible duplicate of [Converting ISO 8601-compliant String to java.util.Date](https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) – Sagar P. Ghagare Feb 21 '19 at 14:55
  • 1
    Where does class `DateTime` come from - are you using Joda Time? (It's not a class from the standard Java library). – Jesper Feb 21 '19 at 15:01
  • There’s something I don’t understand here. You are getting a string from `jsonObjectMap2` and you say you want a string. Is there anything missing at all? What? – Ole V.V. Feb 21 '19 at 21:20
  • 1
    When you’re already using Joda-Time, there’s no point in also struggling with the notoriously troublesome and long outdated `SimpleDateFormat`. Joda-Time can do your formatting (if you need any at all). The other good option is to follow the suggestion from [the Joda-Time home page](https://www.joda.org/joda-time/): migrate entirely to java.time, the modern Java date and time API. – Ole V.V. Feb 21 '19 at 21:29
  • 1
    If using Joda-Time in your code sample, say so in your Question, or add a tag. – Basil Bourque Feb 21 '19 at 21:41
  • Paste your the message you got, *Cannot format given Object as a Date*, into your search engine and get lots of hits. – Ole V.V. Feb 22 '19 at 07:14

5 Answers5

2

tl;dr

java.time.OffsetDateTime.parse( "2019-02-21T09:47:58.699004+00:00" )

java.time

What class is DateTime? If you are using Joda-Time, know that the Joda-Time project is now in maintenance-mode. Its creator, Stephen Colbourne, went on to lead JSR 310 and implement the java.time classes built into Java.

No need for formatting pattern

Your input string is in standard ISO 8601 format. The java.time classes use the ISO 8601 formats by default when parsing/generating strings.

String input = "2019-02-21T09:47:58.699004+00:00" ;  // Standard ISO 8601 format.

OffsetDateTime

Your input string indicates an offset-from-UTC but not a time zone. So the appropriate class to represent this value is OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.parse( input ) ;

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.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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?

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

why not use toString() on your DateTime object?

sznixon
  • 11
  • 3
1

As Thomas alluded to in the comments, DateTime.format() consumes a Date object, not a DateTime object; hence the IllegalArgumentException. Judging by the format of your date you could try parsing your startString using the in-built java.time.ZonedDateTime class:

ZonedDateTime dateTime = ZonedDateTime.parse(startString) //it can parse ISO-8601 date-times

You could then format your dateTime object using java.time.DateTimeFormatter's static ofPattern() method which takes a String pattern to apply to your date-time and returns the formatted date as a String:

String formattedDateTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SSSZ+|-hh:mm"));

EDIT

As Basil pointed out, the OP's DateTime object includes a time offset which is not necessarily a time zone offset--as is represented by the ZonedDateTime class. Thus the most appropriate class to use in this scenario is OffsetDateTime which provides the same capabilities but is more suited for the given use case. See Basil Bourque's answer

Garikai
  • 405
  • 2
  • 15
  • 1
    No, `ZonedDateTime` is not appropriate here. The input string of "2019-02-21T09:47:58.699004+00:00" indicates an offset-from-UTC but not a time zone. So the best fit is `OffsetDateTime` class: `OffsetDateTime.parse( "2019-02-21T09:47:58.699004+00:00" )` – Basil Bourque Feb 21 '19 at 19:54
  • @BasilBourque You're right, hadn't used `OffsetDateTime` before so `ZonedDateTime` immediately came to mind – Garikai Feb 21 '19 at 21:04
0

I am not certain that this will solve your problem, But mm should be uppercase i.e MM, Because MM describes months while mm describes minutes.

see the following code:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ+|- 
    hh:mm");
0

I have found the following to work for me

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS[XXX]");
String formatted = zonedDateTime.format(dateTimeFormatter);
sims_gfl
  • 61
  • 4
  • (A) No need to specify a formatting pattern. Standard ISO 8601 strings such as this input can be parsed by default with *java.time* classes. (B) No `ZonedDateTime` is the wrong class for this input. The `OffsetDateTime` class is appropriate for an input with an offset-from-UTC but not a time zone. – Basil Bourque Feb 21 '19 at 20:07