javaswt

Get Date from DateTime control


I am working on an Eclipse RCP project and using Google Window Builder.

All I need to do is get the date from a DateTime control named: dateTimeDOB

It must return the date in this format (dd/mm/yyyy). If the day or month value is a single digit, it must have a preceding "0".

I think the DateTime control class is org.eclipse.swt.widgets.DateTime.

So for example:

String strDate = dateTimeDOB.getDate("dd/mm/yyyy");

Solution

  •     DateTime dateTimeDOB = ...
    
        int day = dateTimeDOB.getDay();
        int month = dateTimeDOB.getMonth() + 1;
        int year = dateTimeDOB.getYear();
    
        String strDate = (day < 10) ? "0" + day + "/" : day + "/";
        strDate += (month < 10) ? "0" + month + "/" : month + "/";
        strDate += year;