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);
還有其他的方法再羅列就有點孔乙己寫回字的意思了。