0

{

"time_slots": [
{
  "id": 1,
  "from_time": "2000-01-01T09:00:00.000Z",
  "to_time": "2000-01-01T10:00:00.000Z",
  "created_at": "2016-05-27T08:09:05.979Z",
  "updated_at": "2016-05-27T08:09:05.979Z",
  "status": true
},
{
  "id": 2,
  "from_time": "2000-01-01T10:00:00.000Z",
  "to_time": "2000-01-01T11:00:00.000Z",
  "created_at": "2016-05-27T08:17:43.121Z",
  "updated_at": "2016-05-27T08:17:43.121Z",
  "status": true
},
{
  "id": 3,
  "from_time": "2000-01-01T13:00:00.000Z",
  "to_time": "2000-01-01T14:00:00.000Z",
  "created_at": "2016-05-27T08:18:06.251Z",
  "updated_at": "2016-05-27T08:18:06.251Z",
  "status": true
},
{
  "id": 4,
  "from_time": "2000-01-01T14:00:00.000Z",
  "to_time": "2000-01-01T15:00:00.000Z",
  "created_at": "2016-05-27T08:18:34.752Z",
  "updated_at": "2016-05-27T08:18:34.752Z",
  "status": true
}

] } can any one please help how to convert json string time to Time format in android.I tried to convert using simple date format SimpleDateFormat("yyyy-mm-ddTh:m:s"); but its not working.Any help is really appreciated.

Vimesh Pk
  • 144
  • 1
  • 3
  • 8
  • 3
    Please atleast do some searching before asking – Nongthonbam Tonthoi Jun 02 '16 at 10:24
  • Duplicate question: http://stackoverflow.com/questions/20168647/java-text-parseexception-unparseable-date-yyyy-mm-ddthhmmss-sssz-simple – Mehta Jun 02 '16 at 10:30
  • 1
    Possible duplicate of [java.text.ParseException: Unparseable date "yyyy-MM-dd'T'HH:mm:ss.SSSZ" - SimpleDateFormat](http://stackoverflow.com/questions/20168647/java-text-parseexception-unparseable-date-yyyy-mm-ddthhmmss-sssz-simple) – rohitanand Feb 28 '17 at 03:58
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Apr 13 '18 at 00:54

3 Answers3

3

tl;dr

Instant.parse( "2016-05-27T08:17:43.121Z" )

Details

json string time

No such thing as "JSON time". What you have is strings in standard ISO 8601 format.

The Z on the end is pronounced Zulu, and means UTC.

Time format in android.

No such thing as "Android time format". You can use any format that is appropriate to your user’s human language and cultural norms. See the DateTimeFormatter class, and in particular, its ofLocalized… methods.

SimpleDateFormat("yyyy-mm-ddTh:m:s")

That class is part of the troublesome old date-time classes bundled with the earliest versions of Java. They were supplanted years ago by the java.time classes.

The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern.

Instant instant = Instant.parse( "2016-05-27T08:17:43.121Z" ) ;

To see this same moment in the wall-clock time used by the people of a certain region (a time zone), apply a ZoneId to get a ZonedDateTime object.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;   // Always use proper time zone names in `Continent/Region` format, never pseudo-zones like `PDT`, `CST`, `IST`, etc. 
ZonedDateTime zdt = instant.atZone( z ) ;  // Same moment, same point on the timeline, different wall-clock time.

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
2

Try This

String dt_str = "2000-01-01T13:00:00.000Z";  
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");  
try {  
   Date date = format.parse(dt_str);  
   System.out.println(date);  
} catch (ParseException e) {  
   // TODO Auto-generated catch block  
  e.printStackTrace();  
}
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
1

Try this.

SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
Date myDate = myFormat.parse("your date string here");
Micho
  • 3,929
  • 13
  • 37
  • 40
SripadRaj
  • 1,687
  • 2
  • 22
  • 33
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Apr 13 '18 at 00:49