Joda-Time Create DateTime with Time Start Of the Day in Java
Tags: Joda Time DateTime Joda DateTime
In this Joda-Time tutorial, we learn how to create a new DateTime object or convert an existing DateTime object to a new DateTime object with the time set to start of the day at 00:00:00.000 in Java.
Add Joda Time library to your Java project
To use Joda Time Java library in the Gradle build project, add the following dependency into the build.gradle file.
compile group: 'joda-time', name: 'joda-time', version: '2.10.9'
To use Joda Time Java library in the Maven build project, add the following dependency into the pom.xml file.
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.9</version>
</dependency>
To download the Joda Time .jar file you can visit Joda Time releases page at github.com/JodaOrg/joda-time
Convert to new DateTime with time set to start of the day
Joda-Time provides DateTime.withTimeAtStartOfDay() method that allows returning a copy of a DateTime object with the time set to start of the day.
TimeAtStartOfDayExample1.java
import org.joda.time.DateTime;
public class TimeAtStartOfDayExample1 {
public static void main(String[] args) {
DateTime dateTime = new DateTime();
DateTime withTimeAtStartOfDay = dateTime.withTimeAtStartOfDay();
System.out.println(dateTime);
System.out.println(withTimeAtStartOfDay);
}
}
2021-01-17T17:15:32.402+07:00
2021-01-17T00:00:00.000+07:00
TimeAtStartOfDayExample2.java
import org.joda.time.DateTime;
public class TimeAtStartOfDayExample2 {
public static void main(String[] args) {
DateTime dateTime = DateTime.now().withTimeAtStartOfDay();
System.out.println(dateTime);
}
}
2021-01-17T00:00:00.000+07:00
Happy Coding 😊
Related Articles
Convert Joda-Time DateTime into Date in Java
Joda-Time Create UTC DateTime in Java
Joda Time DateTime by Examples in Java
Convert SQL Timestamp into Joda-Time DateTime and Vice Versa in Java
Convert Joda-Time DateTime into String and Vice Versa in Java
Convert Joda-Time DateTime into Calendar and Vice Versa in Java
Format Joda-Time DateTime String using ISODateTimeFormat in Java
Compare Joda-Time DateTime using DateTimeComparator in Java
Joda-Time Create DateTime with Time End of the Day in Java
Get Date and Time fields value of Joda-Time DateTime object in Java