比如1998年7月20号对应的小数形式1998.55年

解决方案 »

  1.   


    Calendar cal = Calendar.getInstance();
    cal.set(1998, 6, 20);
    BigDecimal days = new BigDecimal(cal.get(Calendar.DAY_OF_YEAR));
    BigDecimal p = days.divide(new BigDecimal(365), 2, RoundingMode.HALF_UP);
    System.out.println((new BigDecimal(1998).add(p)).toString());
      

  2.   

    cal.set(1998, 6, 20); 这个就是 7月20日
    楼主可以去翻API。
      

  3.   

    public boolean leapYear(int year) {
    if (((year % 100 == 0) && (year % 400 == 0))|| ((year % 100 != 0) && (year % 4 == 0)))
    return true;//(366)
    else 
    return false;//(365天)
    }
    加个闰年的 判断试试  除数 有变化的
      

  4.   

    对头,这是Calendar的一个小陷阱,Calendar的月份是从0开始的
      

  5.   


    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, 1998);
    c.set(Calendar.MONTH, 6);//Calendar 从一月到十二月对应0到11
    c.set(Calendar.DATE, 20);
    double i = c.get(Calendar.DAY_OF_YEAR);
    c.set(Calendar.MONTH, 11);
    c.set(Calendar.DATE, 31);
    System.out.println(c.get(Calendar.DAY_OF_YEAR));
    double year = c.get(Calendar.YEAR) + i / c.get(Calendar.DAY_OF_YEAR);
    System.out.printf("%.2f", year);
      

  6.   

    呵呵,终于会贴代码了啊!我马上就贴上我的代码,希望LZ有用!public class TurnYearDecimal {
    public static double trunDecimal(int year, int month, int day) {
    int number = 0;
    double result = 0.0; //定义每个月的天数
    int[][] months = {
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    }; //根据闰平年的不同,分别计算
    if (isLeap(year)) {
    for (int i = 1; i < month; i++) {
    number += months[0][i];
    }
    number = number + day;
    result = 1.0 * number / 366;
    } else {
    for (int i = 1; i < month; i++) {
    number += months[0][i];
    }
    number = number + day;
    result = 1.0 * number / 365;
    } return result + year;
    }

    static boolean isLeap(int year) {
    return ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
    } public static void main(String[] args) {
    System.out.printf("%.2f\n", trunDecimal(1998, 7, 20));
    }
    }