22

I want to get the number of day.. i.e. Jan 1 is day 1 jan 2 is day 2 Feb 1 is day 32 and december 31 is day 365 or 366 depending on leap year or not

i have used all kind of techniques such as date1 - date2 etc... but nothing seems to work for me cant get the logic right may be.. what i want is count and add the number of the months that has gone past plus the number days of the running month i.e today is 21st Sept 2012 is day number (31(jan)+29(feb)+31(mar)+30(apr)+31(may)+30(june)+31(july)+31(aug)+20(sept)) = 264th day and they will keep adding plus one every time a day go past... thanks

mycode

int year = Calendar.getInstance().get(Calendar.YEAR);
            GregorianCalendar gc = new GregorianCalendar();
            gc.set(GregorianCalendar.DAY_OF_MONTH, 8);
            gc.set(GregorianCalendar.MONTH, GregorianCalendar.JUNE);
            gc.set(GregorianCalendar.YEAR, year);
            int numberofDaysPassed=gc.get(GregorianCalendar.DAY_OF_YEAR);

numberofDaysPassed is giving me 160, undesired result

  • for the code you're using , it is the right answer june 8th 2012 is the 160th day of the year – Manoj Kumar Sep 21 '12 at 06:51
  • @onkar have you test that code. with 11,12,13,111,112,113 days..??? if not then check it first... – V.J. Sep 21 '12 at 07:01
  • Hey mate I dont need that siffux part..st,nd,rd..... –  Sep 21 '12 at 07:08
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 29 '17 at 00:01

6 Answers6

43
Calendar calendar = Calendar.getInstance();
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);  

Or using Joda-API

DateTime dt = new DateTime();  
int dayOfYear = dt.getDayOfYear();  

If you need 'th' part, use switch statement

switch (dayOfYear > 20 ? (dayOfYear % 10) : dayOfYear) {
            case 1:  return dayOfYear + "st";
                     break;
            case 2:  return dayOfYear + "nd";
                     break;
            case 3:  return dayOfYear + "rd";
                     break;
            default:  return dayOfYear + "th";
                     break;   
} 
weston
  • 54,145
  • 21
  • 145
  • 203
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • 4
    About your `switch` statement: What happens if `dayOfYear` is for example 11, or 21? (You'd want: 11th, 21st). – Jesper Sep 21 '12 at 07:18
  • All numbers that != 1 and != 2 and != 3 will have ending `th`. – Ilya Sep 21 '12 at 07:41
  • 2
    @Jesper You can use `switch(dayOfYear > 20 ? (dayOfYear % 10) : dayOfYear)` – Ridcully Sep 21 '12 at 07:49
  • 3
    @Ilya My comment was to indicate that it's wrong. If `dayOfYear` is 21, then you want 21st, not 21th, for example. Or 172 should give 172nd, not 172th. – Jesper Sep 21 '12 at 08:13
  • @Jesper Sorry, enlish is not my native language. In this case, Ridcully's comment is very helpful – Ilya Sep 21 '12 at 08:15
  • @Ridcully, I adapted the code with your suggestion. – thecarpy Nov 03 '15 at 14:43
  • 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). And *java.time* succeeds Joda-Time as well. – Basil Bourque Jul 04 '18 at 23:23
12

LocalDate

Use the LocalDate class in java.time package built into Java 8 and later.

Get the day-of-year:

int dayOfYear = LocalDate.now().getDayOfYear();

…and set the day-of-year:

LocalDate localDate = LocalDate.now().withDayOfYear( 195 );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
4

Try setting the date on the calendar to the date in the problem, you asked for 21st Sept but you put 8th of June in the code.

Here is the updated code that gives 265 instead:

    int year = Calendar.getInstance().get(Calendar.YEAR);
    GregorianCalendar gc = new GregorianCalendar();
    gc.set(Calendar.DAY_OF_MONTH, 21); // you asked for 21st Sept but put 8
    gc.set(Calendar.MONTH, Calendar.SEPTEMBER); // you aksed for 21st Sept but put JUNE
    gc.set(Calendar.YEAR, year);
    int numberofDaysPassed = gc.get(Calendar.DAY_OF_YEAR);
    System.out.println(numberofDaysPassed);

By the way you don't need to set the month, day etc. on Calendar, it defaults to 'now'...

Adam
  • 35,919
  • 9
  • 100
  • 137
  • Hey for this answer I suppose I need enter the day,month,year manually I suppose, that one is giving me directly... –  Sep 21 '12 at 07:22
3

Using Java 8 you can do this: int n = LocalDate.now().get(ChronoField.DAY_OF_YEAR);

codesalsa
  • 882
  • 5
  • 18
2
Calendar ca1 = Calendar.getInstance();    
int DAY_OF_YEAR=ca1.get(Calendar.DAY_OF_YEAR);
System.out.println("Day of Year :"+DAY_OF_YEAR);

Check the result in your logcat..

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
1
DateTime dt = new DateTime();  
String dayOfYear = dt.getDayOfYear().toString();
String day = "";

if(dayOfYear.endsWith("1") && !dayOfYear.endsWith("11"))
    day = dayOfYear+"st";
else if(dayOfYear.endsWith("2") && !dayOfYear.endsWith("12"))
    day = dayOfYear+"nd";
else if(dayOfYear.endsWith("3") && !dayOfYear.endsWith("13"))
    day = dayOfYear+"rd";
else 
    day = dayOfYear+"th";

System.out.println("Day of year :- "+ day);
V.J.
  • 9,492
  • 4
  • 33
  • 49