3

I will have a String input in the style of:

yyyy-MM-dd'T'HH:mm:ssZ

Is it possible to convert this String into a date, and after, parsing it into a readable dd-MM-yyyy Date Object?

user2004685
  • 9,254
  • 5
  • 33
  • 51
Ivar Reukers
  • 7,370
  • 8
  • 51
  • 93

2 Answers2

7

Yes. It can be done in two parts as follows:

  1. Parse your String to Date object

    SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    Date dt = sd1.parse(myString);
    
  2. Format the Date object to desirable format

    SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
    String newDate = sd2.format(dt);
    System.out.println(newDate);
    

You will have to use two different SimpleDateFormat since the two date formats are different.

Input:

2015-01-12T10:02:00+0530

Output:

2015-01-12
user2004685
  • 9,254
  • 5
  • 33
  • 51
2
//Parse the string into a date variable
Date parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);

//Now reformat it using desired display pattern:
String displayDate = new SimpleDateFormat("dd-MM-yyyy").format(parsedDate);
ernest_k
  • 42,928
  • 5
  • 50
  • 93