一.日期转换为时间戳
public long getTimestamp() throws ParseException{
Date date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2009/12/11 00:00:00");
Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00");
long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime()- date2.getTime() : date2.getTime() - date1.getTime();
//long rand = (int)(Math.random()*1000);
二.时间戳转换为date 型
public void getDate(String unixDate) {
SimpleDateFormat fm1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
SimpleDateFormat fm2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long unixLong = 0;
String date = "";
try {
unixLong = Long.parseLong(unixDate) * 1000;
} catch(Exception ex) {
System.out.println("String转换Long错误,请确认数据可以转换!");
}
try {
date = fm1.format(unixLong);
date = fm2.format(new Date(date));
} catch(Exception ex) {
System.out.println("String转换Date错误,请确认数据可以转换!");
}
System.out.println(date);
}
public long getTimestamp() throws ParseException{
Date date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2009/12/11 00:00:00");
Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00");
long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime()- date2.getTime() : date2.getTime() - date1.getTime();
//long rand = (int)(Math.random()*1000);
//return rand;
return l;
}二.时间戳转换为date 型
public void getDate(String unixDate) {
SimpleDateFormat fm1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
SimpleDateFormat fm2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long unixLong = 0;
String date = "";
try {
unixLong = Long.parseLong(unixDate) * 1000;
} catch(Exception ex) {
System.out.println("String转换Long错误,请确认数据可以转换!");
}
try {
date = fm1.format(unixLong);
date = fm2.format(new Date(date));
} catch(Exception ex) {
System.out.println("String转换Date错误,请确认数据可以转换!");
}
System.out.println(date);
}
系统获取时间戳 (单位ms):
System.currentTimeMillis();