Joda Time LocalTime by Examples in Java

Tags: Joda Time LocalTime DateTime DateTimeZone Chronology GregorianChronology ISOChronology Instant DateTimeFieldType DurationFieldType Period Locale Joda LocalTime Joda DateTime Joda DateTimeZone Joda Chronology Joda GregorianChronology Joda ISOChronology Joda Instant Joda DateTimeFieldType Joda DurationFieldType Joda Period

In this Java tutorial, we learn how to use the org.joda.time.LocalTime class of Joda Time library by different example Java programs.

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

How to create LocalTime object using constructors

In the following Java program we use default constructor to create new LocalTime objects representing the current time of the system as well as create time by time zone and chronology.

JodaTimeLocalTimeExample1.java

import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeLocalTimeExample1 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();

        LocalTime localTime1 = new LocalTime();
        LocalTime localTime2 = new LocalTime(dateTimeZone);
        LocalTime localTime3 = new LocalTime(chronology);

        System.out.println(localTime1);
        System.out.println(localTime2);
        System.out.println(localTime3);
    }
}
The output is:
11:56:52.192
04:56:52.193
11:56:52.193

In the following Java program, the LocalTime object is created from a given milliseconds value.

JodaTimeLocalTimeExample2.java

import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeLocalTimeExample2 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();
        int milliSeconds = 9000;

        LocalTime localTime1 = new LocalTime(milliSeconds);
        LocalTime localTime2 = new LocalTime(milliSeconds, dateTimeZone);
        LocalTime localTime3 = new LocalTime(milliSeconds, chronology);

        System.out.println(localTime1);
        System.out.println(localTime2);
        System.out.println(localTime3);
    }
}
The output is:
07:00:09.000
01:00:09.000
07:00:09.000

Or create LocalTime from a given Instant object.

JodaTimeLocalTimeExample3.java

import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.Instant;
import org.joda.time.LocalTime;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeLocalTimeExample3 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = GregorianChronology.getInstance();
        Instant instant = new Instant();

        LocalTime localTime1 = new LocalTime(instant);
        LocalTime localTime2 = new LocalTime(instant, dateTimeZone);
        LocalTime localTime3 = new LocalTime(instant, chronology);

        System.out.println(localTime1);
        System.out.println(localTime2);
        System.out.println(localTime3);
    }
}
The output is:
04:57:33.294
04:57:33.294
11:57:33.294

Or from a given hour and minute values.

JodaTimeLocalTimeExample4.java

import org.joda.time.Chronology;
import org.joda.time.LocalTime;
import org.joda.time.chrono.GregorianChronology;

public class JodaTimeLocalTimeExample4 {
    public static void main(String[] args) {
        Chronology chronology = GregorianChronology.getInstance();
        int hourOfDay = 10;
        int minuteOfHour = 20;
        int secondOfMinute = 30;
        int millisOfSecond = 400;

        LocalTime localTime1 = new LocalTime(hourOfDay, minuteOfHour);
        LocalTime localTime2 = new LocalTime(hourOfDay, minuteOfHour, secondOfMinute);
        LocalTime localTime3 = new LocalTime(hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
        LocalTime localTime4 = new LocalTime(hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, chronology);

        System.out.println(localTime1);
        System.out.println(localTime2);
        System.out.println(localTime3);
        System.out.println(localTime4);
    }
}
The output is:
10:20:00.000
10:20:30.000
10:20:30.400
10:20:30.400

How to use LocalTime.now() static method to instantiate new LocalTime object

JodaTimeLocalTimeExample5.java

import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
import org.joda.time.chrono.ISOChronology;

public class JodaTimeLocalTimeExample5 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        Chronology chronology  = ISOChronology.getInstance();

        LocalTime localTime1 = LocalTime.now();
        LocalTime localTime2 = LocalTime.now(dateTimeZone);
        LocalTime localTime3 = LocalTime.now(chronology);

        System.out.println(localTime1);
        System.out.println(localTime2);
        System.out.println(localTime3);
    }
}
The output is:
11:59:43.410
04:59:43.410
11:59:43.410

Parse a String to LocalTime object using LocalTime.parse() static method

JodaTimeLocalTimeExample6.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample6 {
    public static void main(String[] args) {
        LocalTime localTime = LocalTime.parse("01:10:20.300");

        System.out.println(localTime);
    }
}
The output is:
01:10:20.300

Convert milliseconds to LocalTime

