0

I have two numbers. First would be the month number. Second would be the day number.

I also know the year.

How can I take those two number, month and day and make it into a single number DAY_OF_YEAR?

I'm using Java 1.7 and the Calendar functions.

  • 1
    Consider not using `Calendar`, even on Java 1.7. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) through the backport, [ThreeTen Backport](https://www.threeten.org/threetenbp/). – Ole V.V. Jan 24 '19 at 05:21
  • 1
    Please tell us what your search and research brought up and in what way it was insufficient. Please tell us what you have already tried and how it failed. We can guide you much more precisely from there. Also showing an effort has a good effect on people’s willingness to answer. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Ole V.V. Jan 24 '19 at 06:19

3 Answers3

1

java.time through ThreeTen Backport

    int year = 2019;
    int monthNumber = 9;
    int dayNumber = 28;

    LocalDate date = LocalDate.of(year, monthNumber, dayNumber);
    int dayOfYear = date.getDayOfYear();

    System.out.println("Day of year is " + dayOfYear);

Output from this snippet is:

Day of year is 271

Tested on Java 1.7.0_79 using ThreeTen Backport 1.3.6 and importing org.threeten.bp.LocalDate.

Consider avoiding the Calendar class

Four lines of the currently accepted answer creating and setting the Calendar object are substituted by just one line here. It’s typical for code using Calendar to be so wordy, which we shouldn’t want. Also despite the name a Calendar object is more than a calendar date, it also carries with it a time of day, a time zone and more, so I do not consider it a good fit for the job at hand. The code using Calendar will give a different result if the default locale is Thai, which will surprise most. The Calendar class is poorly designed and long outdated.

Instead I am using LocalDate of java.time, the modern Java date and time API.

Question: Can I use java.time on Java 7?

I'm using Java 1.7 …

java.time works nicely on Java 7. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Just using calendar and assuming by number of month you mean the zero-indexed one where 0 means January, here is an example for May 13th 2019:

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
c.set(Calendar.YEAR, 2019);
c.set(Calendar.MONTH, 4);
c.set(Calendar.DAY_OF_MONTH, 13);
System.out.println(c.get(Calendar.DAY_OF_YEAR));

Edit: As Ole V.V.'s answer pointed out you get a Calendar object with the system's timezone if you call 'Calendar.getInstance()', so I changed it that way that you explicitly specify the timezone and Locale to be used. The latter is important if you e.g. want to get a date's week number where the rules differ in different regions of the world.

Lothar
  • 5,323
  • 1
  • 11
  • 27
0

You can concat your parts into a String, and use a SimpleDateFormatter like:

int year = 2019;
int month = 1;
int day = 23;

String string = "" + year + "-" + month + "-" + day;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(string);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));

The example is for 2019 January 23.

aBnormaLz
  • 809
  • 6
  • 22
  • Hand formatting and parsing back, that’s certainly the detour. When the question is asking about the poorly designed `Calendar` class, you are suggesting the likewise outdated and even more troublesome `SimpleDateFormat`. I recommend against it. – Ole V.V. Jan 24 '19 at 06:23