السلام عليكم ورحمة الله وبركاتة
لوس محتم اخواني اخواتي احتاج مساعدة ان شاء الله بسيطة في كود جافا
المطلوب ادخال التاريخ (السنة/اليوم/الشهر)
مثال :
ENTER A DATE
1809/12/02
ثم يقوم البرنامج بطلب ادخال تاريخ ثاني
مثال:
ENTER A DATE
1861/04/03
البرنامج يقزم بطرح التاريخ الاول من الثاني ثم يطبع كالتالي:
1809/12/02 - 1861/04/03
الناتج من طرح التاريخين:
52/20/01
ثم يقوم بحستب الايام والاشهر والسنوات الناتج التالي:
19013 days
624.6570841889117 months
52.054757015742645 years
الكود شغال تمام بس مايسوي العملية الحسابية:
ماعرفت اسوي الميثود اليتحسب الاشهر والسنوات
import java.text.DecimalFormat;
public class Date {
final String DELIMITER = "/"; //The delimiter symbol
int days = 1; //The internal encoded value
boolean mutated = true; //Used to indicate when decoding is necessary
int month, day, year; //The date value's decoded constituent parts
public Date (String S) {
int first, last;
S = S.trim(); //Remove all leading and trailing blanks
first = S.indexOf(DELIMITER); //Locate the first DELIMITER in the sequence of chars
last = S.lastIndexOf(DELIMITER); //Locate the last DELIMITER in the sequence of chars
month = 1; day = 1; year = 1;
try {
if(first > 0) {
month = Integer.parseInt(S.substring(0,first).trim()); //Recognize the month value
if(last > (first+1)) {
day = Integer.parseInt(S.substring((first+1),last).trim()); //Recognize the day value
}
year = Integer.parseInt(S.substring(last+1).trim()); //Recognize the year value
}
}
catch(NumberFormatException e) {
}
days = encode(month,day,year);
}
public Date(int Month, int Day, int Year) {
if((Month == 0) && (Day == 0) && (Year == 0)) {
days = 0;
}
else {
days = encode(Month,Day,Year);
}
}
public Date(int Days) {
days = Days;
mutated = true;
}
public Date(Date D) {
days = D.Days();
mutated = true;
}
//----------------------------------------------------------------------------------------
// Used internally to advance a month number
//----------------------------------------------------------------------------------------
private int nextMonth(int Month) {
int result = Month;
if(Month < 12) {
result = result + 1;
}
else {
result = 1;
}
return result;
}
//----------------------------------------------------------------------------------------
// Used internally to retard a month number
//----------------------------------------------------------------------------------------
private int prevMonth(int Month) {
int result = Month;
if(Month > 1) {
result = result - 1;
}
else {
result = 12;
}
return result;
}
//----------------------------------------------------------------------------------------
// Used internally to advance a year number, in coordination with the nextMonth method.
// Thus, this method is meant to be called immediately after calling the nextMonth method.
//----------------------------------------------------------------------------------------
private int nextYear(int Month, int Year) {
int result = Year;
if(Month == 1) {
result = result + 1;
}
return result;
}
//----------------------------------------------------------------------------------------
// Used internally to retard a year number, in coordination with the prevMonth method.
// Thus, this method is meant to be called immediately after calling the prevMonth method.
//----------------------------------------------------------------------------------------
private int prevYear(int Month, int Year) {
int result = Year;
if(Month == 12) {
result = result - 1;
}
return result;
}
//----------------------------------------------------------------------------------------
// Essential internal method used to encode a date value from its constituent parts.
//----------------------------------------------------------------------------------------
private int encode (int Month, int Day, int Year) {
int result = 0;
// NOTE THAT THIS IS WHERE YOUR IMPLEMENTATION GOES.
return result;
}
//----------------------------------------------------------------------------------------
// Essential internal method used to decode a previously encoded date value to its
// constituent parts. NOTE that this method modifies the instance variable of
// month, day and year.
//----------------------------------------------------------------------------------------
private void decode(int Days) {
boolean negated = false;
// NOTE THAT THIS IS WHERE YOUR IMPLEMENTATION GOES.
}
public static int MonthDays(int month, int year) {
int result;
if ((month==4) || (month==6) || (month==9) || (month==11)) {
result = 30;
}
else if(month==2) {
if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0) ) {
result = 29;
}
else {
result = 28;
}
}
else { // (month==1) || (month==3) || (month==5) || (month==7) || (month==8) || (month==10) || (month==12)
result = 31;
}
return result;
}
public int Year() {
if(mutated) {
mutated = false;
decode(this.days);
}
return year;
}
public int Month() {
if(mutated) {
mutated = false;
decode(this.days);
}
return month;
}
public int Day() {
if(mutated) {
mutated = false;
decode(this.days);
}
return day;
}
public int Days() {
return days;
}
public double Months() {
return days / (365.25 / 12);
}
public double Years() {
return days / 365.25;
}
public int compareTo (Date that) {
return (this.Days() - that.Days());
}
public boolean equals(Date that) {
return this.compareTo(that) == 0;
}
public void addDate(Date that) {
days = days + that.Days();
mutated = true;
}
public void subtractDate(Date that) {
days = days - that.Days();
mutated = true;
}
public String toString () {
DecimalFormat DF = new DecimalFormat("00");
String result = DF.format(this.Month()) + DELIMITER + DF.format(this.Day()) + DELIMITER + this.Year();
return result;
}
}------------
import java.util.Scanner;
public class DateTesterV1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String S;
Date Start = new Date("04/07/1776");
Date Prev = new Date(2,12,1809);
Date Current = new Date(Prev);
int count = 0;
do {
System.out.print("Enter a starting date:");
S = keyboard.nextLine().trim();
if (S.length() > 0) {
Start = new Date(S);
}
} while (false);
do {
System.out.print("Enter a date:");
S = keyboard.nextLine().trim();
if (S.length() > 0) {
Prev = Current;
Current = new Date(S);
Date temp = new Date(Current);
temp.subtractDate(Start);
System.out.print(Current + " - " + Start + " yields " + temp);
System.out.print(", " + temp.Days() + " days");
System.out.print(", " + temp.Months() + " months");
System.out.print(", " + temp.Years() + " years!");
System.out.println();
}
} while (S.length() > 0);
}
}