How do I get date / time fields of date in Joda?
Category: org.joda.time, viewed: 911 time(s).
In Joda 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.example.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("DOY = " + dateTime.getDayOfYear());
System.out.println("DOM = " + dateTime.getDayOfMonth());
System.out.println("DOW = " + dateTime.getDayOfWeek());
System.out.println("WOW = " + dateTime.getWeekOfWeekyear());
//
// Get hour of day, minute of hour and second of minute.
//
System.out.println("HOD = " + dateTime.getHourOfDay());
System.out.println("MOH = " + dateTime.getMinuteOfHour());
System.out.println("SOM = " + dateTime.getSecondOfMinute());
//
// Get minute of day and second of day.
//
System.out.println("MOD = " + dateTime.getMinuteOfDay());
System.out.println("SOD = " + dateTime.getSecondOfDay());
}
}
The output of our program are:
dateTime = 2012-02-03T23:06:35.481+08:00
DOY = 34
DOM = 3
DOW = 5
WOW = 5
HOD = 23
MOH = 6
SOM = 35
MOD = 1386
SOD = 83195
More examples on org.joda.time