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 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 这种格式呀,谢谢指教,在线等待中。
time = (int) simpledate.parse(Send_date).getTime();
这里你要将一个Long类型的对象转换成int类型,而这时int类型肯定放不下。所以说问题就出现在这里了。你可以调试下试试,还有,如果你以后正常的操作Date的话,如果出现时间为1970年,那么肯定就还是类似的问题,那个Long类型的对象,就是1970年到所指的时间差
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();
}
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return calendar.getTime();
}
可以这样写啊
public static Date getDate(Long time){
return new Date(time);
}
不就是Date型和long型毫秒的相互转换么。
怎么写这么多代码
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));
}
不过要放到实际应用当中去,我就只想到这步了,我是想把注册 的时间换算成数字,这样对比起来,也方便,
不过还是学到不少!!!!谢谢位,还是最早说的,是因为long和int超范围的问题!最后还是选择了,bigint,当达不到bigint的时候,好像还是会以int存储,呵呵!先前是想直接用int解决,不过没办法,只能用bigint了,我的库选的mysql