Domanda

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();
        }
È stato utile?

Soluzione

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

Altri suggerimenti

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"

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

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();
}

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();
    }
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

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top