Introduction to Jodatime Basics

Source: Internet
Author: User
Tags datetime month name time and date

Joda-time makes time and date values easier to manage, manipulate, and understand. In fact, easy to use is the main design goal of Joda. Other goals include scalability, a complete set of features, and support for multiple calendar systems. And Joda is completely interoperable with JDK, so you don't have to replace all the Java code, just the part of the code that performs date/time calculations. maven Dependencies

<dependency>
  <groupId>joda-time</groupId>
  <artifactid>joda-time</artifactid >
  <version>2.8.2</version>
</dependency>
working with date and time classes

The following five date-time classes will be used frequently:

class name meaning Description
Instant Instant--immutable class, representing an instantaneous point on the timeline Use an event timestamp, no calendar system or time zone worry
Datetime datetime--immutable classes, replacing JDK calendars Time zone information is important as a generic alternative to the JDK calendar class
Localdate localdate--immutable classes, representing a region of dates without time (no timezone) Can be used to represent a birth date, it is not necessary to record a day's time information
LocalTime localtime--immutable class, representing a region with no date (no time zone) Can be used to represent a time of day, for example: When a store is turned on or off
LocalDateTime localdatetime--immutable class, representing the date and time of a region (no time zone)



Each date-time class provides a variety of constructors. These include constructors for objects that allow you to build an instance based on different objects. For example, a datetime can be constructed based on the following objects: Date-a JDK instant calendar-a JDK Calendar string-in ISO8601 format long-in millisecond s any joda-time Date-time class

Specific examples:

Use a date (or millissecond) java.util.Date judate = new Date ();
Long Timeinmillis = Judate.gettime ();
DateTime dt = new DateTime (Timeinmillis);

DT = new DateTime (judate);
Use a Calendar Java.util.Calendar C1 = calendar.getinstance ();
C1.set (calendar.hour_of_day, 0);
C1.set (calendar.minute, 0);
C1.set (Calendar.second, 0);
C1.set (Calendar.millisecond, 0);

DT = new DateTime (calendar);
Use another Joda datetime datetime anotherdatetime = Obtaindatetimesomehow ();

datetime = new DateTime (anotherdatetime);
Use a string (must is formatted property) string timestring = "2006-01-26t13:30:00-06:00";
datetime = new DateTime (timestring);
timestring = "2006-01-26";

datetime = new DateTime (timestring);
Inserts the result of the Joda calculation into the JDK object Calendar calendar = Calendar.getinstance ();
datetime datetime = new DateTime (2000, 1, 1, 0, 0, 0, 0); System.out.println (Datetime.dayofweek (). toString ("E mm/dd/yyyy HH:mm:ss.
SSS ");

Calendar.settime (Datetime.todate ()); Date date = Datetime.todate ();

It is convenient to see the JDK date and calendar objects from above, and to convert Joda-time's datetime classes to each other.

Each date-time class provides a simple and easy way to access its fields.

For example, to access the month and year, you can use:

datetime datetime = new DateTime (
    //year,
    1,    //month
    1,    /
    /day 0,    //hour (midnight is zero)
    0,    //minute
    0,    //second
    0     //milliseconds
);
int month = Dt.getmonthofyear ();  Where January is 1 and December are
int year = Dt.getyear ();

All major date-time classes are immutable, and when you manipulate the Joda class through an API method, you must catch the return value of the method because the instance you are working on cannot be modified. Just as java.lang.String cannot change after it is created. However, for a newly created object, we provide some simple ways to change its field values.

For example, to set a year or add 2 hours, you can use:

datetime dt = new DateTime ();
DateTime year2000 = Dt.withyear ();
DateTime Twohourslater = dt.plushours (2);

In addition to the basic get methods, each date-time class provides a method of taking each field by property 1 . These provide a rich joda-time functionality.

For example, visit one months or year details:

datetime dt = new DateTime ();
String MonthName = Dt.monthofyear (). Getastext (); Get month name
String frenchshortname =        
    dt.monthofyear (). Getasshorttext (Locale.french);//month short French name
Boolean Isleapyear = Dt.year (). Isleap ();  Judge whether it is a leap year

Reference Links: Official Guide & Joda Time API & World properties through other people's eyes, it corresponds to the properties of a Java object. The property is named based on the common structure represented, and it is used to access the structure for the purpose of accomplishing the calculation. Attributes are key to achieving Joda computing power. For example: Yearofcentury,dayofyear,monthofyear,dayofmonth,dayofweek. ↩

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.