Joda Time LocalDateTime by Examples in Java
Tags: Joda Time LocalDateTime Duration Period DurationFieldType DateTimeFieldType Locale LocalDate Date LocalTime Calendar Chronology DateTimeZone ISOChronology Instant Joda LocalDateTime Joda Duration Joda Period Joda DurationFieldType Joda DateTimeFieldType Joda LocalDate Joda LocalTime Joda Chronology Joda DateTimeZone Joda ISOChronology Joda Instant
In this Java tutorial, we learn how to use the org.joda.time.LocalDateTime 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 LocalDateTime object using constructors
The following Java program we show how to use the default LocalDateTime constructor to create a LocalDateTime object that represents the current date and time.
JodaTimeLocalDateTimeExample1.java
import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.chrono.ISOChronology;
public class JodaTimeLocalDateTimeExample1 {
public static void main(String[] args) {
DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
Chronology chronology = ISOChronology.getInstance();
LocalDateTime localDateTime1 = new LocalDateTime();
LocalDateTime localDateTime2 = new LocalDateTime(dateTimeZone);
LocalDateTime localDateTime3 = new LocalDateTime(chronology);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
}
}
2021-01-16T18:15:17.127
2021-01-16T11:15:17.127
2021-01-16T18:15:17.128
We also can create new LocalDateTime objects with a given milliseconds value.
JodaTimeLocalDateTimeExample2.java
import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.chrono.ISOChronology;
public class JodaTimeLocalDateTimeExample2 {
public static void main(String[] args) {
DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
Chronology chronology = ISOChronology.getInstance();
int milliseconds = 60000;
LocalDateTime localDateTime1 = new LocalDateTime(milliseconds);
LocalDateTime localDateTime2 = new LocalDateTime(milliseconds, dateTimeZone);
LocalDateTime localDateTime3 = new LocalDateTime(milliseconds, chronology);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
}
}
1970-01-01T07:01:00.000
1970-01-01T01:01:00.000
1970-01-01T07:01:00.000
Or from a given Instant object as following Java program.
JodaTimeLocalDateTimeExample3.java
import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.Instant;
import org.joda.time.LocalDateTime;
import org.joda.time.chrono.ISOChronology;
public class JodaTimeLocalDateTimeExample3 {
public static void main(String[] args) {
DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
Chronology chronology = ISOChronology.getInstance();
Instant instant = Instant.now();
LocalDateTime localDateTime1 = new LocalDateTime(instant);
LocalDateTime localDateTime2 = new LocalDateTime(instant, dateTimeZone);
LocalDateTime localDateTime3 = new LocalDateTime(instant, chronology);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
}
}
2021-01-16T11:15:48.649
2021-01-16T11:15:48.649
2021-01-16T18:15:48.649
Below constructors to create LocalDateTime objects with specific field values.
JodaTimeLocalDateTimeExample4.java
import org.joda.time.Chronology;
import org.joda.time.LocalDateTime;
import org.joda.time.chrono.ISOChronology;
public class JodaTimeLocalDateTimeExample4 {
public static void main(String[] args) {
Chronology chronology = ISOChronology.getInstance();
int year = 2021;
int monthOfYear = 1;
int dayOfMonth = 10;
int hourOfDay = 5;
int minuteOfHour = 10;
int secondOfMinute = 30;
int millisOfSecond = 500;
LocalDateTime localDateTime1 = new LocalDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour);
LocalDateTime localDateTime2 = new LocalDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute);
LocalDateTime localDateTime3 = new LocalDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
LocalDateTime localDateTime4 = new LocalDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, chronology);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
System.out.println(localDateTime4);
}
}
2021-01-10T05:10:00.000
2021-01-10T05:10:30.000
2021-01-10T05:10:30.500
2021-01-10T05:10:30.500
How to use LocalDateTime.now()
JodaTimeLocalDateTimeExample5.java
import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.chrono.ISOChronology;
public class JodaTimeLocalDateTimeExample5 {
public static void main(String[] args) {
DateTimeZone dateTimeZone = DateTimeZone.forID("Europe/London");
Chronology chronology = ISOChronology.getInstance();
LocalDateTime localDateTime1 = LocalDateTime.now();
LocalDateTime localDateTime2 = LocalDateTime.now(dateTimeZone);
LocalDateTime localDateTime3 = LocalDateTime.now(chronology);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
}
}
2021-01-16T18:16:19.320
2021-01-16T11:16:19.320
2021-01-16T18:16:19.321
Parse a String to LocalDateTime object
JodaTimeLocalDateTimeExample6.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample6 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.parse("2021-01-01T01:20:30.400");
System.out.println(localDateTime);
}
}
2021-01-01T01:20:30.400
Convert Calendar to LocalDateTime
The following Java program, we use LocalDateTime.fromCalendarFields() static method to create a new LocalDateTime object from a given Calendar object.
JodaTimeLocalDateTimeExample7.java
import org.joda.time.LocalDateTime;
import java.util.Calendar;
public class JodaTimeLocalDateTimeExample7 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
LocalDateTime localDateTime = LocalDateTime.fromCalendarFields(calendar);
System.out.println(localDateTime);
}
}
2021-01-16T18:16:44.080
Convert Date to LocalDateTime
In the following Java program we use LocalDateTime.fromDateFields() static method to create a new LocalDateTime object from a given Date object.
JodaTimeLocalDateTimeExample8.java
import org.joda.time.LocalDateTime;
import java.util.Date;
public class JodaTimeLocalDateTimeExample8 {
public static void main(String[] args) {
Date date = new Date();
LocalDateTime localDateTime = LocalDateTime.fromDateFields(date);
System.out.println(localDateTime);
}
}
2021-01-16T18:16:54.070
Convert LocalDateTime to DateTime
To convert LocalDateTime to a DateTime object we can use the toDateTime() method as following Java program.
JodaTimeLocalDateTimeExample9.java
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample9 {
public static void main(String[] args) {
LocalDateTime localDateTime = new LocalDateTime();
DateTime dateTime = localDateTime.toDateTime();
System.out.println(localDateTime);
System.out.println(dateTime);
}
}
2021-01-16T18:17:09.228
2021-01-16T18:17:09.228+07:00
Convert LocalDateTime to LocalTime
To convert LocalDateTime to a LocalTime object we can use the toLocalTime() method as following example Java program. JodaTimeLocalDateTimeExample10.java
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
public class JodaTimeLocalDateTimeExample10 {
public static void main(String[] args) {
LocalDateTime localDateTime = new LocalDateTime();
LocalTime localTime = localDateTime.toLocalTime();
System.out.println(localDateTime);
System.out.println(localTime);
}
}
2021-01-16T18:17:24.235
18:17:24.235
Convert LocalDateTime to Date
To convert LocalDateTime to a Date object we can use the toDate() method as following example Java program.
JodaTimeLocalDateTimeExample11.java
import org.joda.time.LocalDateTime;
import java.util.Date;
public class JodaTimeLocalDateTimeExample11 {
public static void main(String[] args) {
LocalDateTime localDateTime = new LocalDateTime();
Date date = localDateTime.toDate();
System.out.println(localDateTime);
System.out.println(date);
}
}
2021-01-16T18:17:37.097
Sat Jan 16 18:17:37 ICT 2021
Convert LocalDateTime to LocalDate
To convert LocalDateTime to a LocalDate object we can use the toLocalDate() method as following example Java program.
JodaTimeLocalDateTimeExample12.java
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample12 {
public static void main(String[] args) {
LocalDateTime localDateTime = new LocalDateTime();
LocalDate localDate = localDateTime.toLocalDate();
System.out.println(localDateTime);
System.out.println(localDate);
}
}
2021-01-16T18:17:52.019
2021-01-16
Convert LocalDateTime to String using toString() method
JodaTimeLocalDateTimeExample13.java
import org.joda.time.LocalDateTime;
import java.util.Locale;
public class JodaTimeLocalDateTimeExample13 {
public static void main(String[] args) {
LocalDateTime localDateTime = new LocalDateTime();
String str1 = localDateTime.toString();
String str2 = localDateTime.toString("dd/mm/YY HH:mm");
String str3 = localDateTime.toString("dd MMM YY HH:mm", Locale.FRANCE);
System.out.println(localDateTime);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
2021-01-16T18:18:05.226
2021-01-16T18:18:05.226
16/18/21 18:18
16 janv. 21 18:18
How to get specific field of a LocalDateTime object
The following Java example program uses the get() method to get the field value of LocalDateTime object.
JodaTimeLocalDateTimeExample14.java
import org.joda.time.DateTimeFieldType;
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample14 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
int year = now.get(DateTimeFieldType.year());
int monthOfYear = now.get(DateTimeFieldType.monthOfYear());
int dayOfMonth = now.get(DateTimeFieldType.dayOfMonth());
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(year);
System.out.println(monthOfYear);
System.out.println(dayOfMonth);
System.out.println(hourOfDay);
System.out.println(minuteOfHour);
System.out.println(secondOfMinute);
System.out.println(millisOfSecond);
}
}
2021
1
16
18
18
45
815
JodaTimeLocalDateTimeExample15.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample15 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int monthOfYear = now.getMonthOfYear();
int dayOfMonth = now.getDayOfMonth();
int hourOfDay = now.getHourOfDay();
int minuteOfHour = now.getMinuteOfHour();
int secondOfMinute = now.getSecondOfMinute();
int millisOfSecond = now.getMillisOfSecond();
System.out.println(year);
System.out.println(monthOfYear);
System.out.println(dayOfMonth);
System.out.println(hourOfDay);
System.out.println(minuteOfHour);
System.out.println(secondOfMinute);
System.out.println(millisOfSecond);
}
}
2021
1
16
18
19
0
44
How to compare LocalDateTime values
Using compareTo() method.
JodaTimeLocalDateTimeExample16.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample16 {
public static void main(String[] args) {
LocalDateTime localDateTime1 = new LocalDateTime(2021, 1, 1, 1, 1, 1, 1);
LocalDateTime localDateTime2 = new LocalDateTime(2021, 2, 2, 2, 2, 2, 2);
LocalDateTime localDateTime3 = new LocalDateTime(2021, 1, 1, 1, 1, 1, 1);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
System.out.println("localDateTime1 compare to localDateTime2: " + localDateTime1.compareTo(localDateTime2));
System.out.println("localDateTime1 compare to localDateTime3: " + localDateTime1.compareTo(localDateTime3));
System.out.println("localDateTime2 compare to localDateTime1: " + localDateTime2.compareTo(localDateTime1));
System.out.println("localDateTime3 compare to localDateTime1: " + localDateTime3.compareTo(localDateTime1));
}
}
2021-01-01T01:01:01.001
2021-02-02T02:02:02.002
2021-01-01T01:01:01.001
localDateTime1 compare to localDateTime2: -1
localDateTime1 compare to localDateTime3: 0
localDateTime2 compare to localDateTime1: 1
localDateTime3 compare to localDateTime1: 0
Using equals() method. JodaTimeLocalDateTimeExample17.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample17 {
public static void main(String[] args) {
LocalDateTime localDateTime1 = new LocalDateTime(2021, 1, 1, 1, 1, 1, 1);
LocalDateTime localDateTime2 = new LocalDateTime(2021, 2, 2, 2, 2, 2, 2);
LocalDateTime localDateTime3 = new LocalDateTime(2021, 1, 1, 1, 1, 1, 1);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
System.out.println("localDateTime1 equals localDateTime2: " + localDateTime1.equals(localDateTime2));
System.out.println("localDateTime1 equals localDateTime3: " + localDateTime1.equals(localDateTime3));
}
}
2021-01-01T01:01:01.001
2021-02-02T02:02:02.002
2021-01-01T01:01:01.001
localDateTime1 equals localDateTime2: false
localDateTime1 equals localDateTime3: true
How to use LocalDateTime.withDate() method
JodaTimeLocalDateTimeExample18.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample18 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withDate(1999, 1, 2);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:20:57.780
1999-01-02T18:20:57.780
How to use LocalDateTime.withTime() method
JodaTimeLocalDateTimeExample19.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample19 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withTime(10, 20, 30, 400);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:21:10.551
2021-01-16T10:20:30.400
How to use LocalDateTime.withField() method
JodaTimeLocalDateTimeExample20.java
import org.joda.time.DateTimeFieldType;
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample20 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime1 = now.withField(DateTimeFieldType.year(), 1999);
LocalDateTime localDateTime2 = now.withField(DateTimeFieldType.monthOfYear(), 1);
LocalDateTime localDateTime3 = now.withField(DateTimeFieldType.dayOfMonth(), 1);
LocalDateTime localDateTime4 = now.withField(DateTimeFieldType.hourOfDay(), 10);
LocalDateTime localDateTime5 = now.withField(DateTimeFieldType.minuteOfHour(), 20);
LocalDateTime localDateTime6 = now.withField(DateTimeFieldType.secondOfMinute(), 30);
LocalDateTime localDateTime7 = now.withField(DateTimeFieldType.millisOfSecond(), 500);
System.out.println(now);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
System.out.println(localDateTime4);
System.out.println(localDateTime5);
System.out.println(localDateTime6);
System.out.println(localDateTime7);
}
}
2021-01-16T18:21:23.715
1999-01-16T18:21:23.715
2021-01-16T18:21:23.715
2021-01-01T18:21:23.715
2021-01-16T10:21:23.715
2021-01-16T18:20:23.715
2021-01-16T18:21:30.715
2021-01-16T18:21:23.500
How to use LocalDateTime.withFieldAdded() method
JodaTimeLocalDateTimeExample21.java
import org.joda.time.DurationFieldType;
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample21 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime nextYear = now.withFieldAdded(DurationFieldType.years(), 1);
LocalDateTime nextMonth = now.withFieldAdded(DurationFieldType.months(), 1);
LocalDateTime tomorrow = now.withFieldAdded(DurationFieldType.days(), 1);
LocalDateTime nextTwoHours = now.withFieldAdded(DurationFieldType.hours(), 2);
LocalDateTime next30Minutes = now.withFieldAdded(DurationFieldType.minutes(), 30);
LocalDateTime next30Seconds = now.withFieldAdded(DurationFieldType.seconds(), 30);
LocalDateTime next500millis = now.withFieldAdded(DurationFieldType.millis(), 500);
System.out.println(now);
System.out.println(nextYear);
System.out.println(nextMonth);
System.out.println(tomorrow);
System.out.println(nextTwoHours);
System.out.println(next30Minutes);
System.out.println(next30Seconds);
System.out.println(next500millis);
}
}
2021-01-16T18:21:40.053
2022-01-16T18:21:40.053
2021-02-16T18:21:40.053
2021-01-17T18:21:40.053
2021-01-16T20:21:40.053
2021-01-16T18:51:40.053
2021-01-16T18:22:10.053
2021-01-16T18:21:40.553
How to use LocalDateTime.withDurationAdded() method
JodaTimeLocalDateTimeExample22.java
import org.joda.time.Duration;
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample22 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
Duration oneMinuteDuration = new Duration(60000); // 60000 milliseconds = 60 seconds = 1 minute
LocalDateTime next5Minutes = now.withDurationAdded(oneMinuteDuration, 5);
System.out.println(now);
System.out.println(next5Minutes);
}
}
2021-01-16T18:21:54.107
2021-01-16T18:26:54.107
How to use LocalDateTime.withPeriodAdded() method
JodaTimeLocalDateTimeExample23.java
import org.joda.time.LocalDateTime;
import org.joda.time.Period;
public class JodaTimeLocalDateTimeExample23 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
Period period = new Period(60000); // 60000 milliseconds = 60 seconds = 1 minute
LocalDateTime nextTwoMinutes = now.withPeriodAdded(period, 2);
System.out.println(now);
System.out.println(nextTwoMinutes);
}
}
2021-01-16T18:22:05.806
2021-01-16T18:24:05.806
How to use LocalDateTime.withYear() method
JodaTimeLocalDateTimeExample24.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample24 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withYear(1999);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:22:24.112
1999-01-16T18:22:24.112
How to use LocalDateTime.withWeekyear() method
JodaTimeLocalDateTimeExample25.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample25 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withWeekyear(5);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:22:35.388
0005-01-15T18:22:35.388
How to use LocalDateTime.withMonthOfYear() method
JodaTimeLocalDateTimeExample26.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample26 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime juneThisYear = now.withMonthOfYear(6);
System.out.println(now);
System.out.println(juneThisYear);
}
}
2021-01-16T18:22:45.250
2021-06-16T18:22:45.250
How to use LocalDateTime.withWeekOfWeekyear() method
JodaTimeLocalDateTimeExample27.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample27 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withWeekOfWeekyear(5);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:23:06.497
2021-02-06T18:23:06.497
How to use LocalDateTime.withDayOfYear() method
JodaTimeLocalDateTimeExample28.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample28 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withDayOfYear(10);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:23:20.669
2021-01-10T18:23:20.669
How to use LocalDateTime.withDayOfMonth() method
JodaTimeLocalDateTimeExample29.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample29 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withDayOfMonth(5);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:23:35.115
2021-01-05T18:23:35.115
How to use LocalDateTime.withDayOfWeek() method
JodaTimeLocalDateTimeExample30.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample30 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withDayOfWeek(1);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:23:47.614
2021-01-11T18:23:47.614
How to use LocalDateTime.withHourOfDay() method
JodaTimeLocalDateTimeExample31.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample31 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withHourOfDay(12);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:24:01.991
2021-01-16T12:24:01.991
How to use LocalDateTime.withMinuteOfHour() method
JodaTimeLocalDateTimeExample32.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample32 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withMinuteOfHour(59);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:24:13.789
2021-01-16T18:59:13.789
How to use LocalDateTime.withSecondOfMinute() method
JodaTimeLocalDateTimeExample33.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample33 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withSecondOfMinute(0);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:24:25.549
2021-01-16T18:24:00.549
How to use LocalDateTime.withMillisOfSecond() method
JodaTimeLocalDateTimeExample34.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample34 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withMillisOfSecond(999);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:24:37.880
2021-01-16T18:24:37.999
How to use LocalDateTime.withMillisOfDay() method
JodaTimeLocalDateTimeExample35.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample35 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.withMillisOfDay(100000);
System.out.println(now);
System.out.println(localDateTime);
}
}
2021-01-16T18:24:50.663
2021-01-16T00:01:40.000
How to use LocalDateTime.plus() method
JodaTimeLocalDateTimeExample36.java
import org.joda.time.Duration;
import org.joda.time.LocalDateTime;
import org.joda.time.Period;
public class JodaTimeLocalDateTimeExample36 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
Period oneMinutePeriod = new Period(60000); // 60000 milliseconds = 60 seconds = 1 minute
Duration twoMinutesDuration = new Duration(120000); // 120000 = 120 seconds = 2 minutes
LocalDateTime nextOneMinute = now.plus(oneMinutePeriod);
LocalDateTime nextTwoMinute = now.plus(twoMinutesDuration);
System.out.println(now);
System.out.println(nextOneMinute);
System.out.println(nextTwoMinute);
}
}
2021-01-16T18:25:05.541
2021-01-16T18:26:05.541
2021-01-16T18:27:05.541
How to use LocalDateTime.plusYears() method
JodaTimeLocalDateTimeExample37.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample37 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime nextYear = now.plusYears(1);
System.out.println(now);
System.out.println(nextYear);
}
}
2021-01-16T18:25:17.916
2022-01-16T18:25:17.916
How to use LocalDateTime.plusMonths() method
JodaTimeLocalDateTimeExample38.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample38 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime nextMonth = now.plusMonths(1);
System.out.println(now);
System.out.println(nextMonth);
}
}
2021-01-16T18:25:52.644
2021-02-16T18:25:52.644
How to use LocalDateTime.plusWeeks() method
JodaTimeLocalDateTimeExample39.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample39 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime nextWeek = now.plusWeeks(1);
System.out.println(now);
System.out.println(nextWeek);
}
}
2021-01-16T18:26:07.511
2021-01-23T18:26:07.511
How to use LocalDateTime.plusDays() method
JodaTimeLocalDateTimeExample40.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample40 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime next5Days = now.plusDays(15);
System.out.println(now);
System.out.println(next5Days);
}
}
2021-01-16T18:27:11.663
2021-01-31T18:27:11.663
How to use LocalDateTime.plusHours() method
JodaTimeLocalDateTimeExample41.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample41 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime nextTenHours = now.plusHours(10);
System.out.println(now);
System.out.println(nextTenHours);
}
}
2021-01-16T18:27:26.235
2021-01-17T04:27:26.235
How to use LocalDateTime.plusMinutes() method
JodaTimeLocalDateTimeExample42.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample42 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime next30Minutes = now.plusMinutes(30);
System.out.println(now);
System.out.println(next30Minutes);
}
}
2021-01-16T18:29:08.066
2021-01-16T18:59:08.066
How to use LocalDateTime.plusSeconds() method
JodaTimeLocalDateTimeExample43.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample43 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime next30Seconds = now.plusSeconds(30);
System.out.println(now);
System.out.println(next30Seconds);
}
}
2021-01-16T18:29:20.769
2021-01-16T18:29:50.769
How to use LocalDateTime.plusMillis() method
JodaTimeLocalDateTimeExample44.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample44 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime next1Second = now.plusMillis(1000);
System.out.println(now);
System.out.println(next1Second);
}
}
2021-01-16T18:29:32.943
2021-01-16T18:29:33.943
How to use LocalDateTime.minus() method
JodaTimeLocalDateTimeExample45.java
import org.joda.time.Duration;
import org.joda.time.LocalDateTime;
import org.joda.time.Period;
public class JodaTimeLocalDateTimeExample45 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
Period oneMinutePeriod = new Period(60000); // 60000 milliseconds = 60 seconds = 1 minute
Duration twoMinutesDuration = new Duration(120000); // 120000 milliseconds = 120 seconds = 2 minutes
LocalDateTime oneMinuteAgo = now.minus(oneMinutePeriod);
LocalDateTime twoMinutesAgo = now.minus(twoMinutesDuration);
System.out.println(now);
System.out.println(oneMinuteAgo);
System.out.println(twoMinutesAgo);
}
}
2021-01-16T18:29:44.055
2021-01-16T18:28:44.055
2021-01-16T18:27:44.055
How to use LocalDateTime.minusYears() method
JodaTimeLocalDateTimeExample46.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample46 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime tenYearsAgo = now.minusYears(10);
System.out.println(now);
System.out.println(tenYearsAgo);
}
}
2021-01-16T18:29:55.267
2011-01-16T18:29:55.267
How to use LocalDateTime.minusMonths() method
JodaTimeLocalDateTimeExample47.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample47 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime twoMonthsAgo = now.minusMonths(2);
System.out.println(now);
System.out.println(twoMonthsAgo);
}
}
2021-01-16T18:30:09.463
2020-11-16T18:30:09.463
How to use LocalDateTime.minusWeeks() method
JodaTimeLocalDateTimeExample48.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample48 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime oneWeekAgo = now.minusWeeks(1);
System.out.println(now);
System.out.println(oneWeekAgo);
}
}
2021-01-16T18:30:24.389
2021-01-09T18:30:24.389
How to use LocalDateTime.minusDays() method
JodaTimeLocalDateTimeExample49.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample49 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime yesterday = now.minusDays(1);
System.out.println(now);
System.out.println(yesterday);
}
}
2021-01-16T18:30:38.162
2021-01-15T18:30:38.162
How to use LocalDateTime.minusHours() method
JodaTimeLocalDateTimeExample50.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample50 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime tenHoursAgo = now.minusHours(10);
System.out.println(now);
System.out.println(tenHoursAgo);
}
}
2021-01-16T18:30:52.797
2021-01-16T08:30:52.797
How to use LocalDateTime.minusMinutes() method
JodaTimeLocalDateTimeExample51.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample51 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime fiveMinutesAgo = now.minusMinutes(5);
System.out.println(now);
System.out.println(fiveMinutesAgo);
}
}
2021-01-16T18:31:03.632
2021-01-16T18:26:03.632
How to use LocalDateTime.minusSeconds() method
JodaTimeLocalDateTimeExample52.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample52 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime threeSecondsAgo = now.minusSeconds(3);
System.out.println(now);
System.out.println(threeSecondsAgo);
}
}
2021-01-16T18:31:17.458
2021-01-16T18:31:14.458
How to use LocalDateTime.minusMillis() method
JodaTimeLocalDateTimeExample53.java
import org.joda.time.LocalDateTime;
public class JodaTimeLocalDateTimeExample53 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime oneSecondsAgo = now.minusMillis(1000); // 1000 milliseconds = 1 seconds
System.out.println(now);
System.out.println(oneSecondsAgo);
}
}
2021-01-16T18:31:30.283
2021-01-16T18:31:29.283
Happy Coding 😊
Related Articles
Joda Time Instant by Examples in Java
Joda Time LocalDate by Examples in Java
Joda Time LocalTime by Examples in Java
Joda Time DateTime by Examples in Java
Joda Time Duration by Examples in Java