LWUIT Calendar date format

366 views Asked by At

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) {
    }
}
2

There are 2 answers

23
Mun0n On BEST ANSWER

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);
0
PHPFan On
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;  
public class myLibrary extends MIDlet {

Form f;    
Button b;      

public void startApp()  {
    com.sun.lwuit.Display.init(this); 
    private static final int DAY =86400000;
    f = new com.sun.lwuit.Form();
    b = new Button("Enter");
    f.addComponent(b);
    b.addActionListener( new ActionListener()   {
        public void actionPerformed(ActionEvent acv)    {

          java.util.Date d = new java.util.Date();
          d.setTime(d.getTime() + DAY);
          System.out.println(""+ d.toString());
        } 
    });

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

public void destroyApp(boolean unconditional) {
}
}