smallseo.info

joda-time

Joda-Time is the widely used replacement for the Java date and time classes. Joda-Time - Home

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 such as Joda time? I guess these third party libraries exist for a good reason, but I've never really compared them myself.


Source: (StackOverflow)

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

I have date as a string in the following format "04/02/2011 20:27:05". I am using the joda library and would like to convert it to DateTime object. I did:

DateTime dt = new DateTime("04/02/2011 20:27:05")

But im getting the following error :

Invalid format: "04/02/2011 14:42:17" is malformed at "/02/2011 14:42:17"

How to convert the above date to a DateTime object ?

Please Help Thank You.


Source: (StackOverflow)

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 dates.

Days.daysBetween(start, end).getDays() gives me 0 if start is in the evening and end in the morning.

I'm also having the same issue with other date fields so I was hoping there would be a generic way to 'ignore' the fields of lesser significance.

In other words, the months between Feb and 4 March would also be 1, as would the hours between 14:45 and 15:12 be. However the hour difference between 14:01 and 14:55 would be 0.


Source: (StackOverflow)

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

    1. Why do we need three classes?

    2. Which one performs better?

    3. Why is dividing a Period or Duration or Interval instance not implemented? E.g. p = p.divideBy(2);


Source: (StackOverflow)

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 java.time API is much cleaner and can do much more than Joda-Time. But I cannot find examples comparing the two.

  • What can java.time do that Joda-Time cannot?
  • What can java.time do better than Joda-Time?
  • Is the performance better with java.time?

Source: (StackOverflow)

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")
  ymd.parseDateTime("20121212")

But, when I compile the project in SBT, I get a nasty:

[warn] Class org.joda.convert.FromString not found - continuing with a stub.
[warn] Caught: java.lang.NullPointerException while parsing annotations in /home/jack/.ivy2/cache/joda-time/joda-time/jars/joda-time-2.1.jar(org/joda/time/DateTime.class)
[error] error while loading DateTime, class file '/home/jack/.ivy2/cache/joda-time/joda-time/jars/joda-time-2.1.jar(org/joda/time/DateTime.class)' is broken
[error] (class java.lang.RuntimeException/bad constant pool tag 10 at byte 42)

I tried the 2.0 version of joda-time, but get the same error.


Source: (StackOverflow)

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 anyone else have this problem or know how to fix it?


Source: (StackOverflow)

Difference in days between two dates in Java?

I've to find number of days between two dates: one, is from report and one, is current date. My snippet :

  int age=calculateDifference(agingDate, today);

Here, calculateDifference method is a private method, agingDate and today are Date objects, just for your clarification. I've followed two articles from Java Forum Thread1 and Thread 2. It works fine in a standalone program. When I include this into my logic to read from report, I'm getting unusual difference values.

Can anyone help me, why is it happening and how can I fix it?

EDIT :

I'm getting a lot greater than the actual difference of Days...

public static int calculateDifference(Date a, Date b)
{
    int tempDifference = 0;
    int difference = 0;
    Calendar earlier = Calendar.getInstance();
    Calendar later = Calendar.getInstance();

    if (a.compareTo(b) < 0)
    {
        earlier.setTime(a);
        later.setTime(b);
    }
    else
    {
        earlier.setTime(b);
        later.setTime(a);
    }

    while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR))
    {
        tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
        difference += tempDifference;

        earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
    }

    if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR))
    {
        tempDifference = later.get(Calendar.DAY_OF_YEAR) - earlier.get(Calendar.DAY_OF_YEAR);
        difference += tempDifference;

        earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
    }

    return difference;
}

Thanx in advance...

Note :

Unfortunately, I couldn't get the answer this way. I've accomplished this problem with the help of Joda-time library.


Source: (StackOverflow)

Persist Joda-time's DateTime via Hibernate

I'm using Jodatime in my Play app, but currently having to do a bunch of converting back and forth from/to java.util.Date and java.sql.Time.

Since jodatime is included in the Play distribution, I'm thinking there's probably a better way to do this. Is there any way I can make my Model fields DateTimes instead of java.util.Date and java.sql.Time so the conversion is done automatically? Is there another way of streamlining this?


Source: (StackOverflow)

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

Below is the method I wrote:

public List<Map<String, Object>> loadNotYetInEmployee(int shift, Date date,
        int transitionVal, String type, User user) {

    DateTime datetime = new DateTime(date);
    datetime = datetime
            .plus(Period.minutes(shiftTiming.getSession1InTime()));

    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

    sql = SqlMapUtils.getSql("attendance.attendancestatus.latein",
            parameters);
    result = getJdbcTemplate().queryForList(sql);
    for (int i = 0; i < result.size(); i++) {
        Date punchInTime = (Date) result.get(i).get("punchtime");
        DateTime punchTime = new DateTime(punchInTime);
    }
    return result;
}

Now from my method you can see I have a Joda-Time DateTime object in object named datetime and from my result I am getting one timestamp which I am converting to jodatime punchTime. Now I want to find out the diff between these two dates, how do I do that?

Thanks In advance


Source: (StackOverflow)

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) when using annotations :

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentLocalDate")
private LocalDate myDate; 

Are their any known problems with using these versions together ?

Update Well turns out the columns get created but unable to populate with any data :

Handler processing failed; nested exception is java.lang.AbstractMethodError: org.joda.time.contrib.hibernate.PersistentLocalDateTime.nullSafeSet

They are incompatible, and I should be using usertype. See answer below.


Source: (StackOverflow)

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 into a java.sql.Timestamp. I have read the Joda-Time docs and see no reference to this.

Thanks.


Source: (StackOverflow)

Joda-Time: 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 serialized to XML as seconds and then recreated from them). If I simply use the getHours() etc. methods in them, all I get is zero and the total amount of seconds with getSeconds.

How can I make Joda calculate the seconds into the respective fields, like days, hours, etc...?


Source: (StackOverflow)

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 library? Is there a helper method out there that already implements it, or should I write the algorithm myself?


Source: (StackOverflow)

Joda Time: First day of week?

How do you get the first day of week given a 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 apparently Saturday. Some apparently Wednesday?!

Wikipedia "Seven-day week"#Week_number


Source: (StackOverflow)