Questions tagged [jodatime]

Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API.

Joda-Time is a library which can be used as a replacement for the Java date and time classes. From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310).

Sample Code

This sample is provided from code written as part of an interval type enum which could calculate a series of dates at that interval. It provides a great example of what it looks like to call some common methods on Joda's DateTime object.

public DateTime getNextDate(DateTime dt, int interval) {
    switch(this) {
    case SECOND:
        return dt.plusSeconds(interval);
    case MINUTE:
        return dt.plusMinutes(interval);
    case HOUR:
        return dt.plusHours(interval);
    case DAY:
        return dt.plusDays(interval);
    case WEEK:
        dt = dt.plusDays(3).plusWeeks(interval - 1);
        return dt.withDayOfWeek(6);
    case MONTH:
        dt = dt.plusDays(3).plusMonths(interval - 1);
        return dt.withDayOfMonth(dt.dayOfMonth().getMaximumValue());
    case QUARTER:
        dt = dt.plusDays(3).plusMonths((interval - 1) * 3);
        dt = dt.plusMonths((3 - (dt.getMonthOfYear() % 3)) % 3);
        return dt.withDayOfMonth(dt.dayOfMonth().getMaximumValue());
    case YEAR:
        dt = dt.plusDays(3).plusYears(interval - 1);
        return dt.withDayOfYear(dt.dayOfYear().getMaximumValue());
    }
    return dt;
}

Some helpful SO posts

Useful Links

2931 questions
44
votes
7 answers

First day of next month with java Joda-Time

How would you rewrite the method below, which returns the first day of next month, with the org.joda.time package in Joda-Time? public static Date firstDayOfNextMonth() { Calendar nowCal = Calendar.getInstance(); int month =…
MatBanik
  • 26,356
  • 39
  • 116
  • 178
44
votes
5 answers

Formatting a Duration in Java 8 / jsr310

I am transitioning a project from Joda-Time to java8's native time libraries, and I have run into a snag. I have been unable to find a formatter for Duration. I would like to have a custom String format of, for instance, HHH+MM, where a Duration of…
Chad Lowe
  • 721
  • 1
  • 6
  • 12
43
votes
1 answer

Equivalent to jodatime Interval in Java 8 Date and Time API

The Java 8 way of dealing with time seems to have improved so much that I start to think about replacing jodatime in some cases. But what I am missing is an equivalent to the Interval class. I haven't dug too deep into Java 8 yet, so I might have…
nansen
  • 2,912
  • 1
  • 20
  • 33
42
votes
4 answers

Persist Joda-time's DateTime via Hibernate

I'm using Jodatime in my Play app, but currently having to do a bunch of converting back and forth from/to java.util.Date and java.sql.Time. Since jodatime is included in the Play distribution, I'm thinking there's probably a better way to do this.…
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
42
votes
6 answers

Recommended use for Joda-Time's DateMidnight

The javdoc for LocalDate#toDateMidnight reads as follows: As from v1.5, you are recommended to avoid DateMidnight and use toDateTimeAtStartOfDay() instead because of the exception detailed below. This method will throw an exception if the…
oby1
  • 1,302
  • 1
  • 13
  • 20
42
votes
4 answers

Joda DateTime to Timestamp conversion

I am trying to change the value of Timestamp by DateTimeZone in Joda : DateTime dt = new DateTime(rs.getTimestamp("anytimestampcolumn"), DateTimeZone.forID("anytimezone")); Timestamp ts = new…
oko
  • 1,295
  • 1
  • 14
  • 30
42
votes
5 answers

How to subtract two joda datetimes?

I know this isn't "the way it's supposed to work", but still: If you have two DateTime objects, what's a good way to subtract them? Convert them to Date objects? DateTime start = new DateTime(); System.out.println(start + " - doing some stuff"); //…
ripper234
  • 222,824
  • 274
  • 634
  • 905
41
votes
5 answers

Get first day of a particular week in Joda-Time? java

In Joda-Time, is there a way to get the date of the first day of the week(monday). for instance i want to find out what date was this weeks monday based on todays current date 21/01/11 Cheers in advance. edit: i also wish to find the date for the…
Jono
  • 17,341
  • 48
  • 135
  • 217
39
votes
4 answers

Jackson automatic formatting of Joda DateTime to ISO 8601 format

According to http://wiki.fasterxml.com/JacksonFAQDateHandling, “DateTime can be automatically serialized/deserialized similar to how java.util.Date is handled.” However, I am not able to accomplish this automatic functionality. There are…
Jeff M
  • 1,055
  • 1
  • 10
  • 22
38
votes
6 answers

Parse Date String to Some Java Object

I am working in a project that reads files and processes data. There I got to work with dates for example: 2012-01-10 23:13:26 January 13, 2012 I found the package Joda, kinda interesting package but don't know if it is the easiest around. I was…
Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71
38
votes
6 answers

Is there a standard implementation for a GSON Joda Time serialiser?

I'm using GSON to serialise some object graphs to JSON. These objects graphs use Joda Time entities (DateTime, LocalTime etc). The top Google hit for "gson joda" is this…
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
37
votes
4 answers

ISO 8601 Time Interval Parsing in Java

ISO 8601 defines a syntax for representing a time interval. There are four ways to express a time interval: Start and end, such as "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z" Start and duration, such as…
fellahst
  • 641
  • 1
  • 7
  • 16
37
votes
2 answers

How to get properly current date and time in Joda-Time?

How to get properly actual date and time in Joda Time? By properly I mean time in my country. I read official pages and some tutorials - there are many things about Locales and timezones but I found it quite confusing. I did't found any example…
user1097772
  • 3,499
  • 15
  • 59
  • 95
36
votes
3 answers

Joda DateTime to Unix DateTime

It's odd enough, but I didn't find any result about converting Joda(Time) DateTime to Unix DateTime (or timestamp, whichever is the correct name). How can I do this?
Incerteza
  • 32,326
  • 47
  • 154
  • 261
36
votes
2 answers

Comparing two Joda-Time DateTime objects

I am dealing with something very similar to what has been asked here - compare Joda-Time time zones but it does not seem to work for me. Here's a piece of code which I am testing Joda-Time DateTime with - DateTime estDT = new…
FSP
  • 4,677
  • 2
  • 19
  • 19