4 Replies Latest reply: Feb 20, 2011 8:27 PM by tschodt RSS

    Find Day of Year

    841078
      I understand that Calendar.DAY_OF_YEAR is not supported in JavaME - so does anyone know a quick way to find out what the day of year of a specific date is?

      Cheers
        • 1. Re: Find Day of Year
          tschodt
          838075 wrote:
          I understand that Calendar.DAY_OF_YEAR is not supported in JavaME - so
          does anyone know a quick way to find out what the day of year of a specific date is?
          For a given year, month (Jan=1, ... , Dec=12), day;
              int dayno(int y,int m,int d) {
                  java.util.Calendar c = java.util.Calendar.getInstance();
                  c.set(y,m-1,d);
                  java.util.Calendar a = java.util.Calendar.getInstance();
                  a.set(y,0,1);
                  return (int)((c.getTime().getTime()-a.getTime().getTime())/86400000)+1; // there is a bug on this line
              }
          Using floating point but not Calendar (floating point not supported on CLDC 1.0);
              int dayno(int y,int m,int d) {
                  if (m++<3) { m += 12; d -= 365; }
                  else { if (y%4==0&&y%100!=0||y%400==0) ++d; }
                  d += (int)(30.6*m)-63;
                  return d; 
              }
          Using short (suitable for Java Card);
              short dayno(short y,short m,short d) {
                  for(short i=1;i<m;++i) {
                      switch (i) {
                          case 2: d -= (y%4==0&&y%100!=0||y%400==0)?1:2; 
                          case 4: case 6: case 9: case 11: d -= 1;
                      }
                      d += 31;
                  }
                  return d;
              }
          Edited by: tschodt on Feb 21, 2011 1:41 AM
          • 2. Re: Find Day of Year
            841078
            Thanks - that worked a treat
            • 3. Re: Find Day of Year
              tschodt
              838075 wrote:
              Thanks - that worked a treat
              Well, I just discovered there is a bug, did you find it?
              • 4. Re: Find Day of Year
                tschodt
                tschodt wrote:
                Using floating point but not Calendar (floating point not supported on CLDC 1.0);
                int dayno(int y,int m,int d) {
                if (m++<3) { m += 12; d -= 365; }
                else { if (y%4==0&&y%100!=0||y%400==0) ++d; }
                d += (int)(30.6*m)-63;
                return d; 
                }
                No need to use floating point (could even be adapted for Java Card);
                    int dayno(int y,int m,int d) {
                        if (m++<3) { m += 12; d -= 365; }
                        else { if (y%4==0&&y%100!=0||y%400==0) ++d; }
                        d += (306*m)/10-63;
                        return d; 
                    }
                Using short (suitable for Java Card);
                short dayno(short y,short m,short d) {
                for(short i=1;i<m;++i) {
                switch (i) {
                case 2: d -= (y%4==0&&y%100!=0||y%400==0)?1:2; 
                case 4: case 6: case 9: case 11: d -= 1;
                }
                d += 31;
                }
                return d;
                }
                As the Java Card enthusiasts seem obsessed with all types of memory usage
                this produces slightly skinnier byte code (down from 116 to 89)
                    public final short dayno(short y,short m,short d) {
                        for(short i=1;i<m;++i) {
                            d += i>2?(i%5)%3==1?30:31:i<2?31:y%4==0&&y%100!=0||y%400==0?29:28;
                        }
                        return d;
                    }