java 獲取時間的幾種方式,以及SimpleDateFormat裡引數的解釋 等
阿新 • • 發佈:2019-02-18
1、通過Date類來獲取當前時間
Date day=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(day));
2、通過System類中的currentTimeMillis方法來獲取當前時間
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(df.format(System.currentTimeMillis()));
3、Calendar c = Calendar.getInstance();//可以對每個時間域單獨修改
int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int date = c.get(Calendar.DATE); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second); 通過Calendar類來獲取當前時間
4、 通過Date類來獲取當前時間
Date date = new Date();
String year = String.format("%tY", date);
String month = String.format("%tB", date);
String day = String.format("%te", date);
System.out.println("今天是:"+year+"-"+month+"-"+day);