×

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

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Java finally

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection...

5 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.