-2

I am facing some problem to parse the date format dd-m-y with SimpleDateFormat class in Java.

So is there any date formatter for this date format type (12-oct-14)?

9
  • What do you mean by counting the number of days? since when until what? Commented Mar 22, 2018 at 8:18
  • @ClaudiuGuja I have to calculate no of days between two dates of this format Commented Mar 22, 2018 at 9:25
  • @OleV.V. No i am talking about specific date format Commented Mar 22, 2018 at 10:07
  • Yes — and what did you try? Where did your search bring you? If you were unsuccessful piecing a solution together from what you found on the net, you should still tell us how your attempts failed. We’re more willing to help when we see an effort, and we can help you much better when we understand your specific problem with this task. Commented Mar 22, 2018 at 10:22
  • @OleV.V. why you downvote my question? I am just beginner in java and after searching for this topic, I seeking help here to complete my project. Commented Mar 22, 2018 at 10:47

4 Answers 4

6
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy")

If you are using Java 8 or 9, then please refer this

Sign up to request clarification or add additional context in comments.

3 Comments

Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not as the first option. And not without any reservation. Today we have so much better in java.time, the modern Java date and time API and its DateTimeFormatter.
FYI, the troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat are now legacy, supplanted by the java.time classes built into Java 8 & Java 9. See Tutorial by Oracle.
@BasilBourque I have updated my answer with a link that shows how it can be done in Java 8 way.
2

The other answers might have worked, but there's a little detail that most people often forget.

The month name in your input ("oct") is in English (or in some other language where October's abbreviation is "oct", but I'm assuming English here, as this doesn't change the answer).

When you create a SimpleDateFormat, it uses the JVM default locale (represented by the class java.util.Locale). In your case, new SimpleDateFormat("dd-MMM-yy") worked because your JVM's default locale probably is already English.

But the default locale can be changed, even at runtime, and even by other applications running in the same JVM. In other words, you have no control over it and no guarantees at all that the default will always be English.

In this case, if you know that the input is always in English, it's safer and much better to explicity use a java.util.Locale in the formatter:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);

Even better is to use - if available to you - the Java 8's date/time API. This API is much better than SimpleDateFormat, because it solves many of the problems this class has.

The code might look harder in the beginning, but it's totally worth to learn the new API:

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
    // case insensitive, for month name
    .parseCaseInsensitive()
    // pattern for day-month-year
    .appendPattern("dd-MMM-yy")
    // use English for month name
    .toFormatter(Locale.ENGLISH);
System.out.println(LocalDate.parse("12-oct-14", fmt));

1 Comment

Yes, definitely avoid SimpleDateFormat and use java.time classes instead. See Oracle Tutorial.
1

First step: create a DateTimeFormatter by using the format you need and the ofPattern() method.

Second step: create two LocalDate objects with the .parse(CharSequence text, DateTimeFormatter format) method.

Third step: use firstDate.untill(secondDate) and receive a Period object.

Fourth step: use .getDays() method to get the number of days from the Period object.

2 Comments

Since the string doesn’t contain time of day, those should probably be LocalDate objects (as opposed to LocalDateTime).
thank you, it was a typo on my part, will edit now
1
String format = "dd-MMM-yy";

SimpleDateFormat sdf = new SimpleDateFormat(format);
System.out.println(sdf.format(new Date()));
System.out.println(sdf.parse("12-oct-14"));

Result:

22-Mar-18
Sun Oct 12 00:00:00 UTC 2014

2 Comments

How is you answer different to the one that Tino already posted?
@Eyildiz Thanks it works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.