Joda-Time Create UTC DateTime in Java

Tags: Joda Time DateTime DateTimeZone Joda DateTime Joda DateTimeZone UTC

In this Joda-Time tutorial, we learn how to create a DateTime object to represent date and time in Universal Coordinated Time (UTC) time zone 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

Create Current DateTime in UTC using DateTime.now() static method

DateTimeNowUTC.java

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class DateTimeNowUTC {
    public static void main(String[] args) {
        DateTime now = DateTime.now(DateTimeZone.UTC);

        System.out.println("UTC Now: " + now);
    }
}
The output is:
UTC Now: 2021-01-17T09:46:58.189Z

Create Current DateTime in UTC using default constructor

DateTimeUTCFromConstructor.java

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class DateTimeUTCFromConstructor {
    public static void main(String[] args) {
        DateTime now = new DateTime(DateTimeZone.UTC);

        System.out.println("UTC Now: " + now);
    }
}
The output is:
UTC Now: 2021-01-17T09:47:16.930Z

Create DateTime in UTC from milliseconds

DateTimeMillisecondsUTC.java

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class DateTimeMillisecondsUTC {
    public static void main(String[] args) {
        long currentTimeInMilliseconds = System.currentTimeMillis();

        DateTime dateTime = new DateTime(currentTimeInMilliseconds, DateTimeZone.UTC);

        System.out.println("Current time in milliseconds: " + currentTimeInMilliseconds);
        System.out.println("DateTime UTC: " + dateTime);
    }
}
The output is:
Current time in milliseconds: 1610876844911
DateTime UTC: 2021-01-17T09:47:24.911Z

Create DateTime in UTC from specified date and time

DateTimeUTC.java

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class DateTimeUTC {
    public static void main(String[] args) {
        int year = 2022;
        int monthOfYear = 1;
        int dayOfMonth = 1;
        int hourOfDay = 1;
        int minuteOfHour = 9;
        int secondOfMinute = 0;

        DateTime dateTime = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, DateTimeZone.UTC);

        System.out.println("DateTime UTC: " + dateTime);
    }
}
The output is:
DateTime UTC: 2022-01-01T01:09:00.000Z

Happy Coding 😊

Convert Joda-Time DateTime into Date in Java

Joda-Time Create DateTime with Time Start Of the Day 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