1. Date类

1 Date day=new Date();
2 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
3 System.out.println(df.format(day));
4 // 或者 
5 Date date = new Date();    
6 String year = String.format("%tY", date);
7 String month = String.format("%tB", date);
8 String day = String.format("%te", date);
9 System.out.println(year+"-"+month+"-"+day);

2. System类

1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2 System.out.println(df.format(System.currentTimeMillis()));

3. Calendar类

1 Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
2 int year = c.get(Calendar.YEAR);
3 int month = c.get(Calendar.MONTH);
4 int date = c.get(Calendar.DATE);
5 int hour = c.get(Calendar.HOUR_OF_DAY);
6 int minute = c.get(Calendar.MINUTE);
7 int second = c.get(Calendar.SECOND);
8 System.out.println(year + "-" + month + "-" + date + " " +hour + ":" +minute + ":" + second);

 总结:系统时间可通过Date类、Calendar类和System中的currentTimeMillis来获取,时间格式可通过调用SimpleDateFormat类和String中的format方法来设置。

posted on 2018-03-20 20:31  追问图  阅读(274)  评论(0编辑  收藏  举报