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);
Adding date value to ArrayList in Android
-
06-03-2022 - |
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?
Soluzione
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);
Use SimpleDateFormat
check the API: http://developer.android.com/reference/java/text/SimpleDateFormat.html
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow