//1、通过Date类来获取当前时间
Date date =new Date();
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(date));
//2、通过System类中的currentTimeMillis方法来获取当前时间
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(System.currentTimeMillis()));
//3、通过Calendar类来获取当前时间
Calendar calendar=Calendar.getInstance();//可以对每个时间域单独修改
int year=calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
//4.通过String中的format方法来设置
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);
Java获取系统时间的四种方法
最新推荐文章于 2024-10-03 08:33:02 发布