0

How can i find the day of the month using the day of the year?

if(day >=1 && day <=31)
{
   month = 1;
   monthName = "January";
}

I have a day and year input. I separated year days into 12 months.

Survivorr
  • 83
  • 3
  • 10
  • 3
    What are your constraints? Can you use the inbuilt APIs? If you can, have you checked the APIs to find out what's available? If you did, what did you find, why didn't it work for you? – MadProgrammer Mar 01 '17 at 01:34
  • You can't do it without knowing the year. – shmosel Mar 01 '17 at 01:35
  • No APIs and no calendar – Survivorr Mar 01 '17 at 01:42
  • First you need to know if it's a leap year, because it affects all dates after march 1st. As you can't use any API, you can subtract the number of days for each month until you reach the day. You could also post a code snippet so people can see what you're doing and suggest proper changes –  Mar 01 '17 at 01:43
  • Add the code snippet, what have you tried so far ? – Srikanth Balaji Mar 01 '17 at 01:43

3 Answers3

1

Here is a straight forward implementation for your problem, which does not need any imports:

public class DaysToDayOfMonth {

    public static void main(String []args){
        // a leap year has 366 days
        int[] x = ConvertDaysToDayOfMonth(true, 366);
        System.out.println("month:" + x[0] + " day: " + x[1]);
        // in leap years 60 is feb 29
        x = ConvertDaysToDayOfMonth(true, 60);
        System.out.println("month:" + x[0] + " day: " + x[1]);
        // if it is not a leap year, 60 is march 1
        x = ConvertDaysToDayOfMonth(false, 60);
        System.out.println("month:" + x[0] + " day: " + x[1]);
    }

    public static int[] ConvertDaysToDayOfMonth(boolean leapYear, int days) {
        int month = 0;
        int dayOfMonth = days;
        if(dayOfMonth <= 31) return new int[] {month, dayOfMonth};
        dayOfMonth -= 31;
        month++;
        int febdays = (leapYear ? 29 : 28);
        if(dayOfMonth <= febdays) return new int[] {month, dayOfMonth};
        dayOfMonth -= febdays;
        month++;

        // march to july
        for(int i = 0; i < 5; i++) {
            int monthdays = (i%2==0 ? 31 : 30);
            if(dayOfMonth <= monthdays) return new int[]{month, dayOfMonth};
            dayOfMonth -= monthdays;
            month++;
        }
        // august to december
        for(int i = 0; i < 5; i++) {
            int monthdays = (i%2==0 ? 31 : 30);
            if(dayOfMonth <= monthdays) return new int[]{month, dayOfMonth};
            dayOfMonth -= monthdays;
            month++;
        }
        return new int[]{month, dayOfMonth};
    }
}
r3bel
  • 1,350
  • 8
  • 12
1

You could do something like this:

Create and array that contains the numbers of days for each month:

int[] daysInMonth=new int[13];
int[1]=31; // January (month 1) has 31 days
int[2]=28; // or 29 if it's a leap year 

And so on. Note that I created an array with 13 positions but I'm ignoring position 0 just to make January as month 1 instead of 0.

Then you do a loop like that:

int n = // day of year 
for (int i = 1; i < daysInMonth.length; i++) {
    if(n <= daysInMonth[i])
         break;// n is the day of month i
    else n -= daysInMonth[i];
}
  • What if i already gave a value to month like in the code i edited above? – Survivorr Mar 01 '17 at 15:07
  • I think you should choose between using the loop above or use lots of ifs (like your original code). In the loop I suggested, you don't need that "if" you used. –  Mar 01 '17 at 15:10
1

I think this code will help you to find the date for your input. The input you provide is called Julian Calendar. You can refer this code for your reference.

public class TestClass8 
{
    public static void main(String[] args) throws IOException, ParseException
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getFormattedJulianDateOfString("dddyy", "3216"));

        System.out.println("day   :"+cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("month :"+cal.get(Calendar.MONTH));
        System.out.println("year  :"+cal.get(Calendar.YEAR));
    }

    public static Date getFormattedJulianDateOfString(String dateFormat, String dateString)
            throws ParseException
    {

        Date uploadedDate = null;
        if(StringUtils.countMatches(dateFormat, "y") == 0)
        {
            dateFormat = dateFormat.concat("yy");
            dateString = dateString.concat(getFormattedDate(new Date(), "yy"));

            if(dateFormat.length() != dateString.length())
            {
                for(int i= 0; i < Math.abs(dateString.length() - dateFormat.length()); i++)
                {
                    dateString = "0"+dateString;
                }
            }

            uploadedDate = getFormattedUploadedDate(dateFormat, dateString, true);
        }

        if(StringUtils.countMatches(dateFormat, "y") == 1)
        {
            dateFormat = dateFormat.concat("y");
            String currentYear = getFormattedDate(new Date(), "yy");
            currentYear = StringUtils.substring(currentYear, 0, currentYear.length() - 1);
            currentYear = currentYear.concat(StringUtils.substring(dateString, dateString.length() - 1, 
                                                                   dateString.length()));

            dateString = StringUtils.substring(dateString, 0, dateString.length() - 1);
            dateString = dateString.concat(currentYear);

            if(dateFormat.length() != dateString.length())
            {
                dateString = "0"+dateString;
            }

            uploadedDate = getFormattedUploadedDate(dateFormat, dateString, true);
        }

        if(StringUtils.countMatches(dateFormat, "y") == 2)
        {
            uploadedDate = getFormattedUploadedDate(dateFormat, dateString, true);
        }
        return uploadedDate;
    }


    public static String getFormattedDate(Date date, String requiredFormat)
    {
        String formattedDate = "";
        DateFormat formatter = new SimpleDateFormat(requiredFormat);
        formattedDate = formatter.format(date);
        return formattedDate;
    }


    public static Date getFormattedUploadedDate(String dateFormat, String dateString, boolean isJulianDateFormat) 
            throws ParseException
    {
        Date date = null;

        DateFormat sdf = new SimpleDateFormat(dateFormat);
        if(!isJulianDateFormat)
        {
            sdf.setLenient(false);
        }
        date = sdf.parse(dateString);
        if(!isJulianDateFormat && !dateString.equals(sdf.format(date)))
        {
            date = null;
        }

        return date;
    }
}

Finally you can get the day of the month, month of the year and the year as a result for your input.

You have to give input for the method getFormattedJulianDateOfString() as the dateFormat which you need to convert like ddd, dddy, dddyy.

If you give the date format argument as ddd then you have to provide the number of the day in the argument of dateString as you said in your question like 18 or 32 like.

If you give the dateFormat argument as dddy then you have to provide the number of the day in the argument of dateString as 186, 18 is the day of the year and 6 is says that the year of 2016.

If you give the dateFormat argument as dddy then you have to provide the number of the day in the argument of dateString as 1816, 18 is the day of the year and 16 is says that the year of 2016.

Bhuvanesh Waran
  • 593
  • 12
  • 28