How to Printing out datetime in a specific format in Java?

There are many way to Printing out datetime in a specific format in Java.

From java.util.Date package we get the day,date and time.it print the time zone also.
From SimpleDateFormat method we get only date and time.this method use to convert date and time into an appropriate format.
From Java.sql.Date method we get the date only.A java.sql.Date is a java.util.Date.we will supply java.util.Date inside java.sql.Date with getTime method.

				
					import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateUtilDemo {
	public static void main(String[] args) {
		
		java.util.Date ju = new java.util.Date();
		System.out.println("java.util.date format :-- " + ju);
		
		DateFormat df = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
		System.out.println("customized format:-- " + df.format(ju));
		
		java.sql.Date sd = new java.sql.Date(ju.getTime());
		System.out.println("sql format:-- " + sd);
	}
}
				
			

Output :-
java.util.date format :– Tue Aug 15 22:45:14 IST 2023
customized format:– 2023-08-15 10:45:14
sql format:– 2023-08-15

Leave a Comment

Your email address will not be published. Required fields are marked *