-5

I have this string: 7 -Jun- 2014. I want to convert to java.utils.Date;

I use this Code

 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String dateInString = "7-Jun-2013";

        try {

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

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

but I get this exception :

java.text.ParseException: Unparseable date: "7-Jun-2013"
at java.text.DateFormat.parse(Unknown Source)
at ma.abcsolution.util.Test.main(Test.java:15)
devlopp
  • 3
  • 1
  • 7
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – xbilek18 Nov 03 '14 at 12:02
  • 5
    You gave the date to it in the format of `d-MMM-yyyy` for a SimpleDateFormat of `dd/MM/yyyy`, obviously it crashes. Look at how to create date string for date format: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – EpicPandaForce Nov 03 '14 at 12:03
  • the dateformat that you have provided for SimpleDateFormat and your search string don't match. change you date format to dd-MMM-YYYY – vikeng21 Nov 03 '14 at 12:04

5 Answers5

1

try using

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");

the SimpleDateFormat look at the given string like it you tell it to, so in your example it look for a string with 2 chars for days, followed by a '/' sign and then 2 chars for month and so on

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

Use

 SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");

to format the date. You used the wrong format with the / signs.

See also:

L.Butz
  • 2,466
  • 25
  • 44
0

Using SimpleDateFormatter you can convert from date string to date and date to date String

public final class DateUtil {

    private static final String DEFAULT_DATE_FORMAT = "dd-MMM-yyyy";

    private DateUtil() {
    }

    public static final String formatDate(final Date date, final String dateFormat) {
        DateFormat format = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
        return format.format(date);
    }

    public static final Date formatDate(final String date) throws ParseException {
        return formatDate(date, DEFAULT_DATE_FORMAT);
    }

    public static final Date formatDate(final String date, final String dateFormat) throws ParseException {
        DateFormat format = new SimpleDateFormat(dateFormat);
        return format.parse(date);
    }
}

Whenever you are going yo format date please verify the format you are using In your case you used dd/MM/yyyy but date you used in format dd-MMM-yyyy.

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
0
Date date = new SimpleDateFormat("d-MMM-yyyy").parse("7-Jun-2014");
Sahil Jain
  • 177
  • 2
  • 11
  • i get this exception : Exception in thread "main" java.text.ParseException: Unparseable date: "7-Jun-2014" at java.text.DateFormat.parse(Unknown Source) at ma.abcsolution.util.Test.main(Test.java:10) – devlopp Nov 03 '14 at 12:22
0

Use this code. It is simple.

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

public class DateTime {

    public static void main(String args[]){
        SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
        String dateInString = "7-Jun-2013";

        Date date=new Date(dateInString);

        System.out.println(ft.format(date));


    }
}
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46