JAVA获取当前时间的常用方法

1、使用currentTimeMillis再格式化

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

2、使用Date()

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

3、其他:

使用Calendar类,但除非单独获取年或者月,不建议使用这种方式,月份默认从0开始计算,需加1后才是当前月

Calendar c = Calendar.getInstance(); 
int year = c.get(Calendar.YEAR); 
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DATE);

  

还有其他的方法再罗列就有点孔乙己写回字的意思了。

posted on 2021-12-06 13:55  jzzlo  阅读(5567)  评论(0编辑  收藏  举报