0

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?

arenko
  • 174
  • 1
  • 4
  • 16

3 Answers3

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
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);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Corbella
  • 1,791
  • 14
  • 24
0

Use SimpleDateFormat check the API: http://developer.android.com/reference/java/text/SimpleDateFormat.html

EvZ
  • 11,889
  • 4
  • 38
  • 76