importjava.time.LocalDateTime;importjava.time.format.DateTimeFormatter;publicclassGetCurrentTimeExample{publicstaticvoidmain(String[] args){LocalDateTimecurrentTime=LocalDateTime.now();DateTimeFormatterformatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");StringformattedTime=currentTime.format(formatter); S...
importjava.util.Date;importjava.text.SimpleDateFormat;publicclassCurrentTimeExample{publicstaticvoidmain(String[]args){Datedate=newDate();SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");StringformattedTime=sdf.format(date);System.out.println("当前时间:"+formattedTime);}} 1. 2. ...
formatter.setTimeZone(TimeZone.getTimeZone("UTC")); // format date String formatted = formatter.format(date); Java新的日期/时间API 旧类(Date,Calendar和SimpleDateFormat)存在许多问题和设计问题,并且它们将被新API替代。 如果您使用的是Java 8,请考虑使用新的java.time API。与旧的API相比,它更容易,...
String formattedTime = currentTime.format(formatter); System.out.println("当前时间(格式化后): " + formattedTime); // 分解时间各部分 int year = currentTime.getYear(); int month = currentTime.getMonthValue(); int day = currentTime.getDayOfMonth(); int hour = currentTime.getHour(); int ...
This gets us the formatted string output. Example 3: Get Current Date time using predefined constants import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CurrentDateTime { public static void main(String[] args) { LocalDateTime current = LocalDateTime.now(); ...
longtomorrow=currentTime+oneDay; //比较两个日期的先后 if(date1getTime:0351s.com;()>date2getTime()){ Systemoutprintln(date1isafterdate2); } //计算两个日期之间的时间差 longdifference=date2getTime()-date1getTime(); longdays=difference/(24*60*60*1000); ...
boolean isAfter = specificDateTime.isAfter(currentDateTime); 格式化和解析: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = specificDateTime.format(formatter); LocalDateTime parsedDateTime = LocalDateTime.parse("2022-01-01 12:30:00", formatter...
与 获取日期 例子很像,获取时间使用的是LocalTime类,一个只有时间没有日期的LocalDate近亲。可以调用静态工厂方法now()来获取当前时间。默认的格式是hh:mm:ss:nnn。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 //获取当前时间publicvoidgetCurrentTime(){LocalTime time=LocalTime.now();System.out.print...
LocalDateTimeinstance=LocalDateTime.now();DateTimeFormatterformatter=DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm");StringformattedString=formatter.format(instance);//15-02-2022 12:43 2. Get Current Date and Time (Java 7 or Earlier) 2.1. Core Classes ...
import java.time.LocalDateTime;public class DateTimeExample { public static void main(String[] args)...