Convert Joda-Time DateTime into Date in Java
Tags: Joda Time DateTime Joda DateTime Date Convert
In this Joda-Time tutorial, we learn how to convert a Joda-Time’s DateTime object into a java.util.Date object 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 Joda-Time DateTime into Date in Java
To convert a DateTime object to java.util.Date object, Joda-Time provides DateTime.toDate() method.
ConvertJodaTimeDateTimeToDate.java
import org.joda.time.DateTime;
import java.util.Date;
public class ConvertJodaTimeDateTimeToDate {
public static void main(String[] args) {
DateTime now = DateTime.now();
Date date = now.toDate();
System.out.println("Joda-Time DateTime: " + now);
System.out.println("Java Date: " + date);
}
}
Joda-Time DateTime: 2021-01-17T16:05:40.972+07:00
Java Date: Sun Jan 17 16:05:40 ICT 2021
ConvertJodaTimeDateTimeToDate2.java
import org.joda.time.DateTime;
import java.util.Date;
public class ConvertJodaTimeDateTimeToDate2 {
public static void main(String[] args) {
Date date = DateTime.now().toDate();
System.out.println(date);
}
}
Sun Jan 17 17:01:07 ICT 2021
Happy Coding 😊
Related Articles
Joda Time DateTime by Examples in Java
Joda-Time Create UTC DateTime 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
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