Results 1 to 3 of 3
  1. #1
    Qlubbie is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default Get dates of current week

    Hello everyone,

    I want to know the dates of the current week, starting from mon to sun.
    the current date is no problem and the current week isn't a problem either.
    I set up a test where i get a couple of info from the current time:
    Calendar cal = Calendar.getInstance();
    Java Code:
    	int day = cal.get(Calendar.DATE);
    	int week = cal.get(Calendar.WEEK_OF_YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            int year = cal.get(Calendar.YEAR);
            int dow = cal.get(Calendar.DAY_OF_WEEK);
            int dom = cal.get(Calendar.DAY_OF_MONTH);
            int doy = cal.get(Calendar.DAY_OF_YEAR);
     
            System.out.println("Current Date: " + cal.getTime());
            System.out.println("Day: " + day);
            System.out.println("Month: " + month);
            System.out.println("Year: " + year);
            System.out.println("Weeknumber: " + week);
            System.out.println("Day of Week: " + dow);
            System.out.println("Day of Month: " + dom);
            System.out.println("Day of Year: " + doy);
    an example of the result:
    Current Date: Tue Nov 18 11:10:26 CET 2008
    Day: 18
    Month: 11
    Year: 2008
    Weeknumber: 47
    Day of Week: 3
    Day of Month: 18
    Day of Year: 323

    So how could i get the start and enddate of this week? (considering Mon is day 1 instead of 2 and sunday is 7 instead of 1)?

    and how can i convert those dates to epochs?

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Since you know the current day in terms of number and mon,tues, etc and cal.getTime knows the day in terms of day of the week check for a method that could return that.

    You need to know if its Monday or Tuesday and the numeric date together in order to subtract to get the first day and add to get the last day. Since monday is 1 instead of sunday your going to need something like
    Java Code:
    if(day = 1 //sunday)
       startWeek = 2; //monday
       day = 2;
       if (startWeek = 2)
            day = monday
    It's a tad confusing since day is both a numeric and string representation. Your basically just working with the current day values and overriding everything to work for the next day. When you get to sunday though your going to need to be clever about making sure that your week can goto 8 so you can have sunday set as 7 and then just reset the counter.

    As for epochs I have no idea what that even is so I wouldn't know.

  3. #3
    Qlubbie is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks,
    but i think i solved it by calculating the difference between the current day of the week and monday and sunday.
    All i have to do then is check if that works when the start or endday of the week isn't in the same month.

    As for epoch....
    Thats the date calculate from the number of seconds passed scince 1-1-1970.
    It's used in a lot of databases because you don't have problems with timezones.
    google epoch converter for some examples. (really handy if you want to calculate something between dates)
    and in sql you can use select abstime(epoch::int); to select the epoch as a date.

Similar Threads

  1. Current week number method
    By levent in forum New To Java
    Replies: 2
    Last Post: 02-01-2010, 05:33 AM
  2. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:06 AM
  3. Comparing dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:02 AM
  4. Inbetween Dates
    By Rageagainst20 in forum New To Java
    Replies: 4
    Last Post: 12-19-2007, 06:24 AM
  5. help with dates and time
    By osval in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 01:41 PM

Posting Permissions

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