joda-time
Joda-Time is the widely used replacement for the Java date and time classes.
Joda-Time - Home
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)
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)
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)
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 = p.divideBy(2);
Source: (StackOverflow)
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)
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)
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)
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 DateTime
s instead of java.util.Date
and java.sql.Time
so the conversion is done automatically? Is there another way of streamlining this?
Source: (StackOverflow)
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)
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)
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)
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)