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
57
votes
11 answers

Android Java - Joda Date is slow

Using Joda 1.6.2 with Android The following code hangs for about 15 seconds. DateTime dt = new DateTime(); Originally posted this post Android Java - Joda Date is slow in Eclipse/Emulator - Just tried it again and its still not any better. Does…
Mark Worsnop
  • 4,407
  • 15
  • 54
  • 79
57
votes
4 answers

Period to string

I'm using the Joda-Time library with Java. I'm having some difficulty trying to turn a Period object to a string in the format of "x days, x hours, x minutes". These Period objects are first created by adding an amount of seconds to them (they are…
tt
55
votes
2 answers

Defaulting date time zone to UTC for Jodatime's DateTime

I am currently creating UTC DateTime objects using the current idiom DateTime now = new DateTime(DateTimeZone.UTC); Is there any way to default so I can create UTC-based DateTime objects using the default constructor so it is more…
algolicious
  • 1,182
  • 2
  • 10
  • 14
55
votes
2 answers

How to convert Joda Localdate to Joda DateTime?

I'm trying to simply add TimeZone information back into a LocalDate before performing some more calculations. The LocalDate came from using the ObjectLab LocalDateCalculator to add days to an existing DateTime but the method needs to return a…
Simon Gibbs
  • 4,737
  • 6
  • 50
  • 80
54
votes
3 answers

How can I parse a date including timezone with Joda Time

This snippet of code always parses the date into the current timezone, and not into the timezone in the string being parsed. final DateTimeFormatter df = DateTimeFormat .forPattern("EEE MMM dd HH:mm:ss 'GMT'Z yyyy"); final DateTime…
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
53
votes
6 answers

Joda time : How to convert String to LocalDate?

How to specify the format string to convert the date alone from string. In my case, only the date part is relevant Constructing it as DateTime fails: String dateString = "2009-04-17"; DateTimeFormatter formatter =…
bsr
  • 57,282
  • 86
  • 216
  • 316
51
votes
7 answers

Jodatime start of day and end of day

I want to create an interval between the beginning of the week, and the end of the current week. I have the following code, borrowed from this answer: private LocalDateTime calcNextSunday(LocalDateTime d) { if (d.getDayOfWeek() >…
nhaarman
  • 98,571
  • 55
  • 246
  • 278
51
votes
6 answers

AWS Java SDK - AWS authentication requires a valid Date or x-amz-date header

Getting the following exception when using the AWS SDK for Java and Java 1.8u60+. com.amazonaws.services.s3.model.AmazonS3Exception: AWS authentication requires a valid Date or x-amz-date header (Service: Amazon S3; Status Code: 403; Error Code:…
Andrew Shore
  • 1,391
  • 1
  • 9
  • 7
50
votes
1 answer

Is Joda Time deprecated with java 8 Date and Time API? (java.time)

Is there any reason to use Joda Time if I can use Java 8 Date and Time API (java.time)? Should I use Java 8 Date and Time every time?
Ondrej Bozek
  • 10,987
  • 7
  • 54
  • 70
49
votes
5 answers

How to handle jodatime Illegal instant due to time zone offset transition

I want to set up joda DateTime to today at 2 AM (see sample code below). But I'm getting this exception: Exception in thread "main" org.joda.time.IllegalFieldValueException: Value 2 for hourOfDay is not supported: Illegal instant due to time zone…
michal.kreuzman
  • 12,170
  • 10
  • 58
  • 70
47
votes
8 answers

Parsing time strings like "1h 30min"

Anyone know of a Java library that can parse time strings such as "30min" or "2h 15min" or "2d 15h 30min" as milliseconds (or some kind of Duration object). Can Joda-Time do something like this? (I have an ugly long method to maintain that does such…
Jonik
  • 80,077
  • 70
  • 264
  • 372
47
votes
2 answers

Get java.util.Date out from Joda-Time DateTime

I have code like this: // old_api(Date date) old_api(calendar.getTime()); Currently, I need to replace Calendar with Joda-Time DateTime. I was wondering how I can get java.util.Date out from Joda-Time DateTime?
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
47
votes
3 answers

Adding Joda Time to Android Studio

I got the file from the Joda site and followed these instructions for adding libraries but when I get to the Gradle sync I end up with the error: Gradle Sync Error:Configuration with name 'default' not found. Gradle Build Error:A problem occurred…
Cai
  • 5,063
  • 3
  • 18
  • 23
47
votes
2 answers

difference in seconds between two dates using joda time?

Suppose there are two dates A(start time) & B(end time). A & B could be the time on the same day or even different day. My task is to show difference in seconds. Date format which i am using is Date Format :: "yyyy-MM-dd'T'HH:mm:ss.SSSZ" For e.g.…
Ankit Ostwal
  • 1,033
  • 3
  • 14
  • 32
45
votes
4 answers

JodaTime equivalent of DateUtils.truncate()

I have never used JodaTime before, but answering this question, How to get ordinal Weekdays in a Month. I tried it and came up with this ugly code to unset all fields below day: DateTime startOfMonth = input.withDayOfMonth(1) …
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588