java 中的时间怎么相加,把int转换为date型后时间不对啊 
怎样才能得到正确的时间啊

解决方案 »

  1.   

    你具体说说 怎么 个“时间相加”,你怎么把int装换为date类型了?无头无脑的问题,具体说说
      

  2.   

    package com.zhao.test.O1;import java.text.SimpleDateFormat;
    import java.util.Date;public class Test {
     public static void main(String[] args) {
      Date date = new Date();
      // format对象是用来以指定的时间格式格式化时间的
      SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 这里的格式可以自己设置
      // format()方法是用来格式化时间的方法
      String times = from.format(date);
      //原始时间
      System.out.println("原始时间:"+times);
      date.setMinutes(date.getMinutes()+50);//给当前时间加50分钟后的时间
      times=from.format(date);
      System.out.println("相加之后的时间:"+times);
     }
    }详细的相加楼主就请看API吧
      

  3.   

    /**
     * 描述:以long的格式增加num个月
     * @param currentTm
     * @param num
     * @return
     */
    public static long addMonth(long currentTm, int num){
    long newTm = 0; Calendar cale = Calendar.getInstance();
    Date date = new Date();
    date.setTime(currentTm);
    cale.setTime(date);
    cale.add(Calendar.MONTH, num);
    newTm = cale.getTime().getTime(); return newTm;
    } /**
     * 描述:以long的格式增加num天
     * @param currentTm
     * @param num
     * @return
     */
    public static long addDay(long currentTm, int num){
    long newTm = 0; Calendar cale = Calendar.getInstance();
    Date date = new Date();
    date.setTime(currentTm);
    cale.setTime(date);
    cale.add(Calendar.DATE, num);
    newTm = cale.getTime().getTime(); return newTm;
    }
      

  4.   

                    Date nedate = new Date();
    DateFormat dateformat = DateFormat.getDateInstance();
    nedate.setTime(1295539200);
    Date oldDate = dateformat.parse("1970-01-01 00:00:00");
    System.out.println(dateformat.format(oldDate));
    System.out.println(nedate.after(oldDate));
    System.out.println(dateformat.format(nedate)); System.out.println(nedate.getTime());
    System.out.println(oldDate.getTime());
    运行的结果:
    1970-1-1
    true
    1970-1-16
    1295539200
    -28800000我的数据库里面的数据是1295539200
    用select DATE_ADD("1970-01-01 00:00:00",INTERVAL 1295539200 SECOND); 
     转换的时间是2011-01-20 16:00:00
    但是在java里面的时间是1970-1-16
    这使怎么回事啊 
      

  5.   

    如果是date,可以看看date累的方法,如果要看的是日历,可以 用gregraincanlendar类,查下,有加相关的日期的方法
      

  6.   

    你这个Date  是java.util.Date  还是 java.sql.Date
      

  7.   

    long a = 1295539200;
    System.out.println(new Date(a));
    结果是
    Fri Jan 16 07:52:19 CST 1970
      

  8.   

    你SQL里面用的是秒为单位,而JAVA里面则是毫秒为单位,差了1000所以出错
    import java.util.Date;
    import java.util.Calendar;public class CalendarDemo2 {
        public static void main(String[] args) {
            Date date=new Date();
            Calendar c=Calendar.getInstance();
            System.out.println(""+c.getTime());        int seconds=1295539200;//这是你数据库提出的数据
            long millions=new Long(seconds).longValue()*1000;
            System.out.println(""+millions);
            c.setTimeInMillis(millions);
            
            System.out.println(""+c.getTime());
        }
    }
      

  9.   

    按你的程序改成这样
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    public class CalendarDemo {
        public static void main(String[] args) throws Exception{
            Date nedate = new Date();
            DateFormat dateformat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            int seconds=1295539200;
            long millions=new Long(seconds)*1000;
            nedate.setTime(millions);
            Date d=new Date(millions);
    //        System.out.println("time:"+d.toString());
    //        System.out.println(1295539200.0/(60*60*24*365));
            Date oldDate = dateformat.parse("1970-01-01 00:00:00");
            System.out.println(dateformat.format(oldDate));
            System.out.println(nedate.after(oldDate));
            System.out.println(dateformat.format(nedate));        System.out.println(nedate.getTime());
            System.out.println(oldDate.getTime());
        }
    }
      

  10.   

    //日期date上加count小时
    public static Date addMinute(Date date, int count) {
    return new Date(date.getTime() + 60000L * count);
    }public static void main(String[] args) {
            //系统当前时间加上5分钟
    System.out.println(new Date(new Date().getTime()+60000L*5).toLocaleString());
            //系统当前时间加上10分钟
    System.out.println(addMinute(new Date(),10).toLocaleString());
    }运行一下,看是不是你想要的。