Java 获取当前日期 时间

方法一:使用Date

Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(d);
System.out.println(str);

方法二:使用Calendar

Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);

int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);

System.out.println(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);

你可能感兴趣的:(JAVA,Java)