How to get the current date and time in Java
Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many different use times. Whilst we submit blogs on a website, the date of posting receives written down into a database and proven to the reader.
When we make a movement, we'd need to realize the time to be had so that we can preserve songs of them. Long tale short, getting the modern-day date and time in Java could be very vital and has a myriad of usages, and luckily, it is straightforward to acquire it for any form of use. In Java, there are a couple of instructions by means of which we can get the contemporary date and time.
The date class construct: The no arg constructor of the Java.util.Date magnificence returns the Date object representing the present day date and time; the use of this, you can print the modern date as proven below –
Program
We write a program about the date class construction.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Date {
public static void main (String args[] throws ParseException)
{
Date d = new Date();
SimpleDateFormat formatter = new SimpleDateFormat (“dd/mm/yyyy”);
String str = formatter.format(date);
System.out.println (“The current date is: ” + str);
}
}
Result
We compile this system and also run it. The output is given below:
The current date is: 05/05/2000
1.Get current date and time in Java: We use the extension in this program, java.time.format.DateTimeFormatter. The LocalDateTime.now() approach returns the instance of LocalDateTime elegance. To format the present day date, you may use DateTimeFormatter class that's covered in JDK 1.8.
Program
We write a utility approximately how we can get the modern date and time in Java.
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class CurrentDateTime
{
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(datetimeformat.format(now));
}
}
Result
We compile this system and also run it. The output is given below:
2000/05/05 11:25:14
2. Get current date in java.time.LocalDate Format: We use the extension in this program, Java.time.LocalDate. The LocalDate.now() method returns the example of the LocalDate class. If we print the instance of the LocalDate class, it prints the cutting-edge date.
Program
We write an utility approximately how we can get the modern date in Java.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentDateTime
{
public static void main(String[] args)
{
System.out.println(java.time.LocalDate.now());
}
}
Result
We compile this system and also run it. The output is given below:
2000-05-05
3. Calendar class: The getInstance() (without arguments) technique of this elegance returns the Calendar object representing the modern date and time, the use of this; you can print the present day date fee as proven below Program
We write a program about how we can get a calendar class in Java.
Import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
public class Test
{
public static void main (String args[] throws ParseException)
{\
DateFormat formatter = new Date();
DateFormat formatter = new SimpleDateFormat (“dd/mm/yyyy”);
Calendar obj = Calendar.getInstance();
String str = formatter.format(obj.getTime());
System.out.println (“The current date is: ” + str);
}
}
Result
We compile this system and also run it. The output is given below:
The current date is: 05/05/2000
4. Find the Current date: With the aid of printing the example of java.sql.Date elegance, you could print the current date in JavaJava. It does not print time. This date instance is usually used to shop the modern-day date inside the database.
Program
We write a program about how we can get the current date in JavaJava
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentDateTime
{
public static void main(String[] argvs)
{
long millis=System.currentTimeMillis();
java.sql.Date date = new java.sql.Date(millis);
System.out.println(date);
}
}
Result
We compile this system and also run it. We also share the output which is given below:
2016-03-17
So, here we briefly discuss how to get the current date and time in Java. Here we discuss about various process. We also give some examples and give the result of the following program. Therefore, we get the clear idea about current date and time in Java.