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?

3 Answers 3

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);    
Sign up to request clarification or add additional context in comments.

4 Comments

you make something wrong, edit your question and add your code to add string to arraylist
ok I changed "SSSXXX" to "SSSZ" and worked but "newDataString" gets 30.05.2011 instead of 31.05.2011
I just have tried and new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); returned newDateString = 31.05.2011
yeah that Z letter made difference now its working, thanks. Do you know why these Z, X letters change the output?
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);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.