统计信息

  • 访问数:14546
  • 博客数:143
  • 建立时间:2008-01-05
  • 更新时间:2008-10-15
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P5Q21

2008-04-09 20:39:00

天气: 晴朗 心情: 高兴

/*fileName:P5Q21
 * Name:Wang Chaoran
 * Description:
21 (Displaying current date and time)
Listing 2.8, ShowCurrentTime.java, displays the current time. Improve this example to display the current date and time. The calendar example in
§5.11, "Method Abstraction and Stepwise Refinement," should give you some ideas on how to find year, month, and day.
*/

public class P5Q21{
  public static void main(String[] args){
 // Obtain the total milliseconds since the midnight, Jan 1, 1970
  long millis = System.currentTimeMillis();
  System.out.println(millis);
  long days = millis /1000 /3600 /24;
  millis -= days*1000*3600*24;
  System.out.println(millis);
  String output = "";
 
  long year = getYear(days);
  days -= getYearRemainDays(getYear(days));
  long month = getMonth(days,year);
  String monthName = getMonthName((int)(month));
  days -= getMonthRemainDays(days,month,year);
 
  output += year+" "+monthName+" "+(days+1)+" "+convertMillis(millis/1000);
 System.out.println(output);
  }
 
  public static boolean isLeapYear(long year){
  return (year % 400 ==0) || ((year % 4 == 0) && (year % 100 != 0));
  }
  static long getYear(long days){
    long i=1970;
    while(days >= 0){
    if(isLeapYear(i))
    days -= 366;
    else
    days -= 365;
    i++;
    }
    return i-1;  
  }
  static long getYearRemainDays(long year){
    long sum =0;
    for(long i=1970;i<=year-1;i++){
      if(isLeapYear(i))
        sum += 366;
        else
          sum += 365;
    }
    return sum;
  }
  static long getMonth(long days,long year){
    long i = 1;
    while(days>0){
      days -= getNumberOfDaysInMonth(year,i);
      i++;
    }
    return i-1;
  }
  static long getMonthRemainDays(long days,long month,long year){
    long sum = 0;
    for(int i=1;i<=month-1;i++){
    sum += getNumberOfDaysInMonth(year,i);
    }
    return sum;
  }
  static long getNumberOfDaysInMonth(long year, long month) {
      if (month == 1 || month == 3 || month == 5 || month == 7 ||
        month == 8 || month == 10 || month == 12)
        return 31;

      if (month == 4 || month == 6 || month == 9 || month == 11)
        return 30;

      if (month == 2) return isLeapYear(year) ? 29 : 28;

      return 0; // If month is incorrect
    }
  static String getMonthName(int month) {
       String monthName = null;
       switch (month) {
         case 1: monthName = "January"; break;
         case 2: monthName = "February"; break;
         case 3: monthName = "March"; break;
         case 4: monthName = "April"; break;
         case 5: monthName = "May"; break;
         case 6: monthName = "June"; break;
         case 7: monthName = "July"; break;
         case 8: monthName = "August"; break;
         case 9: monthName = "September"; break;
         case 10: monthName = "October"; break;
         case 11: monthName = "November"; break;
         case 12: monthName = "December";
       }
       return monthName;
  }
    static String convertMillis(long milli){
    String result = milli / 3600 +":"+ (milli - milli /3600*3600)/60 +":"+(milli - milli /3600*3600-(milli - milli /3600*3600)/60*60);
    return result;
  }
 
}


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd