×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Question question about Building a Java ME Canvas based calendar/date picker source code

    Hi, I have implemented a program and a calendar is part of my app. To implement the calendar I have used the calendar source code which is in the forum. I have a few question about it’s source code and that is, in the constructor of the CalendarCanvas class the code below has been called:
    calendar = new CalendarWidget(new Date());
    I want that when I create CalendarWidget object in CalendarCanvas class, instead of new Date() parameter which is always date of today the date of 70 days before of today being shown to the user as the date of today for example for each day the program should minuse date of today and date of 70 days before! to do it I had written some code like below but it doesn't work.Could anyone help me to fix this?
    calendar = new CalendarWidget(System.currentTimeMillisecond()-70*24*60*60*1000);


    And the other question is from the first day of the year till today in the calendar How can I create indexes for all the days? for example for 3 months the last day’s index must be 92, Could anyone tell me how can I create indexes for all the days in a year please?
    Last edited by elenora; 2011-09-15 at 11:21.

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

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    Try calendar = new CalendarWidget(new Date(System.currentTimeMillisecond()-70*24*60*60*1000));
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  3. #3
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    thanks for your reply,I have tried like your code in my program but the program will show the date of today 15 days before the date of today and if I change the numbers for example changing 70 to 170, the date of today will be shown 15 days before of the date of today again! Could you tell me another solution for my first and second problem?

  4. #4
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    how about not passing any arguments, thus something like :
    new CalendarWidget(System.currentTimeMillisecond())

    Also have you had a look at the following blog of Jappit :
    http://www.jappit.com/blog/2008/05/1...l-your-canvas/
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  5. #5
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    thank you, Yes but visiting the website which you mentioned didn't solve my problem!

  6. #6
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    How can I create the DD/MM for any day of the year for the Calendar?what is its methods? In Java apI doc I found I can use
    int get(int field)
    Gets the value for a given time field.
    and

    DAY_OF_YEAR
    Field number for get and set indicating the day number within the current year
    but I don't know How can I use these two things to extract the days of a year and create indexes for the days!?

  7. #7
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    Regarding your problem, of not being able to set the Calendar 70 days in the past, here is a solution

    Code:
    Long current=new Long(System.currentTimeMillis());
    Long days70inMS=new Long(Long.parseLong("6048000000"));	
    Long todayMinus70days=new Long(current.longValue()-days70inMS.longValue());
    		
    calendar = new CalendarWidget(new Date(todayMinus70days.longValue()));

  8. #8
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    Regarding your problem of getting the date in the form of MM and DD, you can get the selected by the user date in this format by using the following code within the getSelectedDate() method:

    Code:
    		
    calendar.get(Calendar.DAY_OF_MONTH)  //returns an integer in the range from 1-31
    calendar.get(Calendar.MONTH)               //returns an integer in the range from 0(January)-11(December)
    Last edited by skalogir; 2011-09-19 at 10:09.

  9. #9
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Lightbulb Re: question about Building a Java ME Canvas based calendar/date picker source code

    Thank you very much for your help, I have a problem again, a question is when I write this code in my prog calendar.get(calendar.DAY_OF_YEAR) the prog will have an error that it can not find symbol DAY_OF_YEAR. so what is your solution about it? I want this to extract all the days from the Calendar! I mean I want to know the day's number in a year (from 1 to 365).
    my next problem is I want the day of today in the week, if for example it is on sunday, the calendar shows it, it's on saturday! Could you tell me, How can I do it?
    and the other question is, in my country first 6 months has 31 days and second 5 month except the last moth, has 30 days. for this I changed the method below:

    int getMonthDays()
    {
    int month = calendar.get(Calendar.MONTH);
    int year = calendar.get(Calendar.YEAR);

    switch(month)
    {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 0:
    return 31;
    case 11:
    return calendar.get(Calendar.YEAR) % 4 == 0 && calendar.get(Calendar.YEAR) % 100 != 0 ? 30 : 29;
    default:
    return 30;
    }

    but when I run the prog, and in the emulator, when for example I select the day 31 in the first month, it jumps to first day of the next month, cause in GMT time, the same month has 30 days while in my locale it has 31 days! I appreciate if you could tell me How can I fix this?
    Last edited by elenora; 2011-09-18 at 20:24.

  10. #10
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    The field DAY_OF_YEAR is not valid for the abstract class Calendar. You can see all valid fields for this class here:
    http://download.oracle.com/javame/co.../Calendar.html

    But you could write your own code, where you calculate the selected day of the year as an integer between 1-365 by taking the selected month and the selected day and by adding the number of days for each of the preceding months to the selected day. For example, in the Gregorian Calendar, 17th of March 2011 would be the sum of the number of days for January and February plus the selected day in March, i.e. 31+28+17=76th day of the year. From what I understand, 17th of March would be 14th of March in your Calendar system, because February would have 31 days instead of 28. But in both cases, i.e. 17th of March in the Gregorian or 14th of March in your Calendar, it is still the 76th day of the year, so you don't need any further conversion.

    Also be careful regarding the ranges of the integer values that you get from your Calendar class and how you convert them to days.
    For example:
    calendar.get(Calendar.DAY_OF_WEEK)
    returns 1(Sunday) to 7 (Saturday)

  11. #11
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    Thank you very much for your help, what do you suggest to me about my last question?

  12. #12
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    Hi elenora,

    Regarding your question of how to correctly move/set the day on your custom calendar without "unexpected jumps", this task is a bit more complex than just modifying the code in the getMonthDays method. So if you decide to use the Calendar Widget example, as base code for your custom calendar, you would also need to do additional modifications.

    First you would have to change the getStartWeekday method. This method uses code that gets the weekday on the first of the current month, in order to calculate how many blank (empty) squares will be painted on the first row of the calendar table. For example if 1st of the current month is Monday, no empty squares are needed in the calendar table's first row. But if it's a Sunday, then the program needs to paint six empty squares from Monday to Saturday. Now the problem is that for example 1st of March 2011 is a Tuesday in the Gregorian Calendar, but I would assume that this is a Friday on your custom calendar. This needs to be corrected with some custom code that gets any date from your custom calendar and returns what weekday it is.

    Also the go method needs to be changed. This method checks whether by moving the cursor, the new cursor's position is located in a new month (by new here I mean any month other than the one that is currently being displayed). So the reason why you notice those "jumps" when you test your code, is because when you move for example from 28th of February 2011 to 29th of February in your custom Calendar, your program doesn't know that it is still the same month for your custom calendar (while that's not the case for the Gregorian Calendar).

    Then there are calls from within the paint method that get the current month, year and day of month. You cannot rely on those calls because they would return different values that are based on the Gregorian Calendar.

    You could implement the changes by defining a custom calendar class where there would be methods for translating a date from the Gregorian Calendar to your custom calendar. Then you would instantiate an object of that class in the very beginning, for example in the setCalendar method. That object would be your custom calendar alternative variable that is aware of the differences between the Gregorian and your Custom Calendar. So every time you would have to perform operations like getting the current month, or day of month or on what weekday a certain date falls, you would call your custom methods, instead of the predefined ones.
    Last edited by skalogir; 2011-10-11 at 14:13.

  13. #13
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Exclamation Re: question about Building a Java ME Canvas based calendar/date picker source code

    Dear Skalogir,
    thank you very much for your help, regarding your answer, I have changed the code like you said earlier, and now I have a problem that is the yellow cursor which moves towards up and down and left and right, after I changed the code, in fact it moves when I click on the right key or up key and .... but in showing, the cursor always refer to current date so I can't understand that for example when I click on the left button, what date the cursor is refering to?!!!. I think I have to change some codes in the paint or ...? Could you suggest me a solution about it?

  14. #14
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: question about Building a Java ME Canvas based calendar/date picker source code

    Hi elenora,

    The calendar object, which contains the current date when the application launches, is used to store the selected Calendar dates when the user moves with the up/down/left/right keys. When selecting a date, the getSelectedDate() method is called which returns the updated value of the calendar object. Since you keep getting the current date, you have most likely modified your code so that the calendar object is not properly updated.

    If you need further help for your code specific problems I suggest you use Nokia's professional Technical Support Services:

    http://www.developer.nokia.com/Resou..._support.xhtml

Similar Threads

  1. Calendar Source Code
    By somnathbanik in forum Symbian
    Replies: 48
    Last Post: 2010-12-31, 15:43
  2. java me canvas based calendar UNHANDLED EXCEPTION
    By youssef.lahoud in forum Mobile Java General
    Replies: 18
    Last Post: 2010-05-06, 05:58
  3. J2ME Canvas Based Calendar
    By hm991 in forum Mobile Java General
    Replies: 0
    Last Post: 2010-03-11, 05:50
  4. Problem in Canvas based Calendar
    By rachelwiz in forum Mobile Java General
    Replies: 8
    Last Post: 2009-09-25, 08:22
  5. Canvas based calendar/date picker
    By barrueco in forum Symbian
    Replies: 6
    Last Post: 2009-07-29, 19:05

Posting Permissions

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