Java examples on org.joda.time
- How do I get number of days between two dates in Joda?
- How do I use Joda-Time's DateMidnight class?
- How do I get number of months between two dates in Joda?
- How do I format date information in Joda?
- How do I get the last day of a month in Joda?
- How do I get date / time fields of date in Joda?
- How do I create DateTime object in Joda Time?
- How do I format date in Joda using ISODateTimeFormat class?
- How do I do a date calculations?
- How do I convert DateTime into Calendar or Date object?
- How do I use the Interval class of Joda Time?
- How do I get the first day of the next month in Joda?
- How to add hours, minutes, seconds into DateTime in Joda Time?
- How do I use Joda Time Instant Class?
How do I get date / time fields of date in Joda?
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