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
1
vote
1 answer

In JDO/DataNucleus can I query for a Joda-Time DateTime object?

I see that DataNucleus supports org.joda.DateTime http://www.datanucleus.org/products/accessplatform_2_2/jdo/types.html But does this mean I can index this field and perform queries against this time field, such as "where jodaTimeField > yesterday"?…
David Parks
  • 30,789
  • 47
  • 185
  • 328
1
vote
1 answer

Converting java.lang.double to org.joda.time.Instant

I have looked around a lot. I am very new to Java, and I am trying to cast a Double into an Instant. The use case is for the Google Dataflow Java SDK. The Double is a UNIX timestamp from a file I read using TextIO. When I…
maininformer
  • 967
  • 2
  • 17
  • 31
1
vote
2 answers

Converting to Eastern Time Zone (North America)

I have this piece of code for a client in Eastern time zone: public DateTime getActivityDate() { return new DateTime(activityDate, DateTimeZone.UTC).minusHours(5); } The client was reporting it was working until the time change back in…
Mdjon26
  • 2,145
  • 4
  • 19
  • 29
1
vote
1 answer

Proper validation of time intervals within the same day

I have a method as follows: public void storeAppointment(int year, int monthOfYear, int dayOfMonth, int hourOfDayFrom, int minuteFrom, int hourOfDayUntil, int minuteUntil) { Calendar appointmentCalendar =…
Jim
  • 18,826
  • 34
  • 135
  • 254
1
vote
1 answer

Failure while parsing ZonedDateTime#toString() with Joda time

With Elasticsearch 1.x (1.7.5 depends on Joda time 1.6), I could use the custom date time format "yyyy-MM-dd'T'HH:mm:ss.SSSZZ[ZZZ]" to parse serialized strings obtained with ZonedDateTime#toString() (such as…
Emmanuel Guiton
  • 1,315
  • 13
  • 24
1
vote
2 answers

Joda DateTime array sort array by date time

I have an arraylist of Joda DateTimes like this: List nextRemindersArray = new…
Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89
1
vote
1 answer

Bug in jodatime Period?

How come this test I wrote fails in jodatime 1.6.2? Is it a bug? @Test public void testIfJodaTimePeriodsHandlesPeriodTypesOtherThanMinutesAndHours() { long twentyDaysInMillis = TimeUnit.MILLISECONDS.convert(20, TimeUnit.DAYS); Period twoWeeks =…
eirirlar
  • 814
  • 6
  • 24
1
vote
1 answer

cqengine compare by datetime

I'm trying to find a list of objects that are after a certain DateTime. In order to do so, I've created the following query: return foos.retrieve(QueryFactory.equal(EXPIRY_INDEX, new DateTime())); I then created the following index: public static…
Hooli
  • 1,135
  • 3
  • 19
  • 46
1
vote
2 answers

If I try to compare 2 joda-time DateTime values in Scala I get java.lang.NoClassDefFoundError. How to fix this?

Here's an example: import org.scala_tools.time.Imports._ ... val dt1 : DateTime = new DateTime ("2010-09-01T12:00Z") val dt2 : DateTime = new DateTime ("2010-10-01T12:10Z") println (dt1 < dt2) // This is the Main.scala:48 line mentioned in the…
Ivan
  • 63,011
  • 101
  • 250
  • 382
1
vote
2 answers

How to generate Path with DateTimeFormat for pattern yyyy/mm/dd/HH

I am using spark/scala to load files from s3. My files are located under : s3://bucket/yyyy/mm/dd/HH/parts...files I need to generate the file paths with startDate(string) and endDate(string) import org.joda.time.{DateTime, DateTimeZone} import…
Newbie
  • 691
  • 4
  • 16
  • 28
1
vote
3 answers

Remove duplicates from list of DateTime objects only by Date component

What is preferred way to remove duplicates from list Joda DateTime objects but only by date component (no time component ) var dates = List[DateTime]() dates = dates ::: List(new DateTime(2015, 1, 1, 0, 0, 0, 0)) dates = dates ::: List(new…
Levijatanu
  • 371
  • 2
  • 17
1
vote
2 answers

How to Round Up Years in Joda-Time

Is there an existing way in Joda-Time to round up years? Say you have a DateTime startDate = 10-18-2016 and an DateTime endDate = 12-18-2017. I would like that to round up to 2 years. Currently, when I do: Years yearsBetween =…
This 0ne Pr0grammer
  • 2,632
  • 14
  • 57
  • 81
1
vote
1 answer

Handle Java daylight saving based on different time zone from UTC (joda Interval)

I need to return List intervals based on the input I get from users. everything works fine unless the range includes daylight saving. If that happens the response I get is kinda messed up after that specific day (in this example Nov 1st…
Am1rr3zA
  • 7,115
  • 18
  • 83
  • 125
1
vote
0 answers

Why can't the Compiler locate external libraries in my Java program?

I've created a Java program which uses the JodaTime library, and I'm trying to compile the program within the Mac console, but when I do I get the following error: error: package org.joda.time does not exist I tried setting the class path in my…
user6004535
1
vote
1 answer

SPARK : How to generate s3 file path dynamically using date diff

I am trying to get the list of files between startDate and endDate and read the files from these folders : For example my file structure looks like this: BucketName/year/month/day/files …
Newbie
  • 691
  • 4
  • 16
  • 28