1

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) {
    }
}
PHPFan
  • 756
  • 3
  • 12
  • 46

2 Answers2

1

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);
Mun0n
  • 4,438
  • 4
  • 28
  • 46
  • Be he can use the both classes!! – Mun0n Dec 01 '13 at 23:38
  • If he is using the Calendar class from LWUIT, he can use the Calendar class from java.util package making the import in the way that I've suggested – Mun0n Dec 01 '13 at 23:41
  • Yes really I need to use LWUIT Calendar not java.util.Calendar so there is a mistake in the title – PHPFan Dec 02 '13 at 07:36
  • Ok, if you want to get the Date from the lwuit Calendar in the format desire, you must cut the string and reformat in the way that you want. Take a look on the answer, I'm going to edit it. – Mun0n Dec 02 '13 at 07:52
  • Thank you your code solve the first problem. how to add one or two day for today date? – PHPFan Dec 02 '13 at 08:19
  • Whaaaaaaaaaaaaaaaaaaaat??? add a day for today date?? Can you explain this? Do you mean, change it? – Mun0n Dec 02 '13 at 08:27
  • for example if the code prints Mon Dec 02 I want it prints Mon Dec 03 – PHPFan Dec 02 '13 at 08:35
  • So you will need to change the selected day in the Calendar, using setDate, but for this, use java.util.Calendar, as I suggest...I will edit again my answer – Mun0n Dec 02 '13 at 09:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42307/discussion-between-elhadi-mamoun-and-jmunoz) – PHPFan Dec 02 '13 at 09:22
  • c.set(c.DAY_OF_WEEK, c.DAY_OF_WEEK+1); c.set(c.MONTH,c.MONTH ); c.set(c.YEAR, c.YEAR); Month printed always as mar it should be Dec why? – PHPFan Dec 02 '13 at 10:10
  • You need to +1 to the month. Printed as march? weird – Mun0n Dec 02 '13 at 10:18
  • Printed as march? yes if month+1 it will be Apri/ DAY_OF_WEEK+1 printed as Fri – PHPFan Dec 02 '13 at 10:23
  • I working with emulator and my computer is setted to correct date – PHPFan Dec 02 '13 at 10:27
  • http://chat.stackoverflow.com/rooms/42307/discussion-between-elhadi-mamoun-and-jmunoz – PHPFan Dec 02 '13 at 10:28
  • 1
    o_O oh man, that is working for me...I don't know how to help you....is weird thing – Mun0n Dec 02 '13 at 10:51
  • I've added an image for you, take a look if you can use something for this – Mun0n Dec 02 '13 at 13:42
  • Output Fri Nov 18 03:00:00 GMT+03:00 0001 – PHPFan Dec 03 '13 at 07:28
  • It working yes but it gives the default date Fri mar not the date that it set to it – PHPFan Dec 03 '13 at 08:58
  • Tell me what date do you wanna set, and I will try to set it in my code – Mun0n Dec 03 '13 at 09:01
  • This is my code (Today is 3/12/2013) and my output is CALENDAR Wed Dec 04 10:32:23 GMT+01:00 2013 java.util.Calendar cal = Calendar.getInstance(); int day = cal.get(java.util.Calendar.DAY_OF_MONTH); System.out.println("DAy " + day); cal.set(java.util.Calendar.DAY_OF_MONTH, day + 1); System.out.println("CALENDAR " + cal.getTime().toString()); – Mun0n Dec 03 '13 at 09:35
  • 1
    if you want to do it with date private static final int DAY = 24 * 60 * 60 * 1000; Date d = new Date(); d.setTime(d.getTime() + DAY); – Mun0n Dec 03 '13 at 09:38
  • I have just tested it seriously now and it works perfectly. thank you v much – PHPFan Dec 03 '13 at 12:46
  • Please before accepting your answer edit it to be as your code in your comment , and then I will accept it. thank you again – PHPFan Dec 04 '13 at 06:50
0
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) {
}
}
PHPFan
  • 756
  • 3
  • 12
  • 46