我用的开发工具在存放日期时会将日期转换为整型数存储  例如2008-1-10 转换为2454476,2008-1-14转换为2454480,我现在使用iReport做web报表时,报表中的日期显示的就是转换后的整型数,我想问一下,这个日期是按什么样的方式转化为整型数的,java中有没有方法可以将这个整型数再转换为“2008-1-10 ”这种日期格式?
(注:我用crystal report做同一张表的报表时没有遇到这种问题,它可以自动转化回来,说明这种转换方法应该是常用的。)

解决方案 »

  1.   

    猜不出,建议多给出一些例子,可以根据mapping关系写个算法
      

  2.   

    Date(long   date)   本身就可以把long类型的数字转换成date类型的。
      

  3.   

    按有几个群,你不妨加进去,可以和大家一起讨论啊.........46986340,28039577,4804620                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    在那里看看有无能回答你的,谢谢,LZ,甭忘了给俺分哦,谢谢LZ
      

  4.   

    我猜了个算法,你给的5个例子,有4个对了,1个差1位数,不知道是你写错,还是算法错误,你看下
    你的数据是:
    2008-1-10转换为2454476
    2008-1-14转换为2454480
    2002-1-12转换为2452297     
    1992-10-17转换为2448913 
    2007-6-16转换为2454268
    import java.util.*;
    class DateDemo{
    public static void main(String[] args){
    String[] str={"2008-1-10","2008-1-14","2002-1-12","1992-10-17","2007-6-16"};
    for(int i=0;i<str.length;i++){
    print(str[i]);
    }
    }

    public static void print(String str){
    Date d=new Date(str.replaceAll("-","/"));
    long x=test()+(d.getTime()/(1000*60*60*24));
    System.out.println(str+"转换为"+x);
    }

    public static long test(){
    Date d=new Date("2008/1/14");
    long m=(d.getTime())/(1000*60*60*24);
    long start=2454480-m;
    return start;
    }
    }
    执行结果:
    2008-1-10转换为2454476
    2008-1-14转换为2454480
    2002-1-12转换为2452287     
    1992-10-17转换为2448913 
    2007-6-16转换为2454268
      

  5.   

    personally i suggest using java api,class DateFormat.
      

  6.   

    取某一日期(如2008-1-10)的对应整数值作为基准数 其它数与它的差值即为天数差import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;public class Test {
    public static void main(String[] arg) {
    int base = 2454476;// 2008-01-10
    int[] number = { 2454480, 2454476, 2454268, 2452297, 2448913 };
    Test test = new Test(); for (int num : number) {
    System.out.println(test.getDate(num - base));
    }
    } public String getDate(int diff) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = null, date2 = null; try {
    date1 = sdf.parse("2008-01-10");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date1);
    calendar.add(Calendar.DAY_OF_YEAR, diff);
    date2 = calendar.getTime();
    }
    catch (ParseException e) {
    e.printStackTrace();
    }
    return sdf.format(date2);
    }
    }/*
    2008-01-14
    2008-01-10
    2007-06-16
    2002-01-22
    1992-10-17
    */
    2002-01-22与楼主给的结果也有差异
      

  7.   

    sorry,是我给的一条数据错了,“2002-1-12”这条是手打的,确实应为2002-1-22,多谢两位了!