Date d = new Date(); //获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//转换时间格式为12小时制
System.out.println(sdf.format(d));
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//转换时间格式为24小时制
System.out.println(sdf2.format(d));
结果为
2018-08-07 04:23:01
2018-08-07 16:23:01