2

I have a simple program that asks a user to enter a date in a MM-dd-yyyy format. How can I get the day of the year from this input? For example if the user enters "06-10-2008" the day of the year would be the 162nd day considering this was a leap year.

Here's my code so far:

System.out.println("Please enter a date to view (MM/DD/2008):");

        String date = sc.next();

        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        Date date2=null;
        try {
            //Parsing the String
            date2 = dateFormat.parse(date);
        } catch (ParseException e) {                
            System.out.println("Invalid format, please enter the date in a MM-dd-yyyy format!");
            continue;
        } //End of catch
        System.out.println(date2);
    }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only notoriously troublesome, along with `Date` it is also long outdated. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 07 '18 at 13:13

3 Answers3

4

Assuming you are using Java 8+, you could use the LocalDate class to parse it with a DateTimeFormatter like

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM-dd-yyyy");
System.out.println(LocalDate.parse("06-10-2008", fmt).getDayOfYear());

Outputs (as requested)

162
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    This will work nicely in Java 6 and 7 too if you add [ThreeTen Backport](http://www.threeten.org/threetenbp/) to your project. On (older) Android use the Android edition of same: [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Apr 07 '18 at 13:12
3

Like this

Calendar cal = Calendar.getInstance();
cal.setTime(date2); //Assuming this is date2 variable from your code snippet
int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
Ivan
  • 8,508
  • 2
  • 19
  • 30
  • 2
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/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/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 07 '18 at 21:22
0
Calendar c = Calendar.getInstance();
c.setTime(date2);
System.out.println("Day of year = " + c.get(Calendar.DAY_OF_YEAR));   
Graciano
  • 508
  • 4
  • 11
  • 2
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/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/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 07 '18 at 21:23