2

I am trying to develop an android application, regarding with that I have got some data from server.

Here I need to convert a date which comes in the given format "2013-12-31T15:07:38.6875000-05:00" I need to convert this in to a date object or a calendar object how can we do this?

I have tried with code given below. But it doesn't reach my expectation

String dateString="2013-12-31T15:07:38.6875000-05:00";
Date date = null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
try {
    date = df.parse(dateString);
} catch (ParseException e) {
    e.printStackTrace();
}
return date;
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
ranjith
  • 4,526
  • 5
  • 27
  • 31

5 Answers5

4

Your format string doesn't match the date format you are getting. You should use the following format string:

"yyyy-MM-dd'T'HH:mm:ss.SSSX"
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

Step 1:

As you are getting 2013-12-31T15:07:38.6875000-05:00 as a date value, you need to define a format to parse the date.

String myDateString= "2013-12-31T15:07:38.6875000-05:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
    date = df.parse(myDateString);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Step 2: Once you parse the date, you can format it into desired format.

For example: Let's convert the parsed Date object into the yyyy-MM-dd value.

SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
String strNewDate = newFormat.format(date);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • here is some problem , when we convert this date it goes to Tue Dec 31 17:02:13 GMT 2013 some change in time, because i can't use "z" in the end – ranjith Jan 02 '14 at 15:06
1

You can use the following format for your string formatter

"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

Source : SimpleDateFormat ignoring month when parsing

For reading : http://developer.android.com/reference/java/text/SimpleDateFormat.html

Community
  • 1
  • 1
dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
1

use this code

 String string = "2013-12-31T15:07:38.6875000-05:00";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

Simple date formats

learner
  • 3,092
  • 2
  • 21
  • 33
0

If you were using the Joda-Time library, you could skip the formatter and feed the ISO 8601 string directly to the constructor of a DateTime object.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTime dateTime_DefaultTimeZone = new DateTime( "2013-12-31T15:07:38.6875000-05:00" );

Output to localized time zone and language…

String localizedOutput = DateTimeFormat.forStyle("LS").withLocale(Locale.CANADA_FRENCH).withZone( DateTimeZone.forID( "America/Montreal" ) ).print( dateTime_DefaultTimeZone );

Dump to console…

System.out.println( "dateTime_DefaultTimeZone: " + dateTime_DefaultTimeZone );
System.out.println( "localizedOutput: " + localizedOutput );

When run…

dateTime_DefaultTimeZone: 2013-12-31T12:07:38.687-08:00
localizedOutput: 31 décembre 2013 15:07
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154