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
36
votes
4 answers

Only some users reporting "Resource Not Found" error. Does this make sense?

I am seeing a a couple of errors coming up on Crittercism (Crash reporting service) for my published Android app. The trace is the following: 0 java.io.IOException: Resource not found: "org/joda/time/tz/data/ZoneInfoMap" ClassLoader:…
jvnbt
  • 2,465
  • 4
  • 20
  • 23
35
votes
9 answers

Comparing two dates using Joda time

I want to compare two dates, however I'm running into trouble. 1 date is created from a java.util.date object and the other is manually crafted. The following code is an example: Date ds = new Date(); DateTime d = new DateTime(ds); DateTime e = new…
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
34
votes
6 answers

In Joda-Time, set DateTime to start of month

My API allows library client to pass Date: method(java.util.Date date) Working with Joda-Time, from this date I would like to extract the month and iterate over all days this month contains. Now, the passed date is usually new Date() - meaning…
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
34
votes
4 answers

Joda Time parse a date with timezone and retain that timezone

I want to parse a date, which was created with a specific timezone, convert it to a format and return it. The conversion works but the timezone offset is always set to +0000 with the time difference being added/subtracted as necessary. How can I get…
edwardmlyte
  • 15,937
  • 23
  • 58
  • 83
34
votes
1 answer

How do I create a new Joda DateTime truncated to the last hour?

I am pulling timestamps from a file that I want to create a new DateTime for, but I want to create the DateTime at the floor of the hour (or any Joda Period will do). How Can I do this?
WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61
33
votes
6 answers

java.util.date to String using DateTimeFormatter

How can I convert a java.util.Date to String using DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss") The Date object which I get is passed DateTime now = new DateTime(date);
Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
33
votes
7 answers

JodaTime - how to get current time in UTC

I want to get the current time in UTC. What I do so far is following (just for testing purposes): DateTime dt = new DateTime(); DateTimeZone tz = DateTimeZone.getDefault(); LocalDateTime nowLocal = new LocalDateTime(); DateTime…
prom85
  • 16,896
  • 17
  • 122
  • 242
32
votes
6 answers

LocalDate interval in Joda-time

Joda-time has an Interval class, which is a range between DateTimes. What can be used for a range of LocalDates? I want an object that represents, for example "from 1/1/2011 to 10/1/2011", without the time (or timezones) ever coming into the…
Wouter Coekaerts
  • 9,395
  • 3
  • 31
  • 35
32
votes
2 answers

Joda-Time: DateTime, DateMidnight and LocalDate usage

Joda-Time library includes different datetime classes DateTime - Immutable replacement for JDK Calendar DateMidnight - Immutable class representing a date where the time is forced to midnight LocalDateTime - Immutable class representing a…
mickthompson
  • 5,442
  • 11
  • 47
  • 59
31
votes
6 answers

Why dec 31 2010 returns 1 as week of year?

For instance: Calendar c = Calendar.getInstance(); DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); c.setTime( sdf.parse("31/12/2010")); out.println( c.get( Calendar.WEEK_OF_YEAR ) ); Prints 1 Same happens with Joda time. :)
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
31
votes
7 answers

In Java, how to get strings of days of week (Sun, Mon, ..., Sat) with system's default Locale (language)

The simplest way: String[] namesOfDays = new String[7] { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; This method does not use Locale. Therefore, if the system's language is not English, this method does not work properly. Using Joda…
Naetmul
  • 14,544
  • 8
  • 57
  • 81
30
votes
5 answers

Understanding the Etc/GMT time zone

What is the rationale behind Apple using Etc/GMT timezone when they return the receipt from the App Store for auto-renewable subscriptions. What exactly is the Etc/GMT time zone? Does the Java SDK understand this time zone? Or do I have to use other…
royalghost
  • 2,773
  • 5
  • 23
  • 29
30
votes
3 answers

converting Joda time Instant to Java time Instant

I have an instance of Instant (org.joda.time.Instant) which I get in some api response. I have another instance from (java.time.Instant) which I get from some other call. Now, I want to compare these two object to check which one get the latest one.…
user123475
  • 1,065
  • 4
  • 17
  • 29
30
votes
5 answers

Java 8 Date equivalent to Joda's DateTimeFormatterBuilder with multiple parser formats?

I currently have a Joda date parser that uses the DateTimeFormatterBuilder with half a dozen different date formats that I may receive. I'm migrating to Java 8's Date routines and don't see an equivalent. How can I do something like this using Java…
Todd
  • 2,829
  • 5
  • 34
  • 56
30
votes
2 answers

How to set Time property in Java using Joda-Time

I want to set the hour, minute and seconds in Joda-Time. But when I set it's not changing the property. Here is my code: import org.joda.time.DateTime; public class JodaAkbar { public static void main(String args[]) { DateTime dt = new…
vkrams
  • 7,267
  • 17
  • 79
  • 129