How to output the date type in Java?
In Java, the SimpleDateFormat class can be used to format a Date object and output it as a string. Here is an example code:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
}
}
In the above example, first create a Date object to represent the current date and time. Then, create a SimpleDateFormat object and specify the desired date format (e.g. “yyyy-MM-dd HH:mm:ss”). Next, use the format method to format the Date object into a string and assign the result to the formattedDate variable. Lastly, use System.out.println to output the formatted date string to the console.
The output will look like: 2022-01-01 12:34:56