时间转换
1秒=1000毫秒(ms)
1秒=1,000,000 微秒(μs) 1秒=1,000,000,000 纳秒(ns) 1秒=1,000,000,000,000 皮秒(ps)1纳秒=1000 皮秒 1纳秒 =0.001 微秒1纳秒=0.000001 毫秒1纳秒=0.00000 0001秒1分钟=60秒1小时=60分钟=3600秒类 SimpleDateFormat
[object Object] [object Object] [object Object]
SimpleDateFormat 可以选择任何用户定义的日期-时间格式的模式
import java.util.Date;import java.text.SimpleDateFormat;class DateTime { private SimpleDateFormat sdf = null; public String getDate() { this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); return this.sdf.format(new Date()); } public String getDateComplete() { this.sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒 SSS 毫秒"); return this.sdf.format(new Date()); } public String getTimeStamp() { this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); return this.sdf.format(new Date()); }}public class demo { public static void main(String[] args) { DateTime dt = new DateTime(); System.out.println("系统日期:" + dt.getDate()); System.out.println("中文日期:" + dt.getDateComplete()); System.out.println("时间戳:" + dt.getTimeStamp()); }}
获取系统时间计算比较毫秒数
public static void main(String[] args) { Date date=new Date(); System.out.println(date); // String dateFormet=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);// System.out.println(dateFormet); long timemillis=System.currentTimeMillis(); System.out.println(timemillis); long time=new Date().getTime(); System.out.println(time); String linkTime="2015-06-22 21:18:00";// SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// Date formatParse=null;// try {// formatParse=dateFormat.parse(linkTime);// } catch (ParseException e) {// e.printStackTrace();// }// long formatLong=formatParse.getTime(); long formatLong=0l; try { formatLong=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(linkTime).getTime(); } catch (ParseException e) { e.printStackTrace(); } System.out.println(formatLong); System.out.println(time - formatLong); System.out.println("分:"+(time - formatLong)/(1000*60)); System.out.println("秒:"+(time - formatLong)/1000); if(time-formatLong > 1000*60){ System.out.println("系统时间大于2分 true"); }else{ System.out.println("系统时间不大于2分 false"); } }
System.currentTimeMillis()返回的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMillis());因为Date类还有构造Date(long date),用来计算long秒与1970年1月1日之间的毫秒差。。