Search code examples
java-melwuit

How to format a Date variable in LWUIT?


I have an object whose class has a getter method , and this getter method returns a Date value. I want to show this value in a Label in the format DD/MM/YYYY.

How to achieve that with LWUIT ?

Thank you very much indeed


Solution

  • You can use this code to convert date to string format and pass the this string value to label.

       public static String dateToString (long date)
                 {
                 Calendar c = Calendar.getInstance();
                 c.setTime(new Date(date));
                 int y = c.get(Calendar.YEAR);
                 int m = c.get(Calendar.MONTH) + 1;
                 int d = c.get(Calendar.DATE);
                 String t = (d<10? "0": "")+d+"/"+(m<10? "0": "")+m+"/"+(y<10? "0": "")+y;
                 return t;
                 }