Joda-Time Create DateTime with Time End of the Day in Java
Tags: Joda Time 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 end of the day at 23:59:59.999 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
Using withTime() method to create a new copy of DateTime with time end of day
DateTimeEndOfDayExample1.java
import org.joda.time.DateTime;
public class DateTimeEndOfDayExample1 {
public static void main(String args[]) {
DateTime now = DateTime.now();
DateTime endOfDayDateTime = now.withTime(23, 59, 59, 999);
System.out.println("Now: " + now);
System.out.println("End Of Day: " + endOfDayDateTime);
}
}
Now: 2021-01-19T22:19:44.177+07:00
End Of Day: 2021-01-19T23:59:59.999+07:00
Using withTimeAtStartOfDay().plusDays(1).minusMillis(1) method chains
DateTimeEndOfDayExample2.java
import org.joda.time.DateTime;
public class DateTimeEndOfDayExample2 {
public static void main(String args[]) {
DateTime now = DateTime.now();
DateTime endOfDayDateTime = now.withTimeAtStartOfDay().plusDays(1).minusMillis(1);
System.out.println("Now: " + now);
System.out.println("End Of Day: " + endOfDayDateTime);
}
}
Now: 2021-01-19T22:20:10.081+07:00
End Of Day: 2021-01-19T23:59:59.999+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
Joda-Time Create DateTime with Time Start Of the Day 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
Get Date and Time fields value of Joda-Time DateTime object in Java