In Joda-Time the date and time information are divided into fields. The following example show you some fields that can be obtained from the DateTime
object. For example to get the day of the year we use the getDayOfYear()
method and to get the day of week we can use the getDayOfWeek()
method.
package org.kodejava.joda;
import org.joda.time.DateTime;
public class DateTimeFieldDemo {
public static void main(String[] args) {
DateTime dateTime = new DateTime();
System.out.println("DateTime = " + dateTime);
// Get day of year, day of month, day of week and week of
// year of a date.
System.out.println("Day of Year = " + dateTime.getDayOfYear());
System.out.println("Day of Month = " + dateTime.getDayOfMonth());
System.out.println("Day of Week = " + dateTime.getDayOfWeek());
System.out.println("Week of Week year = " + dateTime.getWeekOfWeekyear());
// Get hour of day, minute of hour and second of minute.
System.out.println("Hour of Day = " + dateTime.getHourOfDay());
System.out.println("Minute of Hour = " + dateTime.getMinuteOfHour());
System.out.println("Second of Minute = " + dateTime.getSecondOfMinute());
// Get minute of day and second of day.
System.out.println("Minute of Day = " + dateTime.getMinuteOfDay());
System.out.println("Second of Day = " + dateTime.getSecondOfDay());
}
}
The outputs of our program are:
DateTime = 2021-10-29T06:46:49.259+08:00
Day of Year = 302
Day of Month = 29
Day of Week = 5
Week of Week year = 43
Hour of Day = 6
Minute of Hour = 46
Second of Minute = 49
Minute of Day = 406
Second of Day = 24409
Maven Dependencies
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.5</version>
</dependency>
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023