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.
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.
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};
}
}
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];
}
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.