Domanda

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?

È stato utile?

Soluzione

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);    

Altri suggerimenti

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top