1
String date="2006-06-21T15:57:24.000Z";

How do I convert this String to a Date object without changing this format in Android?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
user878588
  • 59
  • 9
  • 6
    A `Date` does not **have** a format. If you want it formatted in some way, use a `DateFormat` object. Incidentally, that's *also* the way to **parse** that `String` into a `Date` object. – Joachim Sauer Aug 24 '11 at 14:21
  • [SimpleDateFormat](http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) is what you need in Java. Not sure about Android. – asgs Aug 24 '11 at 14:22
  • You can't do that without changing the format. Perhaps you're plain printing a `java.util.Date` object which would only result in `Date#toString()` being shown? (which indeed has a different and fixed format). You should then first convert `Date` to `String` in order to display it in the desired format to humans. – BalusC Aug 24 '11 at 14:23

5 Answers5

3

Here simple code for this:

  private Date parseDate(String date) {
    SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.S'Z'");
    Date dateObj = new Date();
    try {
      dateObj = curFormater.parse(date);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return dateObj;
  }
peceps
  • 17,370
  • 11
  • 72
  • 79
2

Use the Time class and parse the string. Then use the Time toMillis() function and instantiate a Date.

http://developer.android.com/reference/android/text/format/Time.html#parse3339(java.lang.String)

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
Billy Bob Bain
  • 2,894
  • 18
  • 13
  • But I need coding for 2006-06-21T15:57:24.000Z – user878588 Aug 25 '11 at 06:30
  • From the documentation. *`Time`: This class was deprecated in API level 22. Use GregorianCalendar instead.* Check out [my answer](https://stackoverflow.com/a/52446303/1168342) – Fuhrmanator Sep 21 '18 at 14:57
1

See SimpleDateFormat, http://developer.android.com/reference/java/text/SimpleDateFormat.html

This class converts Strings to Dates and vice versa, using a given pattern.

Once you have created a SimpleDateFormat with the right pattern, you can use it to convert the string to a Date, use the date as you like, and eventually convert the Date back to a String using that same SimpleDateFormat instance.

EDIT: clarification on time zones

In the question it is not specified wether the given string is a "pure" ISO 8601 date, and in that case whether you need or not to support multiple time zones, if that timezones will be represented as only numbers (+0200 as in RFC 822), numbers with a colon (+02:00 as permitted by ISO 8601) or as names (EST etc...).

In case the string is a pure ISO 8601 String, then SimpleDateFormat will have some problems decoding the time zone. If however it is "always Z" (meaning that timezone data is not meaningful and you can safely ignore it), or uses numbers without colon (like +0200 etc..), or uses time zone names, then SimpleDateFormat can handle it correctly.

Simone Gianni
  • 11,426
  • 40
  • 49
0

tl;dr

How do I convert this String to a Date object

Date is supplanted by java.time.Instant.

Instant.parse( "2006-06-21T15:57:24.000Z" )

without changing this format

Date-time objects do not have a “format”. Only text has a format.

String output = instant.toString() ;  // Generate text in a `String` object in standard ISO 8601 format that represents the value of the `Instant` date-time object.

ISO 8601

That input string happens to be in standard ISO 8601 format. The Z on the end is short for Zulu and means UTC.

java.time

The java.time classes use ISO 8601 formats by default when parsing and generating strings that represent date-time values.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.parse( "2006-06-21T15:57:24.000Z" );

To generate a String in standard ISO 8601 format, call toString.

String output = instant.toString(); 

2006-06-21T15:57:24Z

String != date-time

Do not conflate a date-time object with a String representing the value. The date-time object can parse a String, and can generate a String, but is not the String. In other words, a String can be input and/or output but is not the date-time object itself.

So your question, “How do I convert this String to a Date object without changing this format” makes no sense.

To generate a String in formats other than ISO 8601, convert your Instant to an OffsetDateTime or ZonedDateTime object, and use the DateTimeFormatter class. Search Stack Overflow for DateTimeFormatter to see more discussion and many examples.

Conversion

You should avoid the old java.util.Date class whenever possible. But if you must interface with old code not yet updated to the java.time types, you may convert to/from java.time via new methods added to the old date-time classes.

java.util.Date utilDate = java.util.Date.from( instant );

…and going the other direction…

Instant instant = utilDate.toInstant();

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?

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

I came here because this was supposedly an answered question of Covert RFC3339 DateTime to Date in java. However, Time is a deprecated class in Android since API level 22.

A simple answer is based on this one:

import com.google.api.client.util.DateTime;

Date date = new Date(new DateTime("2006-06-21T15:57:24.000Z").getValue());
Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111