-2

i have problem of parsing a String into Date when the month contains 3 letters instead of two

user3505689
  • 97
  • 1
  • 1
  • 10
  • 5
    What is wrong with the doc ? http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – Thai Tran Jul 07 '14 at 08:41
  • SimpleDateFormat sdf = new SimpleDateFormat("dd-Mmm-yyy");sdf.parse("20-Feb-2006"); – user3505689 Jul 07 '14 at 08:42
  • 1
    @bubuzzz: reading manuals is an art no longer known to many people since the advent of search engines and forums. –  Jul 07 '14 at 08:44
  • possible duplicate of [How to parse date string to Date?](http://stackoverflow.com/questions/4496359/how-to-parse-date-string-to-date) – Basil Bourque Jul 07 '14 at 09:53

3 Answers3

4

use DateFormat in this way:

DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println( df.parse("20-Feb-2006"));
Jens
  • 67,715
  • 15
  • 98
  • 113
  • 3
    You need to supply a locale, otherwise this might fail if the current locale does not use `Feb` for Februar (e.g. in France) –  Jul 07 '14 at 08:43
  • it does not work.....maybe because it is Feb and not FEB or maybe i am doing it while parsing xml – user3505689 Jul 07 '14 at 08:53
  • @user3505689 in my test it works fine. What is the exception you get? Or do you get a wrong date? – Jens Jul 07 '14 at 08:56
1

USe

    String strDate = "20-Feb-2006";
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    Date dateStr = formatter.parse(strDate);
Cris
  • 12,799
  • 5
  • 35
  • 50
1
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date date = df.parse("20-Feb-2006");
SparkOn
  • 8,806
  • 4
  • 29
  • 34