Get the current date without time in Java
Hello, Coders!! In this tutorial, we will discuss the methods of getting the current date without time in Java.
The previous versions do have not any way to get a date without the time unless we were using the Joda-time library.
In previous versions, the time on a date was impossible to ignore, as we have to represent the Date class by a specific instant of time. So, to solve this problem, Java 8 and later versions introduced a time API at a later time.
Methods to get the current date without time in older versions of Java
Method1: Using Calendar Class
By using the Calendar class the time can be set to zero, that’s how we can get a clean date without time.
Let’s do a program applying this method:
import java.util.Calendar; class GetCurDate { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); System.out.println(cal.getTime()); } }
Output:
Fri Dec 10 00:00:00 GMT 2021
As we can see, we have got the current date with the time set to zero, as we can’t ignore the time here.
Here have imported the Calendar class to our program that provides methods for conversion between an instant of time and a set of fields of calendar like HOUR_OF_DATE, MINUTE, SECOND, MILLISECOND, etc.
Then we have created an instance of the Calendar class and set it with only the current date calendar fields by setting the time to zero.
Method2: Using a Formatter
In this method, we will format the Date into a string and then convert the string to Date again to get only the current date and no time value.
Let’s do a program applying this method:
import java.util.Date; import java.text.SimpleDateFormat; class HelloWorld { public static void main(String[] args) { SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); try { System.out.println(formatter.parse(formatter.format(new Date()))); } catch(Exception e) { //ParseException } } }
Output:
Sat Dec 10 00:00:00 GMT 2021
Here we have imported the Date class to get the current Date and SimpleDateFormat class to format the date only.
Method to get the current date without time in Java 8 or later versions
In Java 8 or later versions of Java, we can get the date without the time easily by using the LocalDate class of java.time package.
Example:
import java.time.LocalDate; public class Main { public static void main(String[] args){ LocalDate lo_date = LocalDate.now(); System.out.println("Current date: " + lo_date); } }
Output:
Current date: 2021-12-10
Here we have simply created the instance of the LocalDate class with the now() method that set the value to the current date and then printed the LocalDate object to the screen without any time value.
Hope you have enjoyed learning this article and learned how to get the current date without time in different versions of Java.
You can also read, currentTimeMillis() Function In Java
Leave a Reply