数据库的时间c_time列为datetime型String c_time=rs1.getString("c_time"); //数据库时间 得出2012-04-09 10:18:41 想转换整型(int)或长整型(long)
为201204091018String now_time="20"+String.valueOf(date.getYear()).substring(1)+"-"+String.valueOf(date.getMonth()+1)+"-"+date.getDate()+"  "+date.getHours()+":"+date.getMinutes();  //当前时间  转换整型(int)为201204091120本来想转换为整型(int)长整型(long),但是不行希望得到int time=now_time-c_time;
if(time<10){out.println("您注册到目前仍小于10分钟");}
else
{out.println("成功");}

解决方案 »

  1.   

    得到的时间类型 和当前时间的类型都变为201204091120这种啊  不要变成yyyy-mm-dd hh:mi:ss 这种型式
      

  2.   

    然后把string类型的转换成int型  就ok了
      

  3.   

    int c_time=Integer.parseInt(rs1.getString("c_time"));直接报错 
      

  4.   

    你取出来的是时间类型 你要先转换成String类型的 
      

  5.   

    rs1.getString("c_time");
    这样取出来的是String的!
      

  6.   

    //时间类型转换成String类型
    SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
    String c_time= tempDate.format(rs1.getString("c_time"));//string类型转换成int型
    Integer intObj = new Integer(c_time)
    int inte = intObj.intValue();
      

  7.   

    你的意思是你的时间存在数据库里的就是String类型的啊?
      

  8.   

    建议可以直接写个SQL 取得你数据库时间和当前时间的差值  
      

  9.   


     SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间格式
           Date nowTime=new Date();//获取当前时间
                 String   tmf= matter1.format(nowTime);
                                 Date now=matter1.parse(tmf);
                                 Date end=matter1.parse(atime);//atime是数据库中事前存的时间
                                long cha=(now.getTime()-end.getTime())/ (1000 * 60 * 60 * 24);//计算时间差
                             
                              System.out.println("一号时间差事:"+cha);
      

  10.   

    select (to_date(c_time,'yyyymmddhh24miss')-sysdate)24*60 as c_time from table 
    得到的c_time就是他的差值   差值是分钟
      

  11.   

    这个是算出时间差小时的
     long str=(date2.getTime()-date.getTime())/1000/60/60;
    后面的三个数字1000、60、60是控制你要算出的时间差
      

  12.   

    数据库的时间c_time列为datetime型但是String c_time=rs1.getString("c_time"); 可以取到时间,只是后面多了个.0
    Date c_time=rs1.getDate("c_time");  也可以取到时间 但是只能取到 年,月,日SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
    String c_time= tempDate.format(rs1.getString("c_time"));
    第二行就报错了
      

  13.   


    String c_time= tempDate.format(rs1.getDate("c_time"));
    试试看要是不行你就用那种直接写SQL 的吧
      

  14.   


    这样没报错 System.out.println(c_time); 的结果为20120409000000 取不到小时,分,秒
      

  15.   

    SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
    Date tempc_time= tempDate.parse(rs1.getString("c_time"));
    String c_time=tempDate.format(tempc_time);
    试下这个
      

  16.   

    那啥  你取那个数据库时间的时候 to_char一下  把它变成String类型的   to_char(c_time,'yyyymmddhh24miss')  
      

  17.   

    可以将时间存的时候存成long型,算时间差就简单多了,这是存放时间值的方法
    public static long getLongTime()
        {
            try
            {
                long l = 0L;
                Calendar calendar = Calendar.getInstance();
                String s = String.valueOf(calendar.get(1));
                String s1 = String.valueOf(calendar.get(2) + 1);
                String s2 = String.valueOf(calendar.get(5));
                String s3 = String.valueOf(calendar.get(10));
                String s4 = String.valueOf(calendar.get(9));
                String s5 = String.valueOf(calendar.get(12));
                String s6 = String.valueOf(calendar.get(13));
                if(s4.equals("1"))
                    s3 = String.valueOf(Long.parseLong(s3) + 12L);
                if(s1.length() < 2)
                    s1 = "0" + s1;
                if(s2.length() < 2)
                    s2 = "0" + s2;
                if(s3.length() < 2)
                    s3 = "0" + s3;
                if(s5.length() < 2)
                    s5 = "0" + s5;
                if(s6.length() < 2)
                    s6 = "0" + s6;
                l = Long.parseLong(s + s1 + s2 + s3 + s5 + s6);
                return l;
            }
            catch(Exception exception)
            {
                return 0L;
            }
        }
      

  18.   

    问题解决了,最后还是用了10楼的方法, 稍做了下改动,时差就算出来了谢谢大家的热心帮助,特别是fangkaifang同志,谢谢哈!
      

  19.   

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       java.util.Date now = df.parse("2008-03-26 13:31:40");
       java.util.Date date=df.parse("2008-01-02 11:30:24");
       long l=now.getTime()-date.getTime();
       long day=l/(24*60*60*1000);
       long hour=(l/(60*60*1000)-day*24);
       long min=((l/(60*1000))-day*24*60-hour*60);
       long s=(l/1000-day*24*60*60-hour*60*60-min*60);
       System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");