Binding JLabel with mySQL DATETIME object

Question: I created a Java Destop Application by using a NetBeans 6.9 template. It works fine but when JTable and JLabel have to deal with DateTime or Timestamp field types in MySQL table then I get error messages. I want to use JLabel to show the value of the date fields. The JTable and its corresponding column is binded to an entity class. How to build a custom converter? Answer: MySQL DATETIME, DATE and TIMESTAMP field types are handled well by Java. These types are related. The DATETIME type is used when you need both date and time information. MySQL retrieves and displays DATETIME values in ‘YYYY-MM-DD HH:MM:SS’ format. The DATE type is used when you need only a date value, without a time part. The DATE values are stored in ‘YYYY-MM-DD’ format. I assume that your date field is a DATETIME that is converted into a java.util.Date class in your code. For details see java.util.Date class. Do not mix it with java.sql.Date. The entity manager behind your entity class takes care of the conversion. The java.text.DateFormat class, and its subclass java.text.SimpleDateFormat, provide a convenient way to convert strings with date and/or time info to and from java.util.Date objects. A custom converter for your JLabel looks like this:
    public class dateConverter extends Converter {

        public dateConverter(){
        }

        @Override
        public Object convertForward(Object arg){

            // Convert Date object into String
            try{
                if( arg != null && arg.getClass()==java.util.Date.class){
                    String date = new SimpleDateFormat("yyyy/MM/dd hh:mm").format(arg);
                    return date;
                }
            }catch(Exception ex){
                System.out.println("dateConverter(): " + ex.getMessage());
            }
            return arg;
        }

        @Override
        public Object convertReverse(Object arg){  
            // JLabel is "read-only"

            // Add your reverse code when it is a combo box, text field etc.
            // that returns Date object, for example:

            java.util.Date date = new java.util.Date();  // Default for error handling
            try{
                DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm");
                date = formatter.parse( (String)arg );   // String to Date object
            }catch(Exception ex){
                System.out.println("dateConverter.convertReverse error: " + ex.getMessage());
            }
            return date;   // return Date object
        }
    }
Insert it into the JLabel’s setConverter() method with the NetBeans IDE: 1. Find the JLabel’s properties window. Click on the “Binding” tab and select “text” key. 2. Set the “Binding Source”, for example jTable1 3. Set the “Binding Expression” like ${selectedElement.timeupdated} 4. Click on the “Advanced” tab and find “Type Conversion” 5. Add a Custom Code like this: new dateConverter() The NetBeans will add the new code to the JLabel control, for example:
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.timeupdated}"), jLabelDateUpdate, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setConverter(new dateConverter());
        bindingGroup.addBinding(binding);
Links: How to bind different value types to Swing components Get DATETIME and TIMESTAMP from mySQL Notes: The TIMESTAMP data type has a range of ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC. It has varying properties, depending on the MySQL version and the SQL mode the server is running in. See this example how to convert TIMESTAMP to Date and back in Java.