Introduction

This tutorial shows how we can convert joda date time to SQL timestamp or vice versa.

Java’s standard date and time classes (mainly java.util.Date and java.util.Calendar) have limited functionality and have a number of design problems. The fact that many of constructors and methods of java.util.Date are deprecated.

Joda Time has a better thought out API than what is available in Java’s date and time standard library. There are classes for timestamps with or without a timezone, classes for holding only a date (year, month, day) or only a time of day, classes for periods, durations and intervals, it supports the ISO 8601 format (which is the standard format in XML documents) and much more.

Now JDK 8 is out and it has a new date and time API which is similar to Joda Time. For more information please read package java.time.

Prerequisites

Java – any version, Eclipse 4.12, Maven 3.6.1 or Gradle 5.6, Joda API

Creating Project

Create a gradle based project in Eclipse. The project’s name is joda-to-sql-date-vice-versa.

The build.gradle script is updated to include the Joda API as shown in the below:

plugins {
    id 'java-library'
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    jcenter()
}

dependencies {
    implementation 'joda-time:joda-time:2.10.3'
}

If you are using Maven based project then you can use below dependency in your pom.xml file:

<!-- joda date time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.3</version>
</dependency>

Creating Main Class

Write a class which will show how to use joda date time and convert them to SQL timestamp or vice versa.

package com.roytuts.joda.to.sql.date.vice.versa;

import java.sql.Date;
import java.sql.Timestamp;

import org.joda.time.LocalDateTime;

public class JodaToSqlAndViceVersa {

	public static void main(String[] args) {
		LocalDateTime localDateTime = new LocalDateTime();
		System.out.println("LocalDateTime : " + localDateTime);
		System.out.println("LocalDateTime to SQL Date : " + jodatToSQLDate(localDateTime));
		Timestamp timestamp = jodaToSQLTimestamp(localDateTime);
		System.out.println("LocalDateTime to SQL Timestamp : " + timestamp);
		System.out.println("SQL Timestamp to LocalDateTime : " + sqlTimestampToJodaLocalDateTime(timestamp));
	}

	public static Date jodatToSQLDate(LocalDateTime localDateTime) {
		return new Date(localDateTime.toDateTime().getMillis());
	}

	public static Timestamp jodaToSQLTimestamp(LocalDateTime localDateTime) {
		return new Timestamp(localDateTime.toDateTime().getMillis());
	}

	public static LocalDateTime sqlTimestampToJodaLocalDateTime(Timestamp timestamp) {
		return LocalDateTime.fromDateFields(timestamp);
	}

}

Testing the Application

Now run the above main class and you will see the following output in the console.

LocalDateTime : 2019-09-17T18:09:54.855
LocalDateTime to SQL Date : 2019-09-17
LocalDateTime to SQL Timestamp : 2019-09-17 18:09:54.855
SQL Timestamp to LocalDateTime : 2019-09-17T18:09:54.855

Source Code

You can download source code.

That’s all. Thank you for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *