How to Get Current Date and Time in Java

Channel: Linux
Abstract: System.out.println("Date before 10 daysSystem.out.println(cal.getTime())

There are multiple ways to get the current date and time in Java programming language. Here we will discuss two ways using java.util.Date and java.util.Calendar Classes.

Using java.util.Date Class

Simply create a new Date() object to get the current date and time. Use the following Java program, In this program, we also used DateTimeFormatter class to show results in proper formats as required.

import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; public class GetDateTime { public static void main( String[] args ) { DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); Date date = new Date(); System.out.println(dateFormat.format(date)); } }123456789101112131415import java.text.DateFormat;import java.text.SimpleDateFormat;import java.time.format.DateTimeFormatter;import java.util.Calendar;import java.util.Date; public class GetDateTime{    public static void main( String[] args )    {        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");        Date date = new Date();        System.out.println(dateFormat.format(date));    }}

Output: 04/06/2019 13:23:36
Using java.util.Calendar Class

Use the function Calendar.getInstance() from Calendar class. This provides options for manipulating the calendar fields, such as getting the date of the next week or next month, etc.

You can also use date time format class as above with the below examples.

import java.util.Calendar; public class GetDateTime { public static void main( String[] args ) { Calendar cal = Calendar.getInstance(); System.out.println(cal.getTime()); } }12345678910import java.util.Calendar; public class GetDateTime{    public static void main( String[] args )    {        Calendar cal = Calendar.getInstance();        System.out.println(cal.getTime());    }}

Output: Wed Apr 17 11:57:28 IST 2019

Using the Calendar class, you can also find the date and time in the past or future. For example, what will the date after or before 10 days of the current date. Similarly, we can do the same for years, months, hours, minutes and seconds as well. Please find below a sample Java program.

import java.util.Calendar; public class GetDateTime { public static void main( String[] args ) { Calendar cal = Calendar.getInstance(); System.out.println("Current Date & Time: " + cal.getTime()); cal.add(Calendar.DATE, +10); System.out.println("Date after 10 days: " + cal.getTime()); cal.add(Calendar.DATE, -10); System.out.println("Date before 10 days: " + cal.getTime()); cal.add(Calendar.MONTH, 1); System.out.println("Date after 1 month: " + cal.getTime()); cal.add(Calendar.YEAR, 5); System.out.println("Date after 5 years: " + cal.getTime()); } }12345678910111213141516171819202122import java.util.Calendar; public class GetDateTime{    public static void main( String[] args )    {        Calendar cal = Calendar.getInstance();        System.out.println("Current Date & Time: " + cal.getTime());         cal.add(Calendar.DATE, +10);        System.out.println("Date after 10 days: " + cal.getTime());         cal.add(Calendar.DATE, -10);        System.out.println("Date before 10 days: " + cal.getTime());         cal.add(Calendar.MONTH, 1);        System.out.println("Date after 1 month: " + cal.getTime());         cal.add(Calendar.YEAR, 5);        System.out.println("Date after 5 years: " + cal.getTime());    }}

Output:

Current Date & Time:  Wed Apr 17 12:06:05 IST 2019
Date after 10 days:   Sat Apr 27 12:06:05 IST 2019
Date before 10 days:  Wed Apr 17 12:06:05 IST 2019
Date after 1 month:   Fri May 17 12:06:05 IST 2019
Date after 5 years:   Fri May 17 12:06:05 IST 2024

Ref From: tecadmin

Related articles