Getting the Day of the Week without Calendar

I've written a program that needs to determine the day of the week to operate. Before, I was using Calendar and Gregorian Calendar, which work great.

However, I need to host the program online, so to my knowledge I can only use the day() function because Java Imports don't work online.

Is there a way to easily calculate the day of the week (from 1 - 7 etc) in a way that will work with JavaScript online?

Thank you!

Answers

  • edited February 2014

    In JS, it's as simple as to type in -> var week = new Date().getDay(); in order to obtain the # of the current week! O:-)
    However by doing so, it breaks Java Mode! And you'd be stuck in JS Mode from that moment on! :o3

    A proper solution to keep both Java & JS modes working is to create a independent ".js" tab.
    Then setup an interface from the ".pde" side to call upon that native side!
    That's exactly I was reading about these days. If you're interested, read on that article below:

    processingjs.org/articles/PomaxGuide.html

    But you're very luck this time! Java got a class called Date too! And coincidentally, it got a method called getDay() as well!

    download.java.net/jdk8/docs/api/java/util/Date.html#getDay--

    Even though deprecated, it works alright! :D
    Hence Date class functions as a compatibility bridge for such Java & JS methods! <:-P

    /**
     * Date Java-JS Bridge (v1.0)
     * by  GoToLoop (2014/Feb)
     *
     * forum.processing.org/two/discussion/3350/
     * getting-the-day-of-the-week-without-calendar
     *
     * download.java.net/jdk8/docs/api/java/util/Date.html#getDay--
     */
    
    import java.util.Date;
    
    final int week = new Date().getDay();
    println(week);
    
    exit();
    

    Even w/ this fix easily possible, Processing should really consider providing a week() along the other Date functions as well! :-w

  • edited February 2014

    If you want a version that independent of any Java libraries then try this (based on Zeller's algorithm)

    String[] dayName = { 
      "Sunday", "Monday", "Tuesday", "Wednesday", 
      "Thursday", "Friday", "Saturday"
    };
    int w;
    
    public void setup() {
      w = dow(28, 2, 2014);
      println(w + "  " + dayName[w] );
    }
    
    // d = day in month
    // m = month (January = 1 : December = 12)
    // y = 4 digit year
    // Returns 0 = Sunday .. 6 = Saturday
    public int dow(int d, int m, int y) {
      if (m < 3) {
        m += 12;
        y--;
      }
      return (d + int((m+1)*2.6) +  y + int(y/4) + 6*int(y/100) + int(y/400) + 6) % 7;
    }
    
  • edited February 2014

    Fantastic @quark ! But beware of reassigning Processing APIs like day() due to JS:

    String[] day = {}; -> day()

  • @GoToLoop - good point have changed the code above to avoid clash with day()

  • edited February 2014

    Nice @quark ! Although a plural name would be even better! I know, I'm being grammar Nazi! >:)

    @dangreenbergdesign already had a thread where his JS was bugged exactly due to a String reassigning day()! @-)

    forum.processing.org/two/discussion/3132/cant-figure-out-why-processing-java-code-not-working-with-javascript

  • Answer ✓

    Although a plural name would be even better!

    That would be personal preference and I have used both singular and plural in the past and will in the future.

    dayNames - describes the entire array but when referring it its elements we are talking about a single name i.e. dayName[3] so take your pick.

    I don't think there is an accepted naming convention for arrays and collections that covers this. :-?

  • edited February 2014

    I don't think there is an accepted naming convention for arrays and collections that covers this.

    Officially that's true! /:)

    ... but when referring it its elements we are talking about a single name.

    I consider dayNames[3] as picking element #3 amidst the dayNames! :P

  • Thanks for your help! You're right, I did have some issues with day vs day() earlier haha.

    Which would be the most robust way to do this? I don't mind breaking regular Java, just need it to work online.

    I tried adding in the codes, but none seemed to work with java. My code is below, could you please show a noob how to incorporate it?

    Thanks!!!

    //import java.util.GregorianCalendar;
    //import java.util.Calendar;
    //import java.util.Date;
    int xc,yc;
    float angle, dayangle, hourangle, daytimeangle;
    int radiusi, radiuso, radius;
    float innerx, innery, outerx, outery, dayxi, dayyi, dayxo, dayyo;
    float frequency;
    String days;
    int i;
    
    int framesday;
    
    int weekday;
    int weekdayadd;
    
    
    void setup() {
      size(200,200);
      xc=width/2;
      yc=height/2;
      radiusi=55;
      radiuso=90;
      radius=(radiusi+radiuso)/2;
      angle=3*PI/2;
    
      i=0;
      framesday = 2592000; //2592000
      frameRate(30);
    
      background(50);
    
    
    
    }
    
    void draw() {
      background(50);
      //GregorianCalendar gcal = new GregorianCalendar();  
      //weekday = gcal.get(GregorianCalendar.DAY_OF_WEEK);
    
    
      weekday=day() % 7 - 1; //current bad way of getting day of week lol
    
      float s;
      s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
      float m;
      m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI;
      float h;
      h = map(hour() + norm(minute(), 0, 60) + norm(second(), 0, 3600), 0, 24, 0, TWO_PI) - HALF_PI;
      angle = h;
      hourangle = map(hour() + norm(minute(), 0, 60) + norm(second(), 0, 3600), 0, 24, 0, TWO_PI/7) - HALF_PI;
      daytimeangle = dayangle*(weekday-1)+hourangle;
      innerx=xc+cos(angle)*(radiusi);
      innery=yc+sin(angle)*(radiusi);
      outerx=xc+cos(angle)*(radiuso);
      outery=yc+sin(angle)*(radiuso);
      dayxi=xc+cos(daytimeangle)*(radiusi);
      dayyi=yc+sin(daytimeangle)*(radiusi);
      dayxo=xc+cos(daytimeangle)*(radiuso);
      dayyo=yc+sin(daytimeangle)*(radiuso);
    
      fill(230);
      strokeWeight(3);
      ellipse(xc,yc,2*radiuso+2,2*radiuso+2);
    
      switch(weekday) {
        case 1:
          days = "N";
          break;
        case 2:
          days = "M";
          break;
        case 3:
          days = "T";
          break;
        case 4:
          days = "W";
          break;
        case 5:
          days = "H";
          break;
        case 6:
          days = "F";
          break;
        case 7:
          days = "S";
          break;
      }
    
    
      fill(50);
      stroke(50);
      textSize(84);
      textAlign(CENTER,CENTER);
    
      //text(days,xc,yc-7);
    
      dayangle=2*PI/7;
      float offset;
      offset = .265;
      stroke(50);
      strokeWeight(1);
      fill(240);
    
      line(xc,yc,xc,yc+sin(-PI/2)*radiuso);
      line(xc,yc,xc+cos(-PI/2+dayangle)*radiuso,yc+sin(-PI/2+dayangle)*radiuso);
      line(xc,yc,xc+cos(-PI/2+2*dayangle)*radiuso,yc+sin(-PI/2+2*dayangle)*radiuso);
      line(xc,yc,xc+cos(-PI/2+3*dayangle)*radiuso,yc+sin(-PI/2+3*dayangle)*radiuso);
      line(xc,yc,xc+cos(-PI/2+4*dayangle)*radiuso,yc+sin(-PI/2+4*dayangle)*radiuso);
      line(xc,yc,xc+cos(-PI/2+5*dayangle)*radiuso,yc+sin(-PI/2+5*dayangle)*radiuso);
      line(xc,yc,xc+cos(-PI/2+6*dayangle)*radiuso,yc+sin(-PI/2+6*dayangle)*radiuso);
    
      fill(50);
      textSize(24);
    
      text("N",xc+cos(-PI/2+offset)*radius,yc+sin(-PI/2+offset)*radius-2);
      text("M",xc+cos(-PI/2+offset+dayangle)*radius,yc+sin(-PI/2+offset+dayangle)*radius-2);
      text("T",xc+cos(-PI/2+offset+2*dayangle)*radius,yc+sin(-PI/2+offset+2*dayangle)*radius-2);
      text("W",xc+cos(-PI/2+offset+3*dayangle)*radius,yc+sin(-PI/2+offset+3*dayangle)*radius-2);
      text("H",xc+cos(-PI/2+offset+4*dayangle)*radius,yc+sin(-PI/2+offset+4*dayangle)*radius-2);
      text("F",xc+cos(-PI/2+offset+5*dayangle)*radius,yc+sin(-PI/2+offset+5*dayangle)*radius-2);
      text("S",xc+cos(-PI/2+offset+6*dayangle)*radius,yc+sin(-PI/2+offset+6*dayangle)*radius-2);
    
      stroke(50);
      strokeWeight(3.5);
      //line(xc-2,yc-2,innerx+2,innery+2);
      line(xc,yc,dayxo,dayyo);
    
    
    }
    
  • Answer ✓

    weekday=day() % 7 - 1; //current bad way of getting day of week lol

    Since your are only interested in the current date and time then there is nothing incorrect with your code. The example I gave was to find the day-of-the-week for any arbitrary date so is not applicable here.

    Since the clock resolution is 1 second then you only need a frame rate of 1 fps, unless there are other things to add that need a higher framerate.

  • edited October 2016

    I tried adding in the codes, but none seemed to work with Java.

    Both my new Date().getDay(); snippet and @quark 's utility function work in both Java & JS modes! :-w
    Both of them return an int in a range of 0 to 6 representing the day of the week! \m/
    So I don't get why you couldn't apply any of those solutions into your program! :>

    Anyways, I've made a static class which represents a FullDate, from second up to year, and including the week too! :-bd
    And in setup() there are some examples of its usage.

    Also it's working in both Java & JS of course. You can check it out running online below: B-)

    studio.processingtogether.com/sp/pad/export/ro.9to4yV59zus7B/latest

    Good luck adapting it to your code: %%-

    /**
     * FullDate Class (v1.05)
     * by GoToLoop (2014/Mar)
     *
     * forum.Processing.org/two/discussion/3350/
     * getting-the-day-of-the-week-without-calendar#Item_10
     *
     * studio.ProcessingTogether.com/sp/pad/export/ro.9to4yV59zus7B
     */
    
    import java.util.Date;
    
    static final class FullDate {
      static int s, m, h;
      static int d, o, y;
    
      static int w;
      static char wc;
      static String wn, mn;
    
      static String inf;
    
      static final char[] weekChars = {
        'N', 'M', 'T', 'W', 'H', 'F', 'S'
      };
    
      static final String[] weekNames = {
        "Sunday", "Monday", "Tuesday", "Wednesday", 
        "Thursday", "Friday", "Saturday"
      };
    
      static final String[] monthNames = {
        "January", "February", "March", "April", 
        "May", "June", "July", "August", 
        "September", "October", "November", "December"
      };
    
      static final void refresh() {
        final Date present = new Date();
    
        s = present.getSeconds();
        m = present.getMinutes();
        h = present.getHours();
    
        w = present.getDay();
        d = present.getDate();
        o = present.getMonth();
        y = present.getYear() + 1900;
    
        wc = weekChars[w];
        wn = weekNames[w];
        mn = monthNames[o];
    
        inf = present.toString();
      }
    
      static final int dow() {
        return dow(d, o, y);
      }
    
      // Wikipedia.org/wiki/Zeller's_congruence
      static final int dow(int d, int o, int y) {
        if (o < 2) {
          o += 12;
          y--;
        }
    
        return ( d + (int) ((o + 2)*2.6) + y + (y>>2)
          + (y/100 | 0)*6 + ~~(y/400) + 6 ) % 7;
      }
    }
    
    void setup() {
      FullDate.refresh();
    
      println(FullDate.inf);
      println(FullDate.y + "\t" + FullDate.mn);
    
      println(str(FullDate.wc) + " - " + FullDate.dow()
        + "\t" + FullDate.wn);
    
      exit();
    }
    
  • I got Quarks code to work, was just misreading how to implement it :/

    Thanks for all your help! :-c

Sign In or Register to comment.