0

Hello i have date in this format 2013-10-31T19:00:00Z now i want to display this date into yyyy/mm/dd this format.i have used this code but its giving me exception kindly help me

String startDateString = "2013-10-31T19:00:00Z";

        DateFormat df = new SimpleDateFormat("yyyy/mm/dd"); 
        Date startDate;
        try {
            startDate = df.parse(startDateString);
            String newDateString = df.format(startDate);
            System.out.println("Date is "+newDateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Junaid Akhtar
  • 317
  • 1
  • 3
  • 13

7 Answers7

2

You can use SimpleDateFormat's parse method to parse your date, and then use the format method to output it in the format that you prefer.

Try this:

String startDateString = "2013-10-31T19:00:00Z";

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd");

Date date = inputFormat.parse(startDateString);
System.out.println("Date is " + outputFormat.format(date));

Output:

Date is 2013/10/31
blacktide
  • 10,654
  • 8
  • 33
  • 53
2

That's because you're specifically telling Java to look for a 4-digit year followed by a 2-digit month and 2-digit date. Taken straight from the docs, what you want is

"yyyy-MM-dd'T'HH:mm:ssZ"
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
2

Try this

String DateStr="2013-10-31T19:00:00Z";
    SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
    Date d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr); 
System.out.println(sim.format(d));

output 2013-10-31

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

You need a separate SimpleDateFormat to parse the Datestring. Try this:

String startDateString = "2013-10-31T19:00:00Z";
DateFormat originalFormat = new SimpleDateFormat("yyyy-mm-dd'T'HH:MM:SS'Z'");
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
    startDate = originalFormat.parse(startDateString);
    String newDateString = df.format(startDate);
    System.out.println("Date is " + newDateString);
} catch (ParseException e) {
    e.printStackTrace();
}
Keppil
  • 45,603
  • 8
  • 97
  • 119
1

You need two different DateFormat. one for parse date and one for formatting date.

 String startDateString = "2013-10-31T19:00:00Z";

    DateFormat df = new SimpleDateFormat("yyyy/mm/dd"); 
    DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS'Z'"); 
    Date startDate;
    try {
        startDate =df1.parse(startDateString);
        String newDateString = df.format(startDate);
        System.out.println("Date is "+newDateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
bNd
  • 7,512
  • 7
  • 39
  • 72
1
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";

try {

    Date date = formatter.parse(dateInString);
    System.out.println(date);
    System.out.println(formatter.format(date));

} catch (ParseException e) {
    e.printStackTrace();
}

Here is simple example

Deepak
  • 2,287
  • 1
  • 23
  • 30
0

Try this:

        try {
            String startDateString = "2013-10-31T19:00:00Z";
            DateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
            DateFormat output = new SimpleDateFormat("yyyy/MM/dd");
            Date date = input.parse(startDateString);
            System.out.println("Date is " + output.format(date));
        } catch (ParseException e) {

            e.printStackTrace();
        }

Pointing an another simple mistake in your code

You coded as DateFormat df = new SimpleDateFormat("yyyy/mm/dd");

here mm is for minutes , for month use MM

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55