读书人

java 时间和毫秒相互转换的有关问题

发布时间: 2012-05-27 05:42:30 作者: rapoo

java 时间和毫秒,相互转换的问题谢谢指教

Java code
public int  IntegertimeTag(Date date){         int time=0;        Calendar c = Calendar.getInstance();        SimpleDateFormat   simpledate   =   new   SimpleDateFormat( "yyyy-MM-dd HH:mm:ss ");         String   Send_date   =   simpledate.format(date);                try {            time = (int) simpledate.parse(Send_date).getTime();        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }         return time;    }     public Date DatetimeTag(int date){         String time=null;        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");        Long now=new Long((long)date); //int  转 long           Calendar calendar = Calendar.getInstance();          calendar.setTimeInMillis(now);                  time= formatter.format(calendar.getTime());          Date date11=null;          try {            date11 = formatter.parse(time);        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                     return date11;    } 

这个是我自己整理的,代码,因为思路的问题,我代码运行后,得到了下面二个值,

IntegertimeTag(new Date());
String ddfs="Mon May 21 17:22:15 CST 2012";
int ddfi=1857445944;


但是我通过

DatetimeTag(1857445944);得到的日期却是

Mon May 21 17:22:15 CST 2012
Thu Jan 22 07:57:25 CST 1970

这二个时间怎么不相等,

还有个问题,

我这个 时间能不能搞成 2012-05-21 17:22:15 这种格式呀,

谢谢指教,在线等待中。

[解决办法]
你这里的int装会超范围 ,时间最好用Long来装, 在试试
[解决办法]
Calendar是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量,你传入的参数(1857445944)是否加上了Long(1970 年 1 月 1 日的 00:00:00.000转换为long型)。建议参考下API。
[解决办法]
看了很久才看明白lz想表达啥里。看下面一段代码:
time = (int) simpledate.parse(Send_date).getTime();
这里你要将一个Long类型的对象转换成int类型,而这时int类型肯定放不下。所以说问题就出现在这里了。你可以调试下试试,还有,如果你以后正常的操作Date的话,如果出现时间为1970年,那么肯定就还是类似的问题,那个Long类型的对象,就是1970年到所指的时间差
[解决办法]
偶是菜鸟,楼主看下这些代码能用不???
Java code
public class Test {    public static void main(String[] args){         Date date = null;         date = new Date();         System.out.println(getDateString(date));         System.out.println(getDateLong(date));         System.out.println(getDate(getDateLong(date)));         try {            System.out.println(getDate(getDateString(date)));        } catch (ParseException e) {            e.printStackTrace();        }    }        public static String getDateString(Date date){        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);    }    public static Date getDate(String dateStr) throws ParseException{        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);    }    public static Long getDateLong(Date date){        return date.getTime();    }    public static Date getDate(Long time){        Calendar calendar = Calendar.getInstance();        calendar.setTimeInMillis(time);        return calendar.getTime();    }}
[解决办法]
public static Date getDate(Long time){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return calendar.getTime();
}
可以这样写啊
public static Date getDate(Long time){
return new Date(time);
}

------解决方案--------------------


哎,看你的代码实在是。。。
不就是Date型和long型毫秒的相互转换么。
怎么写这么多代码。。。

Java code
    public static void main(String[] args) {                long time = IntegertimeTag(new Date());        System.out.println(time);        print(DatetimeTag(time));    }        private static long IntegertimeTag(Date date) {        return date.getTime();    }        private static Date DatetimeTag(long date) {        return new Date(date);    }        private static void print(Date date) {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println(sdf.format(date));    }
[解决办法]
探讨

哎,看你的代码实在是。。。
不就是Date型和long型毫秒的相互转换么。
怎么写这么多代码。。。

Java code

public static void main(String[] args) {

long time = IntegertimeTag(new Date());
System.out.println(tim……

读书人网 >J2EE开发

热点推荐