Get Date and Time fields value of Joda-Time DateTime object in Java
Tags: Joda Time Joda DateTime Joda DateTimeFieldType
In this Joda-Time tutorial, we learn how to get specified date and time fields of a DateTime object such as year, month day, hour, minute, second, millisecond 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
Using getter methods
GetDateTimeExample1.java
import org.joda.time.DateTime;
public class GetDateTimeExample1 {
public static void main(String[] args) {
DateTime dateTime = DateTime.now();
int year = dateTime.getYear();
int months = dateTime.getMonthOfYear();
int days = dateTime.getDayOfMonth();
int hours = dateTime.getHourOfDay();
int minutes = dateTime.getMinuteOfHour();
int seconds = dateTime.getSecondOfMinute();
int milliseconds = dateTime.getMillisOfSecond();
System.out.println("Year: " + year);
System.out.println("Months: " + months);
System.out.println("Days: " + days);
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
System.out.println("Seconds: " + seconds);
System.out.println("Milliseconds: " + milliseconds);
}
}
Year: 2021
Months: 1
Days: 21
Hours: 1
Minutes: 1
Seconds: 13
Milliseconds: 258
Using get method with DateTimeFieldType
GetDateTimeExample2.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;
public class GetDateTimeExample2 {
public static void main(String[] args) {
DateTime dateTime = DateTime.now();
int year = dateTime.get(DateTimeFieldType.year());
int months = dateTime.get(DateTimeFieldType.monthOfYear());
int days = dateTime.get(DateTimeFieldType.dayOfMonth());
int hours = dateTime.get(DateTimeFieldType.hourOfDay());
int minutes = dateTime.get(DateTimeFieldType.minuteOfHour());
int seconds = dateTime.get(DateTimeFieldType.secondOfMinute());
int milliseconds = dateTime.get(DateTimeFieldType.millisOfSecond());
System.out.println("Year: " + year);
System.out.println("Months: " + months);
System.out.println("Days: " + days);
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
System.out.println("Seconds: " + seconds);
System.out.println("Milliseconds: " + milliseconds);
}
}
Year: 2021
Months: 1
Days: 21
Hours: 1
Minutes: 1
Seconds: 26
Milliseconds: 906
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 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