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,548
  • 5
  • 37
  • 54
Ivar Reukers
  • 7,560
  • 9
  • 56
  • 99

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,548
  • 5
  • 37
  • 54
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
  • 44,416
  • 5
  • 53
  • 99