shimbu shambu
shimbu shambu

Reputation: 125

convert date into different format

I am trying to convert date into user required format. I want date in all format. But formatted date is wrong please help.

package DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormat
{
    DateFormat() throws ParseException
    {
    String dateFormats[] =
    {
            "YYYY/MM/DD",
            "DD/MM/YYYY",
            "DD-MM-YYYY",
    };


    for (int i = 0; i < dateFormats.length; i++)
    {

        String newDate = new SimpleDateFormat(dateFormats[i]).format(new Date());
        System.out.println(newDate);
    }

    }

    public static void main(String[] args) throws ParseException
    {
    new DateFormat();
    }
}

output is

2016/04/98
98/04/2016
98-04-2016

Thank you.

Upvotes: 0

Views: 86

Answers (2)

vssk
vssk

Reputation: 465

It's going wrong because of java code syntax case sensitive. Pls check the right date and time pattern at https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Your String array of patterns should be transformed to:

String dateFormats[] =
{
        "yyyy/MM/dd",
        "dd/MM/yyyy",
        "dd-MM-yyyy",
};

Upvotes: 2

Natalia
Natalia

Reputation: 4532

D   Day in year Number  189
d   Day in month    Number  10

from https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

So, formats should look like:

String dateFormats[] =
{
        "yyyy/MM/dd",
        "dd/MM/yyyy",
        "dd-MM-yyyy",
};

Upvotes: 2

Related Questions