代码如下 package com.mycompany;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class change { /**
 * @param args
 * @throws ParseException
 */
long getnum(String time) throws ParseException { // 把当前时间转换成时间撮     
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date ca = bartDateFormat.parse(time);
return (long) ca.getTime();
}
       public static void main(String[] args) {
       change aa=new change();
       try{
        long d2 = aa.getnum("2008-065-07 10:37:13");
           System.out.print(d2);
       }catch(Exception ex){
        
       }
      
}}通过该方法可以获得一个LONG型数字 是与1970年0时0分0秒的差值
如果我想把这个值转换成日期  怎么写呢  求代码       初来宝地 分数不多只有20分

解决方案 »

  1.   

    答:Date d = new Date();
    d.setTime(long值);
      

  2.   

    直接new Date(Long timeLong)就OK了咩,多使用JDK文档吧.
      

  3.   

    日期是一个时间点的概念.
      而LZ的程序中long型数字,是一个相对的时间差值. 
        个人认为要转化的话,也只能够转化为"相对的时间",譬如多少"小时",多少"分钟".从逻辑上看,不像能够转换成"日期".
          
      

  4.   

    如果是 new Date(Long timeLong)会提示错误的   越界拉 
      

  5.   

    SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
    Date ca = bartDateFormat.parse(time); 
    不是已经得到Date了么???
      

  6.   

    Date time =new Date(ca.getTime());
      

  7.   


    package test;import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    public class change {/**
     * @param args
     * @throws ParseException
     */
    long getnum(String time) throws ParseException { // 把当前时间转换成时间撮     
            SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date ca = bartDateFormat.parse(time);
    return (long) ca.getTime();
    }
           public static void main(String[] args) {
           change aa=new change();
           try{
            long d2 = aa.getnum("2008-06-07 10:37:13");
               System.out.print(new Date(d2));
           }catch(Exception ex){
            
           }
          
    }} 注意,以前你的日期是    2008-065-07 10:37:13
      

  8.   

            Date d = new Date(); 
            d.setTime(d2);   //d2就是要转换的long型值
      

  9.   

      Date gettime(String  num) { // 把时间撮转换成日期时间
    Date d = new Date();  
                    d.setTime(1210127833000);   
    return d;
    }   我把方法写成这样 但是提示错误 越界
      

  10.   

    new Date(Long timeLong)越界问题不清楚,如果Long是你的方法返回的,应该不会出现这样问题才对咩.回7楼,能描述详细点么? Date不就是日期吗?
    详细说明一下你的目的和碰上的问题咩.
      

  11.   

    Date d = new Date();   
    d.setTime(1210127833000l);
    long类型的数据已l或者L结尾咩.
    其他问题还在看.
      

  12.   

    其实我的问题很简单就是求一个把时间撮转换成日期的方法
    我贴的代码是把一个日期转换成一个时间撮
    但是反过来把时间撮转换成日期就不行   楼上的几个朋友提到这个方法
    Date d = new Date();   
    d.setTime(1210127833000);    
    我试拉就是 报错     错误提示是这样的:The literal 1210127833000 of type int is out of range
      

  13.   

    呃,原来是这样咩.
    Date getDate(Long timeLong) throws Exception 
    {
      return new Date(timeLong);
    }
    注意long的结尾区别于int
    测试System.out.println(getDate(1210127833000l));