Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    [DateItem] european format

    Hi,

    I added a DateItem in a form and I want it to display the date in European format so here is my code :

    DateItem resultDateBox = new DateItem();
    resultDateBox.setName("resultDate");
    resultDateBox.setTitle("Date");
    resultDateBox.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
    resultDateBox.setDisplayFormat(DateDisplayFormat.TOEUROPEANSHORTDATE);

    But the item is still displayed in US format.

    Moreover i would like to replace the english months'name and other label with french ones. Is it possible to do ? I also want to do the same for the Calendar object that is displayed right besides the 3 date combo boxes.

    Thank you

    Regards

    #2
    Use setInputFormat() and setDisplayFormat(), and remove your call to setDateFormatter().

    Comment


      #3
      Hello,

      I have the same problem, but unfortunately I couldn't accomplish my goal using the setDisplayFormat() method. The DateDisplayFormat enumeration does not provide the format I need (DD.MM.YYYY, seperated with dots instead of slashes). A javadoc note for DateDisplayFormat says:

      Note: In addition to these standard formats, custom formatting can be set by passing a function directly to ${isc.DocUtils.linkForRef('classMethod:Date.setNormalDisplayFormat')} et al. This function will then be executed whenever the appropriate formatter method is called [eg ${isc.DocUtils.linkForRef('method:Date.toNormalDate')}], in the scope of the date instance in question.

      Unfortunately, I don't understand what this is supposed to say. Can anyone help me to set the display format of my DateItem to DD.MM.YYYY? Thank you in advance.

      Comment


        #4
        Until now, I've found out that the Javadoc information is incorrect and refers to SmartClient, not SmartGWT. So I assume it is not possible to set the date format DD.MM.YYYY by using DateItem.setDisplayFormat(). Is it even possible in Java / SmartGWT or do I have to use JavaScript?

        After a bit of research, I found out that it is possible to use Date.setShortDisplayFormat() as a JavaScript statement in the Project HTML file. Apparently, this method accepts a function that must return a formatted date string. My problem is: I need the Date which is about to be entered in the DateItem. Is this even the right approach? Any suggestions?

        I think a method like

        Code:
        setDisplayFormat("d.m.Y")
        is very important and shouldn't be missing in SmartGWT. At the moment it seems like the only way to customize the displayed date is a set of pre-defined constants. It would be much more useful to be able to use a format string.
        Last edited by jballh; 22 Apr 2009, 02:34.

        Comment


          #5
          Java has built-in support for date formats. You can install a CellFormatter in a grid to use them. We do need to port the SmartClient API that allows you to centrally set date formats via a method.

          Comment


            #6
            Hello Isomorphic,

            thanks for your quick reply. I've read about CellFormatter and came to the conclusion that this is for controlling how a grid cell is rendered. This is a great feature, but how is this supposed to help with my problem, since I'm using a DateItem and not a grid. Can you please give a little hint?

            If CellFormatter does not help with this particular problem, can you tell me if there is any way to set a DateItem's date display format to DD.MM.YYYY?

            Comment


              #7
              I have added the following static utility methods to com.smartgwt.client.util.DateUtil along with sample code in the javadocs. These methods allow you to set date formats centrally and will affect the entire application.

              setNormalDateDisplayFormat(DateDisplayFormat format)
              setNormalDateDisplayFormatter(DateDisplayFormatter formatter)

              setShortDateDisplayFormat(DateDisplayFormat format)
              setShortDateDisplayFormatter(DateDisplayFormatter formatter)

              setDateInputFormatter(DateInputFormatter formatter)

              Example usage of setShortDateDisplayFormatter:

              Code:
              DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
                  public String format(Date date) {
                      if(date == null) return null;
                      //you'll probably want to create the DateTimeFormat outside this method.
                      //here for illustration purposes        
                      DateTimeFormat dateFormatter = DateTimeFormat.getFormat("MMM d, yyyy");
                      String format = dateFormatter.format(date);
                      return format;
                  }
              });
              Sanjiv
              Last edited by sjivan; 28 Apr 2009, 19:42.

              Comment


                #8
                Hi Sanjiv,

                that seems like excactly what I was looking for :-)

                Now my question: do I need to build the smartgwt jar from trunk or is it sufficiant to use only the DateUtil (and dependend) classes from trunk together with the 1.0b2 release?

                Thanks
                Michael

                Originally posted by sjivan
                I have added the following static utility methods to com.smartgwt.client.util.DateUtil along with sample code in the javadocs. These methods allow you to set date formats centrally and will affect the entire application.

                setNormalDateDisplayFormat(DateDisplayFormat format)
                setNormalDateDisplayFormatter(DateDisplayFormatter formatter)

                setShortDateDisplayFormat(DateDisplayFormat format)
                setShortDateDisplayFormatter(DateDisplayFormatter formatter)

                setDateInputFormatter(DateInputFormatter formatter)

                Example usage of setShortDateDisplayFormatter:

                Sanjiv

                Comment


                  #9
                  Hi Michael,
                  Please use the jar from the trunk as some associated utility methods have been added to JSOHelper as well.

                  Sanjiv

                  Comment


                    #10
                    Wrong date in chooser when using the localization code

                    I entered the code suggeste in the Various Control sample and the display works fine but the date chooser won't show the date on the text field but Today's date.

                    For example, enter 30/05/2009 on the text field, go to another field, then come back on click on the date chooser and the selected day will be Today's date instead on June 30th, 2009.

                    Code:
                    	        DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
                    	            public String format(Date date) {
                    	                if(date == null) return null;
                    	                //you'll probably want to create the DateTimeFormat outside this method.
                    	                //here for illustration purposes        
                    	                DateTimeFormat dateFormatter = DateTimeFormat.getFormat("dd/MM/yyyy");
                    	                String format = dateFormatter.format(date);
                    	                return format;
                    	            }
                    	        });

                    Comment


                      #11
                      Does your setting for the input format match what you're outputting? If not, that would cause this problem.

                      Comment


                        #12
                        sjivan,

                        GREAT, it works! I think the missing custom date format was a major issue, and now it works. I can now set a date format like "dd.mm.yyyy" for my DateItem. Great, thank you!

                        Comment


                          #13
                          After some testing, I recognized that it still does not work correctly for me.
                          When I use the code from sjivan to set the date format to DD.MM.YYYY, and pop up the date picker and select any day from 1st to 12th, the DateItem changes day into month and month into day. It seems that the date object which is passed to the format(Date) method of DateDisplayFormatter is wrong.

                          So if I click June 12th in the calendar, it becomes December 6th, June 11th becomes November 6th, June 10th becomes October 6th and so on.



                          If I choose any day from 13th to 31st, everything works as it should.



                          When I change the format string from dd.MM.yyyy to MM.dd.yyyy, or to dd.MMM.yyyy (month as name), it works as expected. But then again, that's not the date format I need.

                          I've used the following test code:

                          Code:
                          	public void onModuleLoad() {
                          
                          		DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
                          			@Override
                          			public String format(Date date) {
                          		        if(date == null) return null;     
                          		        DateTimeFormat dateFormatter = DateTimeFormat.getFormat("dd.MM.yyyy");
                          		        String format = dateFormatter.format(date);
                          		        SC.say("DateString: " + date.toString() + "<br>FormatString" + format);
                          		        return format;
                          			}
                          		});
                          		
                          		DynamicForm f = new DynamicForm();
                          		DateItem d = new DateItem();
                          		d.setUseTextField(true);
                          		f.setFields(d);
                          		f.show();
                          	}
                          Last edited by jballh; 25 Jun 2009, 03:32.

                          Comment


                            #14
                            OK, I guess I have to modify the DateInputFormatter as well? For now, I've created a quick and dirty solution, but it works at last. By the way, is there any other way to create a Date in GWT except using the deprecated methods?

                            Code:
                            		DateUtil.setDateInputFormatter(new DateInputFormatter() {
                            			@Override
                            			public Date parse(String dateString) {
                            				String[] parts = dateString.split("\\.");
                            				int[] numbers = new int[3];
                            				
                            				for(int i=0; i<=parts.length - 1; i++)
                            					numbers[i] = Integer.parseInt(parts[i]);
                            				
                            				Date d = new Date();
                            				d.setDate(numbers[0]);
                            				d.setYear(numbers[2] - 1900);
                            				d.setMonth(numbers[1] - 1);
                            				
                            				return d;
                            			}
                            		});

                            Comment


                              #15
                              How can I apply a DateDisplayFormatter to a form item directly?

                              I have two form items, and I want to use two different display formats for them. So I cannot use DateUtil.setShortDateDisplayFormatter, because it changes the format globally.

                              Comment

                              Working...
                              X