From a web service I am getting a date value like "2011-05-31T00:00:00.000+03:00" and I want to add it into an arrayList(String). How can I parse it to String and write it like "31.05.2011" to the array?
Asked
Active
Viewed 1,624 times
3 Answers
1
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = df1.parse("2011-05-31T00:00:00.000+03:00")
DateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String newDateString = df2.format(date);

Georgy Gobozov
- 13,633
- 8
- 72
- 78
-
you make something wrong, edit your question and add your code to add string to arraylist – Georgy Gobozov Feb 07 '13 at 15:05
-
ok I changed "SSSXXX" to "SSSZ" and worked but "newDataString" gets 30.05.2011 instead of 31.05.2011 – arenko Feb 07 '13 at 15:24
-
I just have tried and new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); returned newDateString = 31.05.2011 – Georgy Gobozov Feb 07 '13 at 15:39
-
yeah that Z letter made difference now its working, thanks. Do you know why these Z, X letters change the output? – arenko Feb 07 '13 at 16:25
1
Use this function:
public String getMydate(String mydate){
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
Date date = sdf.parse(mydate);
SimpleDateFormat sdf2 = new SimpleDateFormat("dd.MM.yyyy", Locale.US);
return sdf2.format(date);
}catch(Exception e){
return null;
}
}
And check it with:
String val = "2011-05-31T00:00:00.000+03:00";
String res = getMydate(val);
0
Use SimpleDateFormat
check the API: http://developer.android.com/reference/java/text/SimpleDateFormat.html

EvZ
- 11,889
- 4
- 38
- 76