In the following Java program we use LocalTime.fromMillisOfDay() static method to create a LocalTime object from a milliseconds value.

JodaTimeLocalTimeExample7.java

import org.joda.time.Chronology;
import org.joda.time.LocalTime;
import org.joda.time.chrono.ISOChronology;

public class JodaTimeLocalTimeExample7 {
    public static void main(String[] args) {
        Chronology chronology  = ISOChronology.getInstance();
        int milliSeconds = 60000;// 60000 milliseconds = 60 seconds = 1 minute

        LocalTime localTime1 = LocalTime.fromMillisOfDay(milliSeconds);
        LocalTime localTime2 = LocalTime.fromMillisOfDay(milliSeconds, chronology);

        System.out.println(localTime1);
        System.out.println(localTime2);
    }
}
The output is:
00:01:00.000
00:01:00.000

Convert Calendar instance to LocalTime

In the following Java program, we use LocalTime.fromCalendarFields() static method to create LocalTime object from a given Calendar object.

JodaTimeLocalTimeExample8.java

import org.joda.time.LocalTime;

import java.util.Calendar;

public class JodaTimeLocalTimeExample8 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        LocalTime localTime = LocalTime.fromCalendarFields(calendar);

        System.out.println(localTime);
    }
}
The output is:
12:00:35.036

Convert Date to LocalTime

To convert Java core Date objects to Joda LocalTime objects we can use LocalTime.fromDateFields() static method as following Java program.

JodaTimeLocalTimeExample9.java

import org.joda.time.LocalTime;

import java.util.Date;

public class JodaTimeLocalTimeExample9 {
    public static void main(String[] args) {
        Date date = new Date();

        LocalTime localTime = LocalTime.fromDateFields(date);

        System.out.println(localTime);
    }
}
The output is:
12:00:55.445

How to get specific fields of a LocalTime object

JodaTimeLocalTimeExample10.java

import org.joda.time.DateTimeFieldType;
import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample10 {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();

        int hourOfDay = now.get(DateTimeFieldType.hourOfDay());
        int minuteOfHour = now.get(DateTimeFieldType.minuteOfHour());
        int secondOfMinute = now.get(DateTimeFieldType.secondOfMinute());
        int millisOfSecond = now.get(DateTimeFieldType.millisOfSecond());

        System.out.println(hourOfDay);
        System.out.println(minuteOfHour);
        System.out.println(secondOfMinute);
        System.out.println(millisOfSecond);
    }
}
The output is:
12
1
10
608

JodaTimeLocalTimeExample11.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample11 {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();

        int hourOfDay = now.getHourOfDay();
        int minuteOfHour = now.getMinuteOfHour();
        int secondOfMinute = now.getSecondOfMinute();
        int millisOfSecond = now.getMillisOfSecond();

        System.out.println(hourOfDay);
        System.out.println(minuteOfHour);
        System.out.println(secondOfMinute);
        System.out.println(millisOfSecond);
    }
}
The output is:
12
1
21
140

How to compare LocaTime objects

Using compareTo() method.

JodaTimeLocalTimeExample12.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample12 {
    public static void main(String[] args) {
        LocalTime localTime1 = new LocalTime(1, 1, 1);
        LocalTime localTime2 = new LocalTime(2, 2, 2);
        LocalTime localTime3 = new LocalTime(1, 1, 1);

        System.out.println("localTime1 compare to localTime2:" + localTime1.compareTo(localTime2));
        System.out.println("localTime1 compare to localTime3:" + localTime1.compareTo(localTime3));
        System.out.println("localTime2 compare to localTime1:" + localTime2.compareTo(localTime1));
        System.out.println("localTime3 compare to localTime1:" + localTime3.compareTo(localTime1));
    }
}
The output is:
localTime1 compare to localTime2:-1
localTime1 compare to localTime3:0
localTime2 compare to localTime1:1
localTime3 compare to localTime1:0

Using equals() method.

JodaTimeLocalTimeExample13.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample13 {
    public static void main(String[] args) {
        LocalTime localTime1 = new LocalTime(1, 1, 1);
        LocalTime localTime2 = new LocalTime(2, 2, 2);
        LocalTime localTime3 = new LocalTime(1, 1, 1);

        System.out.println("localTime1 equals localTime2:" + localTime1.equals(localTime2));
        System.out.println("localTime1 equals localTime3:" + localTime1.equals(localTime3));
    }
}
The output is:
localTime1 equals localTime2:false
localTime1 equals localTime3:true

How to use LocalTime.withField() method

