Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sk.stuba.fei.Zadanie2cvikouloha1;
- public class MyDate {
- public int getDay() {
- return day;
- }
- public void setDay(int day) {
- this.day = day;
- }
- public int getMonth() {
- return month;
- }
- public void setMonth(int month) {
- this.month = month;
- }
- public int getYear() {
- return year;
- }
- public void setYear(int year) {
- this.year = year;
- }
- public String[] getStrMonthsSk() {
- return strMonthsSk;
- }
- public void setStrMonthsSk(String[] strMonthsSk) {
- this.strMonthsSk = strMonthsSk;
- }
- public String[] getStrDaysSk() {
- return strDaysSk;
- }
- public void setStrDaysSk(String[] strDaysSk) {
- this.strDaysSk = strDaysSk;
- }
- public int[] getDaysInMonths() {
- return daysInMonths;
- }
- public void setDaysInMonths(int[] daysInMonths) {
- this.daysInMonths = daysInMonths;
- }
- private int day;
- private int month;
- private int year;
- private static String[] strMonthsSk = {"Jan", "Feb", "Mar", "Apr", "Maj", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"};
- private static String[] strDaysSk = {"Po", "Ut", "Str", "Stv", "Pia", "So", "Ne"};
- private static int[] daysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- public MyDate(int day, int month, int year) {
- this.day = day;
- this.month = month;
- this.year = year;
- }
- public void setDate(int day, int month, int year) {
- this.day = day;
- this.month = month;
- this.year = year;
- }
- public static boolean isLeapYear(int year) {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true;
- return false;
- }
- public static boolean isValidDate(int day, int month, int year) {
- if (year >= 1 && year <= 9999) {
- if (month >= 1 && month <= 12) {
- if (month == 2) {
- if (isLeapYear(year)) {
- if (day >= 1 && day <= 29) return true;
- else return false;
- } else if (day >= 1 && day <= 28) return true;
- else return false;
- } else {
- if (day >= 1 && day <= daysInMonths[month - 1]) {
- return true;
- } else return false;
- }
- } else return false;
- } else return false;
- }
- public MyDate nextDay(int day, int month, int year) {
- if (month == 12 && day == 31) {
- this.year += 1;
- this.month = 1;
- } else if (month >= 1 && month < 12) {
- if (month == 2) {
- if (isLeapYear(year)) {
- if (day >= 1 && day < 29) this.day++;
- else {
- this.day = 1;
- this.month++;
- }
- } else if (day >= 1 && day < 28) this.day++;
- else {
- this.day = 1;
- this.month++;
- }
- } else {
- if (day >= 1 && day < daysInMonths[month - 1]) {
- this.day++;
- } else this.day = 1;
- this.month++;
- }
- }
- return this;
- }
- public MyDate nextYear() {
- if (year < 9999) {
- year++;
- }
- return this;
- }
- public static int getDayOfWeek(int d, int m, int y) {
- int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
- y -= (m < 3) ? 1 : 0;
- return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
- }
- public static void nameOfDay(int d)
- {
- switch (d){
- case 1:
- System.out.println(strDaysSk[0]);break;
- case 2:
- System.out.println(strDaysSk[1]);break;
- case 3:
- System.out.println(strDaysSk[2]);break;
- case 4:
- System.out.println(strDaysSk[3]);break;
- case 5:
- System.out.println(strDaysSk[4]);break;
- case 6:
- System.out.println(strDaysSk[5]);break;
- case 0:
- System.out.println(strDaysSk[6]);break;
- }
- }
- @Override
- public String toString() {
- return
- "day=" + day +
- ", month=" + month +
- ", year=" + year
- ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement