Results 1 to 6 of 6

Thread: Calendar API in java

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Calendar API in java

    Hello,
    I have a concern with the Calendar API in java. I run the following code:
    Code:
          String str = "200901";
            Calendar cal = new GregorianCalendar();
            cal.set(Calendar.YEAR, Integer.function valueOf() {
        [native code]
    }(str.substring(0, 4));
            cal.set(Calendar.WEEK_OF_YEAR, Integer.function valueOf() {
        [native code]
    }(str.substring(4, 6));
    I would like my calendar points to the 1st week of 2009. But it points after:
    Code:
     new GregorianCalendar()
    Any help on this?

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Calendar API in java

    Hi,
    Already I'd have to start with a calendar.clear () to have an empty date and clean, with no data "clean". It will point to the 1st week of 2008. (And here I understand that to one weeks less than the previous decrements me my year but how do I get around?). Your code works but I bet you the wrong poster, which is the case if you simply display the week number and year, the first week of 2009 begins December 29 2008. You have to correct this first.

  3. #3
    Join Date
    Dec 2009
    Posts
    292

    Re: Calendar API in java

    Hello,
    Thank you for your reply. I have to initialize a calendar with the fields corresponding to 4 YEAR first character of the area and 200,901 WEEK OF YEAR 2 with the following. For the moment I can not and I agree with you on the fact that the first week of 2009 begins December 29, 2008. But when I get the year and week I have the problem of decrement of the year by setting my week. Thank you in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Calendar API in java

    Hello,
    If I am not wrong there is no decrement. Calendar as SimpleDateFormat allows you to get a date. By specifying only the year and week number, you get in this case the first day of the first week of 2009. i.e the December 29, 2008. If you only view the year and number of weeks of this date, you will 2008 and 01. I think you have not understood the concept clearly. First get the concept clear and then program it, it will be easier for you.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Calendar API in java

    Hello,
    I think you have a problem in he following code, just see, I will try to explain you. The next method you return a week. For example the 1st week of 2009 is id '200901 '.
    Code:
        Public static String identifier(Calendar cal) {
            return String.function valueOf() {
        [native code]
    }(cal.get(Calendar.YEAR)) + DOUBLE_ZERO_FORMAT.format(cal.get(Calendar.WEEK_OF_YEAR));
        }
    If you give it January 2, 2009, the method will returns you 200,901.
    But December 30, 2008 returned 200,801 when you want 200,901. Hope you got the point what I am trying to explain you.

  6. #6
    Join Date
    Nov 2009
    Posts
    343

    Re: Calendar API in java

    Hello,
    I have a code with me, please check out, i thin it will help you. it is very simple. I hope you will understand it and interpret in your program.
    Code:
     Public static String identifier(Calendar calendar) {
        	int = calendar mn.get(Calendar.MONTH);
        	int = calendar yr.get(Calendar.YEAR);
        	int = calendar wk.get(Calendar.WEEK_OF_YEAR);
        	    	
        	if (mn == Calendar.DECEMBER & & Week ==1) {
        		/ / If it is in December on the wk No. 1:
        		/ / We must add a yr:
        		yr + +;
        	} else if (mn == Calendar.JANUARY & & Week>10) {
        		/ / If we're in January on one of the last wks (I took off: have:)
        		/ / We must remove a yr:
        		yr -;
        	}
            return String.format("% 04d% 02d", Year, wk);
        }

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2012, 12:24 PM
  2. Replies: 5
    Last Post: 23-04-2011, 10:19 PM
  3. help me to understand java code (calendar java code) ?
    By so0oma in forum Software Development
    Replies: 2
    Last Post: 10-01-2011, 12:01 AM
  4. Replies: 3
    Last Post: 18-10-2010, 12:12 PM
  5. Calendar program in java
    By D_chapple in forum Software Development
    Replies: 3
    Last Post: 01-12-2009, 05:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,864,615.02614 seconds with 17 queries