Write a Java program to get the current date and time. Sample Solution: Java Code: importjava.util.*;publicclassExercise6{publicstaticvoidmain(String[]args){Calendarnow=Calendar.getInstance();System.out.println();System.out.println("Current full date and time is : "+(now.get(Calendar.MONTH)...
Get Current Date and Time in Java packagecom.callicoder;importjava.time.LocalDateTime;publicclassCurrentDateTimeExample{publicstaticvoidmain(String[] args){// Current Date and TimeLocalDateTimecurrentDateTime=LocalDateTime.now(); System.out.println("Current Date & Time : "+ currentDateTime); } } Out...
import java.util.Calendar; public class Main { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time: " + calendar.getTime()); } } 复制代码 这两种方法都将输出当前的日期和时间。请注意,java.util.Date类在Java 8及更...
LocalDateTime dateTime = LocalDateTime.now(); // get the currentdateand time format的方式也一样 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); System.out.println(dateTime.format(formatter)); 得到的日期结果类似于: 25-11-2018 00:57:20 比较两个日期 SimpleDateFor...
import java.time.Instant; void main() { Instant instant = Instant.now(); System.out.println(instant); } The code example usesjava.time.Instantto get the current date and time. Instant instant = Instant.now(); TheInstant.nowmethod obtains the current instant from the system clock. ...
// Get current dateLocalDatetoday=LocalDate.now();System.out.println(today);//2022-02-15// Get current timeLocalTimenow=LocalTime.now();System.out.println(now);//12:43:51.519251200// Get current date and timeLocalDateTimeinstance=LocalDateTime.now();System.out.println(instance);//2022-02-...
getInstance();System.out.println("Current Date and Time is: "+currentDateTime.getTime());}} ...
Date currentDate = new Date(); System.out.println("Current date and time: " + currentDate); 复制代码 使用Calendar类: Calendar currentDateTime = Calendar.getInstance(); System.out.println("Current date and time: " + currentDateTime.getTime()); 复制代码 这两种方法都会输出当前的日期和时间。需...
Example 1: Get Current date and time in default format import java.time.LocalDateTime; public class CurrentDateTime { public static void main(String[] args) { LocalDateTime current = LocalDateTime.now(); System.out.println("Current Date and Time is: " + current); } } Output Current Date ...
最后一个是LocalDateTime,也是Java中最常用的Date / Time类,代表前两个类的组合 - 即日期和时间的值: LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time 1. format的方式也一样 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); ...