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
361
votes
9 answers

Number of days between two dates in Joda-Time

How do I find the difference in Days between two Joda-Time DateTime instances? With ‘difference in days’ I mean if start is on Monday and end is on Tuesday I expect a return value of 1 regardless of the hour/minute/seconds of the start and end…
pvgoddijn
  • 12,638
  • 15
  • 47
  • 56
300
votes
4 answers

Differences between Java 8 Date Time API (java.time) and Joda-Time

I know there are questions relating to java.util.Date and Joda-Time. But after some digging, I couldn't find a thread about the differences between the java.time API (new in Java 8, defined by JSR 310) and Joda-Time. I have heard that Java 8’s…
Zack
  • 5,108
  • 4
  • 27
  • 39
262
votes
2 answers

Convert from java.util.date to JodaTime

I want to convert a java.util.Date to JodaTime so as to carry out subtractions between dates. Is there a good concise way to convert from Date to JodaTime?
Krt_Malta
  • 9,265
  • 18
  • 53
  • 91
255
votes
10 answers

Converting a date string to a DateTime object using Joda Time library

I have a date as a string in the following format "04/02/2011 20:27:05". I am using Joda-Time library and would like to convert it to DateTime object. I did: DateTime dt = new DateTime("04/02/2011 20:27:05") But I'm getting the following error…
Tom
  • 2,561
  • 2
  • 15
  • 4
210
votes
10 answers

How to format Joda-Time DateTime to only mm/dd/yyyy?

I have a string "11/15/2013 08:00:00", I want to format it to "11/15/2013", what is the correct DateTimeFormatter pattern? I've tried many and googled and still unable to find the correct pattern. edit: I am looking for Joda-Time DateTimeFormatter,…
iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169
200
votes
2 answers

Joda-Time: what's the difference between Period, Interval and Duration?

In Joda-Time 2, what is the difference between the three kinds of time spans: Period Interval Duration Why do we need three classes? Which one performs better? Why is dividing a Period or Duration or Interval instance not implemented? E.g. p =…
Gerard
  • 13,023
  • 14
  • 72
  • 125
166
votes
5 answers

What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?

What's the standard way to work with dates and times in Scala? Should I use Java types such as java.util.Date or there are native Scala alternatives?
Ivan
  • 63,011
  • 101
  • 250
  • 382
156
votes
7 answers

Convert LocalDate to LocalDateTime or java.sql.Timestamp

I am using JodaTime 1.6.2. I have a LocalDate that I need to convert to either a (Joda) LocalDateTime, or a java.sqlTimestamp for ormapping. The reason for this is I have figured out how to convert between a LocalDateTime and a…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
153
votes
4 answers

How to find difference between two Joda-Time DateTimes in minutes

Below is the method I wrote: public List> loadNotYetInEmployee(int shift, Date date, int transitionVal, String type, User user) { DateTime datetime = new DateTime(date); datetime = datetime …
Deepak Kumar
  • 2,317
  • 5
  • 23
  • 21
147
votes
5 answers

Should I use Java date and time classes or go with a 3rd party library like Joda Time?

I'm creating a web based system which will be used in countries from all over the world. One type of data which must be stored is dates and times. What are the pros and cons of using the Java date and time classes compared to 3rd party libraries…
user14070
144
votes
5 answers

String to LocalDate

How can i convert a string to a LocalDate? I have seen examples like: LocalDate dt = new LocalDate("2005-11-12"); But my strings are like: 2005-nov-12
clankill3r
  • 9,146
  • 20
  • 70
  • 126
128
votes
10 answers

How to serialize Joda DateTime with Jackson JSON processor?

How do I get Jackson to serialize my Joda DateTime object according to a simple pattern (like "dd-MM-yyyy")? I've tried: @JsonSerialize(using=DateTimeSerializer.class) private final DateTime date; I've also tried: ObjectMapper mapper = new…
Haywood Jablomey
  • 1,255
  • 2
  • 9
  • 3
116
votes
4 answers

How to get the last date of a particular month with JodaTime?

I need to get the first date (as org.joda.time.LocalDate) of a month and the last one. Getting the first is trivial, but getting the last seems to need some logic as months have different length and February length even varies over years. Is there a…
Ivan
  • 63,011
  • 101
  • 250
  • 382
92
votes
2 answers

Class broken error with Joda Time using Scala

I'm adding the Joda Time repository to SBT with libraryDependencies ++= Seq( "joda-time" % "joda-time" % "2.1" ) Then I merrily use it like this: val ymd = org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd") …
Jack
  • 16,506
  • 19
  • 100
  • 167
88
votes
2 answers

Add one day into Joda-Time DateTime

I have date Wed May 08 00:00:00 GMT+06:30 2013. I add one day into it by using Joda-Time DateTime like this. DateTime dateTime = new DateTime(date); dateTime.plusDays(1); When I print dateTime, I got this date 2013-05-08T00:00:00.000+06:30. The…
user1156041
  • 2,155
  • 5
  • 24
  • 54
1
2 3
99 100