-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)?

pist
  • 165
  • 4
kunal
  • 163
  • 1
  • 14
  • What do you mean by counting the number of days? since when until what? – Claudiu Guja Mar 22 '18 at 08:18
  • @ClaudiuGuja I have to calculate no of days between two dates of this format – kunal Mar 22 '18 at 09:25
  • @OleV.V. No i am talking about specific date format – kunal Mar 22 '18 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. – Ole V.V. Mar 22 '18 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. – kunal Mar 22 '18 at 10:47
  • You guessed correctly, that was me downvoting your question. Because it is a very poor question. Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) for more. – Ole V.V. Mar 22 '18 at 11:43
  • @kunal Your Question deserves down-voting for two reasons at least: (a) You did not ask a specific technical question, and did not show any code attempts on which we could comment and fix. (b) You did not bother to search Stack Overflow before posting. This site is meant to be more like Wikipedia and less like an ongoing discussion board. You can assume at this point in time that any basic programming task such as parsing a date has already been asked and answered on Stack Overflow. – Basil Bourque Mar 23 '18 at 20:27
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 23 '18 at 20:33
  • @BasilBourque Thanks – kunal Mar 27 '18 at 06:24

4 Answers4

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

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

Tino M Thomas
  • 1,676
  • 2
  • 16
  • 24
  • 2
    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](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Mar 22 '18 at 11:45
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 23 '18 at 20:32
  • @BasilBourque I have updated my answer with a link that shows how it can be done in Java 8 way. – Tino M Thomas Mar 25 '18 at 05:20
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));
pist
  • 165
  • 4
  • Yes, *definitely* avoid `SimpleDateFormat` and use *java.time* classes instead. See [*Oracle Tutorial*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 23 '18 at 20:20
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

E. YILDIZ
  • 21
  • 2
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.

Claudiu Guja
  • 290
  • 1
  • 3
  • 12