0

I am using GWT, java, iText to produce a PDF and want to reformat the date. However, this code, on the server side, results in the message "Connection failed" on the client side (there are no error messages in the log) and no output:

          String storedName = " ";
          DateTimeFormat sdf = DateTimeFormat.getFormat("dd-MM-yyyy");

          for (final Transcript scoutNamesDescription : listymAwards) {

              if (scoutNamesDescription.getSection().equals(storedName)){
                  table.addCell(" ");
              }else{
                  storedName = scoutNamesDescription.getSection();
                  table.addCell(scoutNamesDescription.getSection());
              }
              table.addCell(scoutNamesDescription.getAwardName());

              Date awardedDate = sdf.parse(scoutNamesDescription.getAwardedDate());
              String awardedString = DateTimeFormat.getFormat("dd-MM-yyyy").format(awardedDate);
              table.addCell(awardedString);     
          }
          preface.add(table);
          document.add(preface);

When I comment out the date reformatting this works.

I have tried replacing the reformatting with:

System.out.println(scoutNamesDescription.getAwardedDate());
              formatedDate = StringUtils.substring(scoutNamesDescription.getAwardedDate(), 8, 2) +
                      StringUtils.substring(scoutNamesDescription.getAwardedDate(), 4, 4) +
                      StringUtils.substring(scoutNamesDescription.getAwardedDate(), 0, 2);
              System.out.println(formatedDate);

And this also produces the same error between the two println.

Based on Andrei Volgin's reply I have the following:

          String storedName = null;
          DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
          DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");

          for (final Transcript scoutNamesDescription : listymAwards) {

              if (scoutNamesDescription.getSection().equals(storedName)){
                  table.addCell(" ");
              }else{
                  storedName = scoutNamesDescription.getSection();
                  table.addCell(scoutNamesDescription.getSection());
              }
              table.addCell(scoutNamesDescription.getAwardName());

              Date awardedDate = df1.parse(scoutNamesDescription.getAwardedDate());
              String awardedString = df2.format(awardedDate);
              table.addCell(awardedString); 
          }
          preface.add(table);
          document.add(preface);
      }
Glyn
  • 1,933
  • 5
  • 37
  • 60
  • Are all your `include`s in the `server` or `shared` package? – Adam Sep 25 '16 at 10:11
  • Hi Adam, I do believe that I have all includes as I am not getting any error message that normally occur when they are not there. – Glyn Sep 25 '16 at 22:06
  • `DateTimeFormat` has two versions: in `com.google.gwt.i18n.client` and in `com.google.gwt.i18n.shared` packages. The first, client is to be used on the client side only, the shared version can be used on both, client and server side. Check this includes carefully. – Adam Sep 26 '16 at 10:19

1 Answers1

1

You cannot use GWT code on the server side. And in this case there is no need.

Use standard Java tools for formatting dates: Convert java.util.Date to String

Community
  • 1
  • 1
Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58