Requirements: According to the date of the days, weeks, weeks
Train of thought: try to write a date class without Java encapsulation, attributes: Year, month, day, first to determine whether the date is legal,
First function Calculation days: The month before the one months and the first month of the difference between days, in addition to the current month to the number of days;
The second function calculates the week ordinal: The number of days that are evaluated with the first is 7. The date is the week ordinal of the current year;
The third function calculates the day of the week: first we need to find a coordinate, based on this coordinates for the number of weeks, the computer world is based on January 1, 1970, but we know that January 1, 70 is Thursday; This is the one-week two-dimensional array we created, subscript 0 for Thursday, Subscript 6 for Wednesday; then we find out the date from January 1, 1970 in the number of days in the 7, the remainder of 1 (-1 is because the number to represent the subscript), and then use the subscript to look for the corresponding weeks in the array, so you can get the week. (Code below)
public class MyDate {private int year;
private int month;
private int day;
Public MyDate () {super ();
Public mydate (int year, int month, int day) {super ();
This.year = year;
This.month = month;
This.day = day;
public int getyear () {return year;
public int getmonth () {return month;
public int getday () {return day;
/** * * * @return * * * The first day of the year * * public static int day (mydate date) {int years = Date.getyear ();
int months = Date.getmonth ();
int days = Date.getday ();
if (!feifapanduan (years, months, days)) {System.out.println ("Enter the year illegal!");
return-1;
/** * Calculates the number of days in the current year */int sum = 0;
for (int i = 1; i < months i++) {sum + = ym (years, i);
Returns the number of days of this year return sum + day;
/** * weeks/public static void weeks (mydate date) {int sum = day (date);
int weeks = SUM/7;
if (sum% 7 = 0) {System.out.println ("cap" + weeks + "Week");
else {int week = weeks + 1; System. OUT.PRINTLN ("First" + Week + "Week"); }/** * Week/public static void Weekxingqi (mydate date) {string[] Wekesday = new string[] {"Thursday", "Friday",
"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"};
int year = Date.year;
int sum = day (date);
/** * Calculate the date from 70 to meet the conditions 6+n<=day<=6+7n * */int yearsum = 0;
for (int a = 1970 a < year a++) {if (a% 4 = 0 && A%!= 0 | | A% = = 0) {yearsum = 366;
else {yearsum = 365;
}//define the number of days from 70 int ymsum = yearsum + sum;
Number of days to 7 to get int weekday = ymsum% 7;
SYSTEM.OUT.PRINTLN ("Distance 70" + ymsum + "" + "remainder" + weekday); /** * Cycle Week data according to the remainder to find the week/for (int i = 0; i < wekesday.length; i++) {if (i = = weekday-1) {System.out.
println (Wekesday[i]); }}/** * Determines whether leap year, select Month * * @param years * @param month * @return/public static int ym (int years, in
T month) {int days = 0; Switch (month) {case 1:case 3:case 5:case 7:cASE 8:case 10:case 12:days = 31;
Break
Case 4:case 6:case 9:case 11:days = 30;
Break
Case 2:if (years% 4 = 0 && years%!= 0 | | years% = = 0) {days = 29;
else {days = 28;
} break;
Default:break;
return days; /** * To determine whether the date entered is valid * * @param year * @param month * @param ri * @return/public static Boolean Feifapa Nduan (int year, int month, int ri) {if [year!= 0 && month > 0 && month < && Ri < y
M (year, month)) {return true;
return false; }
}
Test code:
@SuppressWarnings ("static-access") public
static void Main (string[] args) {
//TODO auto-generated method stub< C2/>mydate date = new MyDate (2018, 2,);
SYSTEM.OUT.PRINTLN ("The Year" + date.day (date) + "Day");
Date.weeks (date);
Date.weekxingqi (date);
Run Result:
OK, write to share, the code can run directly