JodaTimeLocalTimeExample14.java

import org.joda.time.DateTimeFieldType;
import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample14 {
    public static void main(String[] args) {
        LocalTime localTime = new LocalTime();

        LocalTime localTime1 = localTime.withField(DateTimeFieldType.hourOfDay(), 8);
        LocalTime localTime2 = localTime.withField(DateTimeFieldType.minuteOfHour(), 10);
        LocalTime localTime3 = localTime.withField(DateTimeFieldType.secondOfMinute(), 20);
        LocalTime localTime4 = localTime.withField(DateTimeFieldType.millisOfSecond(), 300);

        System.out.println(localTime);
        System.out.println(localTime1);
        System.out.println(localTime2);
        System.out.println(localTime3);
        System.out.println(localTime4);
    }
}
The output is:
12:07:52.065
08:07:52.065
12:10:52.065
12:07:20.065
12:07:52.300

How to use LocalTime.withFieldAdded() method

JodaTimeLocalTimeExample15.java

import org.joda.time.DurationFieldType;
import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample15 {
    public static void main(String[] args) {
        LocalTime localTime = new LocalTime();

        LocalTime localTime1 = localTime.withFieldAdded(DurationFieldType.hours(), 1);
        LocalTime localTime2 = localTime.withFieldAdded(DurationFieldType.minutes(), 10);
        LocalTime localTime3 = localTime.withFieldAdded(DurationFieldType.seconds(), 20);
        LocalTime localTime4 = localTime.withFieldAdded(DurationFieldType.millis(), 300);

        System.out.println(localTime);
        System.out.println(localTime1);
        System.out.println(localTime2);
        System.out.println(localTime3);
        System.out.println(localTime4);
    }
}
The output is:
12:08:08.910
13:08:08.910
12:18:08.910
12:08:28.910
12:08:09.210

How to use LocalTime.withPeriodAdded() method

JodaTimeLocalTimeExample16.java

import org.joda.time.LocalTime;
import org.joda.time.Period;

public class JodaTimeLocalTimeExample16 {
    public static void main(String[] args) {
        Period oneMinutePeriod = new Period(60000); // 60000 milliseconds = 60 seconds = 1 minute
        LocalTime now = new LocalTime();

        LocalTime nextOneMinute = now.withPeriodAdded(oneMinutePeriod, 1);
        LocalTime nextTwoMinutes = now.withPeriodAdded(oneMinutePeriod, 2);

        System.out.println(now);
        System.out.println(nextOneMinute);
        System.out.println(nextTwoMinutes);
    }
}
The output is:
12:08:23.238
12:09:23.238
12:10:23.238

How to use LocalTime.plus() method

JodaTimeLocalTimeExample17.java

import org.joda.time.LocalTime;
import org.joda.time.Period;

public class JodaTimeLocalTimeExample17 {
    public static void main(String[] args) {
        Period oneMinutePeriod = new Period(60000); // 60000 milliseconds = 60 seconds = 1 minute
        LocalTime now = new LocalTime();

        LocalTime nextOneMinute = now.plus(oneMinutePeriod);

        System.out.println(now);
        System.out.println(nextOneMinute);
    }
}
The output is:
12:08:37.245
12:09:37.245

How to use LocalTime.plusHours() method

JodaTimeLocalTimeExample18.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample18 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime nextHour = now.plusHours(1);

        System.out.println(now);
        System.out.println(nextHour);
    }
}
The output is:
12:08:51.442
13:08:51.442

How to use LocalTime.plusMinutes() method

JodaTimeLocalTimeExample19.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample19 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime nextTenMinutes = now.plusMinutes(10);

        System.out.println(now);
        System.out.println(nextTenMinutes);
    }
}
The output is:
12:09:05.623
12:19:05.623

How to use LocalTime.plusSeconds() method

JodaTimeLocalTimeExample20.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample20 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime nextMinutes = now.plusSeconds(60);

        System.out.println(now);
        System.out.println(nextMinutes);
    }
}
The output is:
12:09:19.012
12:10:19.012

How to use LocalTime.plusMillis() method

JodaTimeLocalTimeExample21.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample21 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime nextSecond = now.plusMillis(1000);

        System.out.println(now);
        System.out.println(nextSecond);
    }
}
The output is:
12:09:36.650
12:09:37.650

How to use LocalTime.minus() method

JodaTimeLocalTimeExample22.java

import org.joda.time.LocalTime;
import org.joda.time.Period;

