📄 datemain.java
字号:
import java.util.Scanner ;
class Date {
private long year = 0 ;
private long month = 0 ;
private long days = 0 ;
private long julianDate ;
Date() {
year = askForInt("Enter year: ");
month = askForInt("Enter month: ") ;
days = askForInt("Enter day: ") ;
julianDate = returnJulianDate(year, month, days) ;
}
public static int askForInt(String message) {
Scanner keyboard ;
int aValue ;
keyboard = new Scanner(System.in) ;
System.out.print(message) ;
aValue = keyboard.nextInt() ;
return aValue ;
}
public long getYear() {
return year ;
}
public long getMonth() {
return month ;
}
public long getDays() {
return days ;
}
public void setYear(long theYear) {
if (theYear >= 1900)
year = theYear ;
}
public void setMonth(long theMonth) {
if ((theMonth > 0) && (theMonth < 13))
month = theMonth ;
}
public void setDays(long theDays) {
if ((theDays > 0) && (theDays <= 31))
days = theDays ;
}
public boolean isLeapYear(long year) {
boolean returnValue = false ;
if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))
returnValue = true ;
return returnValue ;
}
public int returnDaysInYear(long year) {
int returnValue = 365 ;
if (isLeapYear(year))
returnValue = 366;
return returnValue ;
}
public int returnDaysInMonth(long year, long month) {
int returnValue = 31 ;
if (month == 2)
if (isLeapYear(year))
returnValue = 29 ;
else
returnValue = 28 ;
if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
returnValue = 30 ;
return returnValue ;
}
public long returnJulianDate() {
return julianDate ;
}
public long returnJulianDate(long year, long month, long day) {
long aMonth = 0 ;
long aYear = 0 ;
long elapsedDaysInMonth = 0 ;
long elapsedDaysInYear = 0 ;
long julianDate = 0 ;
aYear = 1900 ;
while (aYear < (year)) {
elapsedDaysInYear = elapsedDaysInYear + returnDaysInYear(aYear) ;
aYear++ ;
}
aMonth = 1 ;
while (aMonth < (month)) {
elapsedDaysInMonth = elapsedDaysInMonth + returnDaysInMonth(year, aMonth) ;
aMonth++ ;
}
julianDate = elapsedDaysInYear + elapsedDaysInMonth + day ;
return julianDate ;
}
public long returnYears() {
long aYear ;
long jd = julianDate ;
for (aYear = 1900 ; jd >= returnDaysInYear(aYear) ; aYear++)
jd = jd - returnDaysInYear(aYear) ;
return aYear ;
}
public long returnMonths() {
long aMonth ;
long aYear = 1900 ;
long jd = julianDate ;
for (aMonth = 1 ; jd >= returnDaysInMonth(aYear, aMonth) ; aMonth++) {
jd = jd - returnDaysInMonth(aYear, aMonth) ;
if (aMonth == 12) {
aYear++ ;
aMonth = 0 ;
}
}
return aMonth ;
}
public long returnDays() {
long aYear ;
long aMonth ;
long day ;
long jd = julianDate ;
for (aYear = 1900 ; jd >= returnDaysInYear(aYear) ; aYear++)
jd = jd - returnDaysInYear(aYear) ;
for (aMonth = 1 ; jd >= returnDaysInMonth(aYear, aMonth) ; aMonth++)
jd = jd - returnDaysInMonth(aYear, aMonth) ;
day = jd ;
return day ;
}
}
public class DateMain {
public static void main (String[] args) {
Date aDate = null ;
aDate = new Date() ;
System.out.println("Number of days since Jan 1,1900: " + aDate.returnJulianDate()) ;
System.out.println("Year of the julian date: " + aDate.returnYears()) ;
System.out.println("Month of the julian date: " + aDate.returnMonths()) ;
System.out.println("Day of the julian date: " + aDate.returnDays()) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -