Sinha
Sinha

Reputation: 803

String to date conversion

Following code

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy", new DateFormatSymbols(Locale.US));
        System.out.println(simpleDateFormat.parse("03-Apr-96"));

Gives output as Wed Apr 03 00:00:00 IST 1996

What should I do get the output like 1996-04-03 00:00:00.0

Upvotes: 0

Views: 86

Answers (3)

Obl Tobl
Obl Tobl

Reputation: 5684

Try the following:

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy", new DateFormatSymbols(Locale.US));
    Date d = simpleDateFormat.parse("03-Apr-96");  
    simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss.S");
    System.out.println(simpleDateFormat.format(d));

Upvotes: 1

asermax
asermax

Reputation: 3123

A DateFormat object can be either used to parse a String into a Date object or the other way around. In your example, you're doing the former, when what you really want to do is to format your Date into a given pattern.

Here's an example of what you may want to do:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.print(df.format(yourDateObject));

Upvotes: 0

user1965351
user1965351

Reputation: 21

Try this:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy", new DateFormatSymbols(Locale.US));
        Date date=simpleDateFormat.parse("03-Apr-96");

        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", new DateFormatSymbols(Locale.US));
        System.out.println(simpleDateFormat1.format(date));

Upvotes: 0

Related Questions