×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    25

    get wrong Month?!

    I used this code to get Date and Time. It works quite well but MONTH argument is wrong. Now's April but it returns 03???
    Can you show me? Thanks for any help!

    Code:
    package getDateTime;
    
    
    import java.util.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    
    public class DateTime extends MIDlet implements CommandListener{
    	private Display disp;
    	private Date d;
    	Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+7"));
    	String time,date;
    	private DateField currentDate;
    	private Command start, exit;
    	private Form form;
    	private int index;
    
    	public DateTime(){
    		form = new Form("Data and Time");
    		d = new Date();
    		start = new Command("Start", Command.SCREEN, 1);
    		exit = new Command("Exit", Command.EXIT, 0);
    		currentDate = new DateField("", DateField.DATE_TIME);
    		currentDate.setDate(d);
    	}
    
    	public void startApp(){
    		form.append("CURRENT TIME IS: ");
    		index = form.append(currentDate);
    		form.addCommand(start);
    		form.addCommand(exit);
    		form.setCommandListener(this);
    		disp = Display.getDisplay(this);
    		disp.setCurrent(form);
    	}
    
    	public void pauseApp(){}
    
    	public void destroyApp(boolean uncond){}
    
    	public void commandAction(Command cmd, Displayable s){
    		if (cmd == exit){
    			notifyDestroyed();
    		}else if (cmd == start){
    			d = new Date();
    			c.setTime(d);
                            date = "Date: " + c.get(Calendar.DAY_OF_MONTH) + c.get(Calendar.MONTH) + c.get(Calendar.YEAR);
    			time = "Time: " + c.get(Calendar.HOUR_OF_DAY) +c.get(Calendar.MINUTE) + c.get(Calendar.SECOND);
    			form.append(time);
                            form.append(date);
    			form.append("\n");
    		}
    	}
    }

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: get wrong Month?!

    Month values are from Calendar.JANUARY ( = 0 ) to Calendar.DECEMBER ( = 11 ). Calendar.APRIL = 3, so that's correct.

    Graham.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    25

    Re: get wrong Month?!

    So how can i get correct number of month? (=4). In my app i need the number, not April ^^
    Thanks.

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: get wrong Month?!

    Just update this line of you code:
    date = "Date: " + c.get(Calendar.DAY_OF_MONTH) + c.get(Calendar.MONTH) + c.get(Calendar.YEAR);

    to

    date = "Date: " + c.get(Calendar.DAY_OF_MONTH) + c.get((Calendar.MONTH)+1) + c.get(Calendar.YEAR);

    thanks,
    ~Amitabh

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    25

    Re: get wrong Month?!

    Thank you, im2amit but it's still incorrect
    Any other help for me?!

  6. #6
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: get wrong Month?!

    As Graham said:"Month values are from Calendar.JANUARY ( = 0 ) to Calendar.DECEMBER ( = 11 ). Calendar.APRIL = 3, so that's correct."

    It is Correct: If you see the java.util.Calendar API document you will find these Constants.

    So now for April you will get Month=3 from this API, which means April only.
    Image Calendar's as a Month Array which JAN,Feb,Mar,Apr.... and see even for you Month[0]=="Jan" and Month[3]=="April"

    but for simple english numeric represenation of Month we write April as 04 instead of 03, so just add +1, as its a know fact and display your date accordingly. Its not a BUG.

    thanks,
    ~Amitabh

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    25

    Re: get wrong Month?!

    All i want in my app is Date and Time data as format below:
    Date: ddmmyyy e.g: 22 April 2010 => 22042010
    Time: hhmmss e.g: 13:24:24 => 132424

    I tried some but it didn't work. I hope you guy can help me a hand. Thank you !

  8. #8
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: get wrong Month?!

    try

    date = "Date: " + c.get(Calendar.DAY_OF_MONTH) + (c.get(Calendar.MONTH)+1) + c.get(Calendar.YEAR);

    This resolve your problem of date: you should now get Date:22042010

    if you are getting Date:2242010 then you need do a bit of extra processing to add 0 before any any month if you get month <9.

    Let us know, what are you getting then we can help you ?

    thanks,
    ~Amitabh

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    25

    Re: get wrong Month?!

    Thank you im2amit. It works.hah
    But i have a question: is there any way to get like this: 22042010 is not 2242010 for date and time is: 020435 not 2435 (2h4m35s). I want to do that because it would be easier to put into my database, in the same format.

  10. #10
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: get wrong Month?!

    is there any way to get like this: 22042010 is not 2242010 for date and time is: 020435 not 2435 (2h4m35s). I want to do that because it would be easier to put into my database, in the same format.
    You can do it like this:
    String dat="Date:";
    int day=c.get(Calendar.DAY_OF_MONTH);
    if(day<10) dat="0"+day;
    else dat=day;
    int mon=(c.get(Calendar.MONTH)+1);
    if(mon<10) dat=dat+"0"+mon;
    else dat=dat+mon;
    dat=dat+c.get(Calendar.YEAR);

    //Print dat

    // make similar logic for time too

    thanks,
    ~Amitabh

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    25

    Re: get wrong Month?!

    Thank you ^^

Similar Threads

  1. Congratulations to im2amit August’s Poster of the Month
    By Nokia Ron in forum News and Announcements
    Replies: 16
    Last Post: 2009-09-18, 18:11
  2. Congratulations to skumar_rao August Poster of the Month
    By Nokia Ron in forum News and Announcements
    Replies: 20
    Last Post: 2008-10-05, 10:32
  3. Hoolee is named Wiki Contributor of the Month for July
    By Nokia Ron in forum News and Announcements
    Replies: 13
    Last Post: 2008-08-20, 15:40
  4. How to Prompt the user for IAP selection only if the wrong IAP is currently selected
    By kaiser_20 in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2005-10-26, 15:19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×