cscsaba
cscsaba

Reputation: 1289

How can I convert java.util.Date to org.joda.time.DateTime?

I have to use java.util.Date class as field type in a table. But I would like to change the display format of the date field with help of joda time (confortable, prefered to use), thats why I want to convert a Date to DateTime.

I know I oversee something, because there is no such a question in stackoverflow :) but I could not find the soulution among the DateTime constructors and so on. The reverse conversion DateTime.toDate(); exists, but what about the opposite way ?

Thanks for the answers in advance.

Cs

Upvotes: 2

Views: 2726

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338181

Yes, Use Joda-Time

Definitely use Joda-Time or the java.time package in Java 8 (inspired by Joda-Time). The old java.util.Date and java.util.Calendar classes are notoriously troublesome, confusing, and outmoded.

Also, read the Wikipedia pages on UTC and ISO 8601.

Yes, Pass Date To Joda-Time Constructor

➔ Yes indeed, you can pass a java.util.Date object to the constructor of a Joda-Time DateTime object.

The API doc is a bit confusing as this apparently falls into the catch-all version of the constructor taking an java.lang.Object instance. If that Object is in fact a java.util.Date, Joda-Time will extract its millisecond-count-since-epoch and use that number as its own.

Time Zone

A DateTime constructor also assigns a time zone. By default, the JVM’s current default time zone is assigned. I recommend you always pass a desired time zone rather than rely implicitly on the default even if that means calling getDefault.

Example Code

Here is some example code in Joda-Time 2.5 showing how to pass a java.util.Date to a Joda-Time constructor.

java.util.Date date = new java.util.Date();
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime dateTimeMontreal = new DateTime( date , zone );
DateTime dateTimeUtc = dateTimeMontreal.withZone( DateTimeZone.UTC ); // Adjust to another time zone.

Dump to console.

System.out.println( "date: " + date ); // Misleading output. A j.u.Date is in UTC but its toString method applies JVM’s current default time zone.
System.out.println( "dateTimeMontreal: " + dateTimeMontreal );
System.out.println( "dateTimeUtc: " + dateTimeUtc );

When run.

date: Sat Oct 18 18:54:55 PDT 2014
dateTimeMontreal: 2014-10-18T21:54:55.740-04:00
dateTimeUtc: 2014-10-19T01:54:55.740Z

As shown in the Question, to go from a DateTime to java.util.Date, call toDate.

java.util.Date date = dateTimeMontreal.toDate();

Upvotes: 0

user973999
user973999

Reputation:

In Vaadin, if you want to change display format in a table without joda, you simply override the method protected String formatPropertyValue(Object rowId, Object colId, Property property)

Here an example to do it :

Table t = new Table() {

        @Override
        protected String formatPropertyValue(Object rowId, Object colId,
                Property property) {
            Object v = property.getValue();
            if (v instanceof Date) {
                Date dateValue = (Date) v;
                return new SimpleDateFormat("yyyy-MMMM-dd").format(dateValue);
            }
            return super.formatPropertyValue(rowId, colId, property);
        }

    };

Regards

Éric

Upvotes: 1

Related Questions