Search code examples
javadatejava-melwuit

LWUIT Calendar date format


I have a LWUIT code that supposed to print today date .

The problem with me is the date printed in "Mon dd hh:mm:ss GMT+...... yyyy" format

e.g Thu Nov 28 01:00:00 GMT+03:00 2013

So I have a couple of questions

  1. How to get the format in "yyyy-mon-dd" format.

  2. how to add a day to the today date after conversion to "yyyy-mon-dd" .

Observe that some classes wouldn't work in J2ME like Simpledateformat class.

import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;  
public class myLibrary extends MIDlet {

    Form f;    
    com.sun.lwuit.Calendar cal;
    Button b;      

    public void startApp()  {
        com.sun.lwuit.Display.init(this); 
        f = new com.sun.lwuit.Form();
        cal = new com.sun.lwuit.Calendar();
        b = new Button("Enter");
        f.addComponent(cal);
        f.addComponent(b);
        b.addActionListener( new ActionListener()   {
            public void actionPerformed(ActionEvent acv)    {
                System.out.println(""+cal.getDate());
            } 
        });

        f.show();
    }
    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

Solution

  • In order to use the java.lwuit.Calendar class, to get your date in that format you will need to substring the data from the cal.getDate().

    for example

    System.out.println("DAY " + cal.getDate().toString().substring(0,3));
    

    Doing that, you will get your data and after that reorder them in a String.

    To change the Date from the Calendar view you will need to use Calendar.setDate(Date d);

    I suggest you to use java.util.Calendar

    java.util.Calendar c = Calendar.getInstnace();
    c.set(Calendar.DAY_OF_THE_MONTH, day_that_you want);
    c.set(Calendar.MONTH, month_that_you want);
    c.set(Calendar.YEAR, year_that_you want);
    
    java.lwuit.Calendar cal = new java.lwuit.Calendar();
    cal.setDate(c.getDate().getTime());
    

    If you still want to use the Date class, try this code, it will print the tomorrow day

    private static final int DAY = 24 * 60 * 60 * 1000; 
    Date d = new Date(); d.setTime(d.getTime() + DAY);