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
81
votes
19 answers

Difference in days between two dates in Java?

I need to find the number of days between two dates: one is from a report and one is the current date. My snippet: int age=calculateDifference(agingDate, today); Here calculateDifference is a private method, agingDate and today are Date objects,…
Venkat
  • 2,604
  • 6
  • 26
  • 36
79
votes
3 answers

Using Joda Date & Time API to parse multiple formats

I'm parsing third party log files containing date/time using Joda. The date/time is in one of two different formats, depending on the age of the log files I'm parsing. Currently I have code like this: try { return…
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
78
votes
3 answers

How to convert Joda-Time DateTime to java.util.Date and vice versa?

Is it possible to do that? If yes, then how do I do the conversion from Joda-Time to Date and vice versa?
Time
  • 1,551
  • 2
  • 13
  • 14
77
votes
9 answers

Time: How to get the next friday?

How can I get the next friday with the Joda-Time API. The LocalDate of today is today. It looks to me you have to decide whever you are before or after the friday of the current week. See this method: private LocalDate calcNextFriday(LocalDate d) { …
michael.kebe
  • 10,916
  • 3
  • 44
  • 62
75
votes
4 answers

Is there a class in java.time comparable to the Joda-Time Interval?

I'm evaluating to migrate my project from the usage of Joda-Time to the java.time package in Java 8. In Joda-Time, I heavily used the Interval class. I could not find anything like this in java.time. Is there a comparable class?
dajood
  • 3,758
  • 9
  • 46
  • 68
75
votes
14 answers

How to get the current date and time of your timezone in Java?

I have my app hosted in a London Server. I am in Madrid, Spain. So the timezone is -2 hours. How can I obtain the current date / time with my time zone. Date curr_date = new Date(System.currentTimeMillis()); e.g. Date curr_date = new…
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179
72
votes
6 answers

How to calculate elapsed time from now with Joda-Time?

I need to calculate the time elapsed from one specific date till now and display it with the same format as StackOverflow questions, i.e.: 15s ago 2min ago 2hours ago 2days ago 25th Dec 08 Do you know how to achieve it with the Java Joda-Time…
fabien7474
  • 16,300
  • 22
  • 96
  • 124
66
votes
2 answers

How to use Joda-Time with java.sql.Timestamp

I Have a prepared statement INSERT INTO mst(time) VALUES (?); where time is of type Timestamp in a PostgreSQL database. I am inserting a Joda-Time DateTime object, or I should say I am trying to. I can find no way to convert the DateTime object…
WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61
65
votes
5 answers

How to convert Joda LocalDate to java.util.Date?

What is the simplest way to convert a JodaTime LocalDate to java.util.Date object?
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
64
votes
3 answers

Is Joda-Time DateTimeFormatter class thread safe?

Is the Joda-Time DateTimeFormatter class thread safe? Once I get an instance from DateTimeFormat.forPattern, can its various parse methods be called by multiple threads? DateTimeFormatter's Javadocs makes no mention of thread safety.
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
63
votes
2 answers

hibernate 4 and joda-time

are they happily married ? I am using the latest version of hibernate (4) and version 1.3 of joda-time hibernate support, which I also believe to be the current latest release. Everything seems to be working OK (date columns created as expected)…
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
59
votes
8 answers

Start of week for locale using Joda-Time

How do you determine which day of the week is considered the “start” according to a given Locale using Joda-Time? Point: Most countries use the international standard Monday as first day of week (!). A bunch others use Sunday (notably USA). Others…
stolsvik
  • 5,253
  • 7
  • 43
  • 52
58
votes
4 answers

How do I obtain the number of days within a given month using Joda-Time?

30 days hath September, April, June and November, All the rest have 31, Excepting February alone (And that has 28 days clear, With 29 in each leap year). Can I obtain this info anagrammatically? (I don't mean the poem, of course)
Manuel Araoz
  • 15,962
  • 24
  • 71
  • 95
58
votes
7 answers

Are there any cons to using Joda-Time?

I want to convince the architecture manager to include the Joda-Time jar in our product. Do you know any disadvantages in using it? I think Joda-Time needs to be constantly updated because of the files that it includes. And that is a disadvantage.…
user2427
  • 7,842
  • 19
  • 61
  • 71
58
votes
7 answers

In joda time how to convert time zone without changing time

I am getting UTC timestamp from database which is I am setting into a JodaTime DateTime instance DateTime dt = new DateTime(timestamp.getTime()); It stores the time perfectly say 10:00 AM but with local time zone. E.g, I am in IST time zone which…
Abhinav
  • 3,322
  • 9
  • 47
  • 63