public static int dayOfYear(int month, int dayOfMonth, int year) {
if (month == 2) {
dayOfMonth += 31;
} else if (month == 3) {
dayOfMonth += 59;
} else if (month == 4) {
dayOfMonth += 90;
} else if (month == 5) {
dayOfMonth += 31 + 28 + 31 + 30;
} else if (month == 6) {
dayOfMonth += 31 + 28 + 31 + 30 + 31;
} else if (month == 7) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30;
} else if (month == 8) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31;
} else if (month == 9) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
} else if (month == 10) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
} else if (month == 11) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
} else if (month == 12) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31;
}
return dayOfMonth;
}
Public Sub dayOfYear (month As Int, dayOfMonth As Int,year As Int) As Int
Dim Sum() As Int = Array As Int (0,0,31,59,90,120,151,181,212,243,273,304,334)
Return dayOfMonth + Sum(month)
End Sub
Public Sub dayOfYear (month As Int, dayOfMonth As Int,year As Int) As Int
Dim Sum() As Int = Array As Int (0,31, 28, 31, 30, 31, 30, 31, 31, 30,31, 30)
For i=1 To month
dayOfMonth = dayOfMonth + Sum(i)
Next
Return dayOfMonth
End Sub
public static int dayOfYear(int month, int dayOfMonth, int year) {
int mdays[] = {31, 28+((year&3)?0:1), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return( (month>1) ? ( dayOfYear( month-1, mdays[month-1] + dayOfMonth, year)) : dayOfMonth );
}
public class Main {
public static void main(String[] args) {
System.out.println(getdayofyear(12,3,2000));
}
public static int getdayofyear(int day, int month, int year){
int[] days = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
//if (day > days[month-1]){System.out.println("you have entered a wrong value."); return 0;} //check if day is correct
for (int i=0;i<month-1;i++){day += days[i];}
//if (year > 1900 && year % 4 == 0){day += 1;} //calculate the leap year
return day;
}
}
Public Sub dayOfYear (month As Int, dayOfMonth As Int,year As Int) As Int
Dim Sum() As Int = Array As Int (0,0,31,59,90,120,151,181,212,243,273,304,334)
Return dayOfMonth + Sum(month)
End Sub
public static int dayOfYear(int month, int dayOfMonth, int year) {
int mdays[] = {31, 28+((year&3)?0:1), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return( (month>1) ? ( dayOfYear( month-1, mdays[month-1] + dayOfMonth, year)) : dayOfMonth );
}
HEREreally smart. and yours jordi is also really smart. can you please explain this sign ? in your code. what means ? inside the code?
I have to thank my informatics teacher (of high school)i must say that i am very surprised. both of you beat me with 2 lines
this was my code:
B4X:public class Main { public static void main(String[] args) { System.out.println(getdayofyear(12,3,2000)); } public static int getdayofyear(int day, int month, int year){ int[] days = new int[]{31,28,31,30,31,30,31,31,30,31,30,31}; //if (day > days[month-1]){System.out.println("you have entered a wrong value."); return 0;} //check if day is correct for (int i=0;i<month-1;i++){day += days[i];} //if (year > 1900 && year % 4 == 0){day += 1;} //calculate the leap year return day; } }
how did i not think of doing it like this:
code?
Public Sub dayOfYear (month As Int, dayOfMonth As Int,year As Int) As Int
Dim bissextile As Int = 2*(0.5-Min((year Mod 4),0.5)+Min((year Mod 100),0.5)-Min((year Mod 400),0.5))
Dim Sum() As Int = Array As Int (0,0,31,59,90,120,151,181,212,243,273,304,335)
Return dayOfMonth + Sum(month) + (bissextile * Floor(Min(month,2)/2))
End Sub
Dim localDate As JavaObject
localDate.InitializeStatic("java.time.LocalDate")
Log(localDate.RunMethodjo("of",Array(2019,12,31)).RunMethod("getDayOfYear",Null))
Dim localDate as JavaObject
log(localDate.InitializeStatic("java.time.LocalDate").RunMethodJO("of",Array(2019,12,31)).RunMethod("getDayOfYear",null))
It is an Ternary Operator.can you please explain this sign ? in your code. what means ? inside the code?
In Java this expression evaluates to:
If foo is selected, assign selected foo to bar. If not, assign baz to bar.
Object bar = foo.isSelected() ? foo : baz;
It is an Ternary Operator.
if a>b then c else d
B4X:Sub DayOfYear(Month As Int, DayOfMonth As Int, Year As Int) As Int Return DateTime.GetDayOfYear(DateUtils.SetDate(Year, Month, DayOfMonth)) End Sub
date --date="2019-01-19" "+%j"
no cheating allowed
Surely a little cheating is allowed?
public function dayOfYear($month, $dayOfMonth)
{
return [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 31][$month] + $dayOfMonth;
}
Same as IIF in vb.net.in b4x it would be like:
B4X:if a>b then c else d
Public Sub dayOfYear (month As Int, dayOfMonth As Int,year As Int) As Int
Return dayOfMonth+Bit.ParseInt(Regex.Split(",","0,0,31,59,90,120,151,181,212,243,273,304,335")(month),10)+2*(0.5-Min((year Mod 4),0.5)+Min((year Mod 100),0.5)-Min((year Mod 400),0.5))*Floor(Min(month,2)/2)
End Sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?