java1.8中如何使用精確到毫秒的時間
阿新 • • 發佈:2019-01-08
date() urn this 操作 聲明 進行 gre 工具包 use 主要涉及三個類:
- SimpleDateFormat,用於顯示時間的格式。
- Date,獲取精確到毫秒的時間。
- Calendar,獲取日歷格式的時間。
示例代碼如下:
package baseAPI; import java.util.* ; // 導入需要的工具包 import java.text.* ; // 導入SimpleDateFormat所在的包 class mmm{ // 以後直接通過此類就可以取得日期時間 private SimpleDateFormat sdf = null ; // 聲明SimpleDateFormat對象 public String getDate(){ // 得到的是一個日期:格式為:yyyy-MM-dd HH:mm:ss.SSS this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ; return this.sdf.format(new Date()) ;// 將當前日期進行格式化操作 } public String getDateComplete(){ // 得到的是一個日期:格式為:yyyy年MM月dd日 HH時mm分ss秒SSS毫秒 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 useDate{ public static void main(String args[]){ mmm dt = new mmm() ; System.out.println("系統日期:"+dt.getDate()) ; System.out.println("中文日期:"+dt.getDateComplete()) ; System.out.println("時間戳:"+dt.getTimeStamp()) ; Calendar calendar = new GregorianCalendar(); StringBuffer strb= new StringBuffer(); strb.append(calendar.get(Calendar.YEAR)); strb.append("-"+(calendar.get(Calendar.MONTH)+1)); strb.append("-"+calendar.get(Calendar.DAY_OF_MONTH)); strb.append(" "+calendar.get(Calendar.HOUR_OF_DAY)); strb.append(":" +calendar.get(Calendar.MINUTE)); strb.append(":"+calendar.get(Calendar.SECOND)); strb.append(":"+calendar.get(Calendar.MILLISECOND)); System.out.println(strb); } };
java1.8中如何使用精確到毫秒的時間