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
<!-- https://search.maven.org/remotecontent?filepath=joda-time/joda-time/2.11.2/joda-time-2.11.2.jar -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.11.2</version>
</dependency>
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023