Convert Joda-Time DateTime into String and Vice Versa in Java
Tags: Joda Time Joda DateTime Joda DateTimeFormat Joda DateTimeFormatter String Convert
In this Joda-Time tutorial, we learn how to convert Joda-Time’s DateTime object into String value and vice versa parse String value into Joda-Time’s DateTime 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 DateTime to String in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZZ)
DateTimeToStringISO.java
import org.joda.time.DateTime;
public class DateTimeToStringISO {
public static void main(String[] args) {
DateTime dateTime = DateTime.now();
String dateTimeString = dateTime.toString();
System.out.println(dateTimeString);
}
}
2021-01-18T21:37:33.515+07:00
Convert DateTime to String in specified format
DateTimeToString.java
import org.joda.time.DateTime;
public class DateTimeToString {
public static void main(String[] args) {
DateTime dateTime = DateTime.now();
String dateTimeString = dateTime.toString("DD/MM/yyyy");
System.out.println(dateTimeString);
}
}
18/01/2021
Convert DateTime to String using DateTimeFormatter
DateTimeFormatterExample.java
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
DateTime dateTime = DateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("DD/MM/yyyy HH:mm:ss");
String dateTimeString = dateTimeFormatter.print(dateTime);
System.out.println(dateTimeString);
}
}
18/01/2021 21:38:07
Parse String into DateTime using DateTime.parse()
ParseStringToDateTime.java
import org.joda.time.DateTime;
public class ParseStringToDateTime {
public static void main(String[] args) {
String dateTimeString = "2021-01-18T20:42:10.670+07:00";
DateTime dateTime = DateTime.parse(dateTimeString);
System.out.println(dateTime);
}
}
2021-01-18T20:42:10.670+07:00
Parse String into DateTime using DateTime.parse() with DateTimeFormatter
ParseStringWithDateTimeFormatter.java
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class ParseStringWithDateTimeFormatter {
public static void main(String[] args) {
String dateTimeString = "18/01/2021";
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("DD/MM/yyyy");
DateTime dateTime = DateTime.parse(dateTimeString, dateTimeFormatter);
System.out.println(dateTime);
}
}
2021-01-18T00:00:00.000+07:00
Parse String into DateTime using DateTimeFormatter.parseDateTime()
ParseDateTimeExample.java
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class ParseDateTimeExample {
public static void main(String[] args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("DD/MM/yyyy HH:mm:ss");
String dateTimeString = "18/01/2021 08:30:00";
DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeString);
System.out.println(dateTime);
}
}
2021-01-18T08:30: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
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 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