views:

195

answers:

1

I'm new to joda-time and I didn't find anywhere examples to do some simple things.

I want to make an object where to save a time value read from a table in a database (a java.sql.Time - e.g. "18:30:00") I don't care about time zone, so I think that I need LocalDate. But the problem is that I couldn't create a LocalDate object based on that Time object.

I tried with no success LocalDate.fromDateFields(), DateTimeParser.parseInto(), DateTimeParser.parseDateTime().

EDIT: I should have used LocalTime. These work:

java.sql.Time time = Time.valueOf("18:30:00");
LocalTime lt1 = LocalTime.fromDateFields(time);
LocalTime lt2 = new LocalTime(time);
A: 

According to the documentation, you should be able to construct a LocalDate directly by passing it a java.util.Date as the sole constructor argument. Since a java.sql.Time extends java.util.Date, you should be able to

final LocalDate ld = new LocalDate(mySqlTime);

This works for me:

System.out.println(new LocalDate(Time.valueOf("18:30:00")));

On the other hand, it's not a meaningful thing to do, since you'll always get January 1, 1970. But I imagine you know what you're doing.

Jonathan Feinberg
I already tried `LocalDate ld = new LocalDate(Time.valueOf("18:30:00"));` and it gives `java.lang.UnsupportedOperationException`
True Soft
It works for me.
Jonathan Feinberg
I should have used `LocalTime`. I edited my question.
True Soft