Get date without time in Java
This post will discuss how to get a date without time in Java.
1. Using Joda Time Library
The Joda-Time date-time library provides the high-quality replacement of standard date and time classes prior to Java SE 8. To get the date without time, you can use the LocalDate class.
|
1 2 3 4 5 6 7 8 |
import org.joda.time.LocalDate; public class Main { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println(date); } } |
Output (will vary):
2021-12-24
The Joda Time library DateTimeZone class provides access to the time zone through factory method forID():
|
1 2 3 4 5 6 7 8 9 10 |
import org.joda.time.DateTimeZone; import org.joda.time.LocalDate; public class Main { public static void main(String[] args) { DateTimeZone zone = DateTimeZone.forID("Europe/London"); LocalDate date = LocalDate.now(zone); System.out.println(date); } } |
Output (will vary):
2021-12-24
2. Using LocalDate class
Similar to the Joda-Time library, Java 8 java.time package included a LocalDate class to represent a date without a time-zone.
|
1 2 3 4 5 6 7 8 |
import java.time.LocalDate; public class Main { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println(date); } } |
Output (will vary):
2021-12-24
To obtain the current date in the specified time zone, you can specify a Zone ID to the LocalDate.now() method.
|
1 2 3 4 5 6 7 8 9 |
import java.time.LocalDate; import java.time.ZoneId; public class Main { public static void main(String[] args) { LocalDate date = LocalDate.now(ZoneId.of("Europe/Paris")); System.out.println(date); } } |
Output (will vary):
2021-12-24
3. Using Date object
The Date object is initialized with the current date and time when its no-arg constructor is called. To parse only date (without time) in the specified format, use the DateFormat class.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) throws ParseException { DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); String date = formatter.format(new Date()); System.out.println(date); } } |
Output (will vary):
12-24-2021
4. Using Apache Commons Lang
The DateUtils class from Apache Commons Lang library provides a lot of useful utilities for Date and time manipulation. You can call its truncate() method to truncate a Date, as shown below.
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.apache.commons.lang3.time.DateUtils; import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] args) { Date date = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH); System.out.println(date); } } |
Output (will vary):
Sun Dec 24 00:00:00 PST 2021
That’s all about getting a date without time in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)