×

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.


Related Topics

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Java String Reader

StreamReaderClass: This class is present in the java.io package. It is a character stream in which string act as a source.This method provides to read characters from a string. Character Stream: This class...

3 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

How to compare two dates in different format in Java?

We need to compare two dates frequently when coding. Real-world examples include sorting a list of persons by age or keeping track of students' attendance. To compare two dates, we...

7 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.