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());}}复制代码