1

I am pulling a Date from a database, reassigning it as a TextField, and then toString() it, but the date comes out "Thurs 0900 OCT 12 2015" when all I need is MM/DD/YYYY format. How do I change the format?

  Date myDatabaseDate = someDBGetMethod();

    TextField myDateTF = new TextField()
    myDateTF.setCaption("My date is: ");
    myDateTF.setValue(myDatabaseDate).toString());
    myDateTF.setReadOnly(true);

FormLayout fLayout = new FormLayout();
addComponent(fLayout);
fLayout.addComponent(myDateTF);
  • What's happening: Thu Oct 22 12:19:04 CDT 2015
  • What I want: 22/10/2015

Thank you in advance! Examples make the most sense to me as I am very new to vaadin.

Null
  • 139
  • 5
  • 14
  • 1
    Have you checked the [Vaadin book examples on date fields](https://vaadin.com/book/-/page/components.datefield.html)? They're specifically designed for _date manipulation_. – Morfic Oct 29 '15 at 20:30
  • 2
    Beside using a DateField for this, if you only wish to display it, then it's just as everywhere in java, you need a DateFormat class for this. For example http://stackoverflow.com/questions/16424667/java-formatting-date – André Schild Oct 30 '15 at 06:35

2 Answers2

2

Vaadin Not The Problem

If trying to display a String representation of a date-time object, then Vaadin is not a part of the problem. Vaadin offers a widget, DateField, for the user to pick a date-time value. You do not need that widget for simply displaying the string representation. For display, use a TextField as you described in the Question.

So the problem is how to generate that String representation.

First be aware that Java includes two classes named "Date":

Confusingly, they are not parallel. The util one is a date plus a time-of-day in UTC. The sql one is meant to represent a date-only, but is poorly designed; as a hack it subclasses the util one but alters the time-of-day to be 00:00:00.000. These old date-time classes in early Java are a bad mess.

From a database you should be receiving the latter, java.sql.Date for a date-only stored value. If you were storing date-time values, then you should be getting java.sql.Timestamp objects from your JDBC driver.

java.time LocalDate

As java.sql.Date is one of the old clunky date-time classes bundled with early Java, we should convert to the new classes found in the java.time framework built into Java 8 and later. See Tutorial.

For a date-only value, java.time offers the LocalDate class. The "Local" in the name refers to any locality rather than a particular locality. It represents the vague idea of a date, but is not tied to the timeline. A date starts earlier in Paris than in Montréal, for example. If you care about exact moments on the timeline, you would be using java.sql.Timestamp rather than java.sql.Date.

Converting from java.sql.Date to java.time.LocalDate is an easy one-liner, as a conversion method is provided for you.

LocalDate localDate = myJavaSqlDate.toLocalDate();

Going the other way is just as easy, when storing data into the database.

java.sql.Date myJavaSqlDate = java.sql.Date.valueOf( localDate );

With a LocalDate in hand, you can call on the java.time.format package to format a string localized for easy reading by the user. Specify the Locale expected by the user.

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH );
String output = localDate.format( formatter );

Example output:

dimanche 1 novembre 2015

ZonedDateTime

Let’s consider if you did get a java.sql.Timestamp rather than java.sql.Date.

We can convert from a java.sql.Timestamp to a Instant, a moment on the timeline in UTC.

Instant instant = myTimestamp.toInstant();

Next, assign a time zone.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId);

We use the java.time.format package to create the String representation. Note the chained calls, the second one setting a specific Locale. If not specified, your JVM’s current default Locale is implicitly applied. Better to be explicit.

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL ).withLocale ( Locale.CANADA_FRENCH );
String output = zdt.format ( formatter );

Example output:

dimanche 1 novembre 2015 2 h 24 EST

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

java.util.date object does not contain any information how to present himself as String. That's because in different locations communities writes dates in different ways. So Java separates operations on dates from printing them out on the screen. What you are missing in your code is a DateFormat.

    Date myDatabaseDate = new Date(2014,10,10);
    DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK);
    TextField myDateTF = new TextField();
    myDateTF.setCaption("My date is: ");
    myDateTF.setValue(format.format(myDatabaseDate));
    myDateTF.setReadOnly(true);
    layout.addComponent(myDateTF);

I would strongly encourage you however to use Java 8 Dates API since Java 7 Dates API is a total mess. You can read more about that here What's wrong with Java Date & Time API?

Community
  • 1
  • 1
kukis
  • 4,489
  • 6
  • 27
  • 50