代码如下 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分
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分
d.setTime(long值);
而LZ的程序中long型数字,是一个相对的时间差值.
个人认为要转化的话,也只能够转化为"相对的时间",譬如多少"小时",多少"分钟".从逻辑上看,不像能够转换成"日期".
Date ca = bartDateFormat.parse(time);
不是已经得到Date了么???
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
d.setTime(d2); //d2就是要转换的long型值
Date d = new Date();
d.setTime(1210127833000);
return d;
} 我把方法写成这样 但是提示错误 越界
详细说明一下你的目的和碰上的问题咩.
d.setTime(1210127833000l);
long类型的数据已l或者L结尾咩.
其他问题还在看.
我贴的代码是把一个日期转换成一个时间撮
但是反过来把时间撮转换成日期就不行 楼上的几个朋友提到这个方法
Date d = new Date();
d.setTime(1210127833000);
我试拉就是 报错 错误提示是这样的:The literal 1210127833000 of type int is out of range
Date getDate(Long timeLong) throws Exception
{
return new Date(timeLong);
}
注意long的结尾区别于int
测试System.out.println(getDate(1210127833000l));