boolean returnShortDate() {
return Integer.toString ( determineGregorianMonth ( ) )+”/”+ Long.toString (determineGregorianDay ( ) )+”/”+ Long.toString (determineGregorianYear ( ) %100 );
}
boolean returnFullDate() {
int months = Integer.toString ( FULLMONTH[ ]);
return Integer.toString ( FULLMONTH[ ])+”, ”+ Long.toString (determineGregorianDay ( ) )+”, ”+ Long.toString (determineGregorianYear ( ) );
}
boolean returnEuroDate() {
return Integer.toString ( determineGregorianMonth ( ) )+”-”+ Long.toString (determineGregorianDay ( ) )+”-”+ Long.toString (determineGregorianYear ( ) %100 );
}
boolean returnSortableDate() {
return Long.toString ( determineGregorianYear ( ) ) + Integer.toString (determineGregorianMonth ( ) ) + Long.toString (determineGregorianDay ( ) );
}
boolean returnShortFullDate() {
int months = Integer.toString ( SHORTMONTH [ ]);
return Integer.toString ( SHORTMONTH [ ])+”. ”+ Long.toString (determineGregorianDay ( ) )+”, ”+ Long.toString (determineGregorianYear ( ) %100 );
}
簡短一下,其實就是這幾句有問題,該如何改呢??
就是給出一個具體的日期,比如公元2010年12月31日.這個日期與公元1年1月1日相隔總數多少天.多少年,多少月,跟天數.然後將2010年12月31日的格式寫成5種表達形式:12/31/10(年份2010變10),December 31 ,2010,12-31-10, 2010 12 31跟Dec 31,10
API不對?那該怎樣改呢?
我把你的第一個方法已經更改了其他的方法已經屏蔽掉了!
你自己把下麵的代碼copy下 來運行 看看第一個方法是不是你想要的結果!
class Date {private final int DAYSINAYEAR[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31 };private final int DAYSINALEAPYEAR[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30,31, 30, 31 };private final String FULLMONTH[] = { " ", "January", "February", "March","April", "May", "June", "July", "August", "September", "October","November", "December" };private final String SHORTMONTH[] = { " ", "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };private long days;/** * determineGregorianMonthDay * @return long */public long determineGregorianMonthDay() {long year = 1;int months = 1;long days = this.days;for (; days > (isALeapYear(year) ? 366 : 365); year++)days -= (isALeapYear(year) ? 366 : 365);for (; days > (isALeapYear(year) ? DAYSINALEAPYEAR[months - 1]: DAYSINAYEAR[months - 1]); months++)days -= (isALeapYear(year) ? DAYSINALEAPYEAR[months - 1]: DAYSINAYEAR[months - 1]);return days;}/** * determineGregorianMonthYear * @return long */public long determineGregorianMonthYear() {long year = 1;int months = 1;long days = this.days;for (; days > (isALeapYear(year) ? 366 : 365); year++)days -= (isALeapYear(year) ? 366 : 365);year++;for (; days > (isALeapYear(year) ? DAYSINALEAPYEAR[months - 1]: DAYSINAYEAR[months - 1]); months++)days -= (isALeapYear(year) ? DAYSINALEAPYEAR[months - 1]: DAYSINAYEAR[months - 1]);return months;}/** * determineGregorianYear * @return long */public long determineGregorianYear() {long year = 1;long days = this.days;for (; days > (isALeapYear(year) ? 366 : 365); year++)days -= (isALeapYear(year) ? 366 : 365);return year;}/** * determineJulianDate * @param year---long type * @param month---long type * @param day---long type */public void determineJulianDate(long year, long month, long day) {int yearCounter = 0;for (yearCounter = 1; yearCounter < year; yearCounter++)days += (isALeapYear(yearCounter)) ? 366 : 365;for (int monthCounter = 1; monthCounter < month; monthCounter++)days += (isALeapYear(yearCounter)) ? DAYSINALEAPYEAR[monthCounter - 1]: DAYSINAYEAR[monthCounter - 1];this.days += day;}/** * getDays * @return long */public long getDays() {return days;}/** * isALeapYear * @param year---long type * @return boolean */private boolean isALeapYear(long year) {return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) ? true: false;}// boolean returnShortDate() {////return Integer.toString ( determineGregorianMonth ( ) )+"/"+ Long.toString (determineGregorianDay ( ) )+"/"+ Long.toString (determineGregorianYear ( ) %100 );// }/** * returnShortDate * @return String */public String returnShortDate() {StringBuffer sb = new StringBuffer();sb.append(String.valueOf(determineGregorianMonthYear()));sb.append("/");sb.append(String.valueOf(determineGregorianMonthDay()));sb.append("/");sb.append(String.valueOf(determineGregorianYear() % 100));return sb.toString();}//boolean returnFullDate() {//int months = Integer.toString ( FULLMONTH[ ]);//return Integer.toString ( FULLMONTH[ ])+", "+ Long.toString (determineGregorianDay ( ) )+", "+ Long.toString (determineGregorianYear ( ) );// }////boolean returnEuroDate() {////return Integer.toString(determineGregorianMonth()) + "-"//+ Long.toString(determineGregorianDay()) + "-"//+ Long.toString(determineGregorianYear() % 100);//}////boolean returnSortableDate() {//return Long.toString(determineGregorianYear())//+ Integer.toString(determineGregorianMonth())//+ Long.toString(determineGregorianDay());//}////boolean returnShortFullDate() {//int months = Integer.toString ( SHORTMONTH [ ]);//return Integer.toString ( SHORTMONTH [ ])+". "+ Long.toString (determineGregorianDay ( ) )+", "+ Long.toString (determineGregorianYear ( ) %100 );// }}public class Driver {public static void main(String[] args) {Date aTestDate = new Date();aTestDate.determineJulianDate(10, 12, 31);System.out.println("Days is " + aTestDate.getDays());System.out.println("Days is " + aTestDate.determineGregorianMonthDay());System.out.println("months is "+ aTestDate.determineGregorianMonthYear());System.out.println("year is " + aTestDate.determineGregorianYear());System.out.println("shortDate is " + aTestDate.returnShortDate());//System.out.println("fullDate is "+aTestDate. returnFullDate());//System.out.println("euroDate is "+aTestDate. returnEuroDate());//System.out.println("sortableDate is "+aTestDate. returnSortableDate());//System.out.println("shortFullDate is "+aTestDate. returnShortFullDate());}}複製代碼