利用SimpleDateFormat对象的format方法获取当前时间,并将其存储在一个字符串中。 StringcurrentTime=sdf.format(newDate()); 1. 这里的new Date()会获取当前系统时间。 步骤4:格式化时间单位 最后,你可以输出或者使用currentTime这个字符串,里面包含了当前时间的信息。 System.out.println("当前时间为:"+currentTime)...
System.out.println(dateFormat.format(timeNow)); //获取小时 System.out.println(timeNow.getHour()); //获取分钟 System.out.println(timeNow.getMinute()); //获取秒数 System.out.println(timeNow.getSecond()); 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. LocalTime和LocalDate大同小异,用法...
通过System类中的currentTimeMillis方法来获取当前时间,无需导入类,速度最快。3、通过Calendar类来获取当前时间 Calendar calendar=Calendar.getInstance();intyear =calendar.get(Calendar.YEAR);intmonth =calendar.get(Calendar.MONTH);intdate =calendar.get(Calendar.DATE);inthour =calendar.get(Calendar.HOUR_OF_D...
printStackTrace(); } /** * 获取现在时间 * * @return返回字符串格式 yyyy-MM-dd HH:mm:ss */ public static String getStringDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentTime...
System.out.println(dft.format(System.currentTimeMillis())); } } 第三种:Calendar.getInstance()(国际通用) publicclassDateDemo {publicstaticvoidmain(String[] args) { Calendar date=Calendar.getInstance(); System.out.println(date.getTime());//可以单独获取时间System.out.println("年:"+date.get(Ca...
//方法 一System.currentTimeMillis();//方法 二Calendar.getInstance().getTimeInMillis();//方法 三newDate().getTime(); 获取当前时间 代码语言:javascript 复制 此代码由Java架构师必看网-架构君整理 SimpleDateFormat df=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式String date=df.forma...
System.out.println(dft.format(System.currentTimeMillis())); } } 第三种:Calendar.getInstance()(国际通用) public class DateDemo { public static void main(String[] args) { Calendar date = Calendar.getInstance(); System.out.println(date.getTime()); ...
System.out.println(getCurrentDateTime()); System.out.println(getLocalDateTimeByString("2022-05-05 15:35:20.222", FormatPattern.DATE_TIME_FORMAT.getFormat())); } /** * 获取当前日期: yyyy-mm-dd hh:mm:ss.SSS * @return */ public static LocalDateTime getCurrentDateTime() { ...
long currentTime=Calendar.getInstance().getTimeInMillis(); } } public static void testDate(long times){ for(int i=0;i long currentTime=new Date().getTime(); } } } 执行结果 133 2372 137 Calendar.getInstance().getTimeInMillis() 这种方式速度最慢,这是因为Canlendar要处理时区问题会耗费较多...
对于java.time.ZonedDateTime, 使用ZonedDateTime.now() // get current date-time, with system default time zoneDateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"); ZonedDateTime now = ZonedDateTime.now(); System.out.println(dtf.format(now));// 2021/03/22 16:37:15...