方法1:
Date date =new Date(); SimpleDateFormat sim=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //以固定格式输出 String str5=sim.format(date); //对象调用方法 System.out.println("现在的时间是:"+str5);
运行结果:
现在的时间是:2017/07/11 20:15:22
方法2:
Calendar c = Calendar.getInstance(); //可以对每个时间域单独修改 int year = c.get(Calendar.YEAR); //获取年 int month = c.get(Calendar.MONTH); //获取月 int date = c.get(Calendar.DATE); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);