public class JodaTimeLocalTimeExample22 {
    public static void main(String[] args) {
        Period oneMinutePeriod = new Period(60000); // 60000 milliseconds = 60 seconds = 1 minute
        LocalTime now = new LocalTime();

        LocalTime lastMinute = now.minus(oneMinutePeriod);

        System.out.println(now);
        System.out.println(lastMinute);
    }
}
The output is:
12:09:48.147
12:08:48.147

How to use LocalTime.minusHours() method

JodaTimeLocalTimeExample23.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample23 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime lastHour = now.minusHours(1);

        System.out.println(now);
        System.out.println(lastHour);
    }
}
The output is:
12:10:00.687
11:10:00.687

How to use LocalTime.minusMinutes() method

JodaTimeLocalTimeExample24.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample24 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime lastTenMinutes = now.minusMinutes(10);

        System.out.println(now);
        System.out.println(lastTenMinutes);
    }
}
The output is:
12:10:13.012
12:00:13.012

How to use LocalTime.minusSeconds() method

JodaTimeLocalTimeExample25.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample25 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime lastMinutes = now.minusSeconds(60);

        System.out.println(now);
        System.out.println(lastMinutes);
    }
}
The output is:
12:10:25.797
12:09:25.797

How to use LocalTime.minusMillis() method

JodaTimeLocalTimeExample26.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample26 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime lastSecond = now.minusMillis(1000);

        System.out.println(now);
        System.out.println(lastSecond);
    }
}
The output is:
12:10:37.588
12:10:36.588

How to use LocalTime.withHourOfDay() method

JodaTimeLocalTimeExample27.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample27 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime atEightToday = now.withHourOfDay(8);

        System.out.println(now);
        System.out.println(atEightToday);
    }
}
The output is:
12:10:49.186
08:10:49.186

How to use LocalTime.withMinuteOfHour() method

JodaTimeLocalTimeExample28.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample28 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime localTime = now.withMinuteOfHour(30);

        System.out.println(now);
        System.out.println(localTime);
    }
}
The output is:
12:10:59.863
12:30:59.863

How to use LocalTime.withSecondOfMinute() method

JodaTimeLocalTimeExample29.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample29 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime localTime = now.withSecondOfMinute(50);

        System.out.println(now);
        System.out.println(localTime);
    }
}
The output is:
12:11:10.396
12:11:50.396

How to use LocalTime.withMillisOfSecond() method

JodaTimeLocalTimeExample30.java

import org.joda.time.LocalTime;

public class JodaTimeLocalTimeExample30 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        LocalTime localTime = now.withMillisOfSecond(500);

        System.out.println(now);
        System.out.println(localTime);
    }
}
The output is:
12:11:22.284
12:11:22.500

Convert LocalTime to DateTime

In the following Java program we use the toDateTimeToday() method to convert a LocalTIme object into DateTime object.

JodaTimeLocalTimeExample31.java

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

public class JodaTimeLocalTimeExample31 {
    public static void main(String[] args) {
        DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
        LocalTime now = new LocalTime();

        DateTime today = now.toDateTimeToday();
        DateTime todayAtLondon = now.toDateTimeToday(dateTimeZone);

        System.out.println(now);
        System.out.println(today);
        System.out.println(todayAtLondon);
    }
}
The output is:
12:11:40.308
2021-01-16T12:11:40.308+07:00
2021-01-16T12:11:40.308Z

Convert LocalTime to String

To convert a LocalTime object to a String value we can use the toString() method as following Java program.

JodaTimeLocalTimeExample32.java

import org.joda.time.LocalTime;

import java.util.Locale;

public class JodaTimeLocalTimeExample32 {
    public static void main(String[] args) {
        LocalTime now = new LocalTime();

        String time1 = now.toString();
        String time2 = now.toString("HH:mm:ss");
        String time3 = now.toString("HH:mm:ss", Locale.JAPANESE);

        System.out.println(now);
        System.out.println(time1);
        System.out.println(time2);
        System.out.println(time3);
    }
}
The output is:
12:11:59.167
12:11:59.167
12:11:59
12:11:59

Happy Coding 😊

Joda Time Instant by Examples in Java

Joda Time LocalDate by Examples in Java

Joda Time LocalDateTime by Examples in Java

Joda Time DateTime by Examples in Java

Joda Time Duration by Examples in Java

Joda Time Period by Examples in Java

Joda Time Interval by Examples in Java