We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Calendar/date lookup table
Page Index Toggle Pages: 1
Calendar/date lookup table (Read 1966 times)
Calendar/date lookup table
Jul 3rd, 2008, 5:45pm
 
All,

I have four lines on my cell phone plan, which we share 1000 minutes. Lately I am getting charged a lot for overages of that 1000 minutes. I am working on plotting each users usage over a month. We are only charged for calls made between 7am to 9pm, monday through friday. I have all the information I need in a csv file with the following column titles:

Code:

Date,Destination,Time,Number,Call Type,Minutes,Airtime,Toll, Total


Since we are not charged for weekend usage, I need a function were I input a date i.e. 7/3/2008 and return whether or not that date is a weekend. What is the easiest way to do this? Is there a calendar lookup table available?
Re: Calendar/date lookup table
Reply #1 - Jul 3rd, 2008, 7:05pm
 
java.util.Calendar lets you get the day of the week for a given date.

api:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

example:
http://www.exampledepot.com/egs/java.util/DayOfWeek.html
Re: Calendar/date lookup table
Reply #2 - Jul 3rd, 2008, 10:45pm
 
Sweet! Thanks a lot. I was working on creating a function out of an algorithm I found here: http://www.geocities.com/siliconvalley/heights/3836/jdnfuncs.html#weekday

Mainly converting the following javascript:
Code:

function jdjday(yyyymmdd)
{ ymd = yyyymmdd;
// get year only
var iyear = ymd - (ymd % 10000);
// get month only
var imonth = (ymd - iyear);
imonth = (imonth - (imonth % 100)) / 100;
iyear = iyear / 10000;
var iday = ymd % 100;
if (imonth < 3) { imonth += 12; iyear --;}
imonth++;
var ia = (iyear - (iyear % 100)) / 100;
var ib = (ia - (ia % 4)) / 4;
var ic = 2 - ia + ib;
var ie = (iyear + 4716) * 1461;
ie = (ie - (ie % 4)) / 4;
var ig = imonth * 306001;
ig = (ig - (ig % 10000)) / 10000;
var ijd = ic + iday + ie + ig - 1524;
return ijd;
}

Code:

{wk=jdjday(document.FMWKDfrCal.caldatew.value);
wk = (wk +1) % 7;
if (wk == 0) wkd = 'Sunday';
if (wk == 1) wkd = 'Monday';
if (wk == 2) wkd = 'Tuesday';
if (wk == 3) wkd = 'Wednesday';
if (wk == 4) wkd = 'Thursday';
if (wk == 5) wkd = 'Friday';
if (wk == 6) wkd = 'Saturday';
openwin('Weekday for '+document.FMWKDfrCal.caldatew.value,wk+' is '+wkd);
}
else {alert('Input date format: yyyymmdd')}


I still may try this out because for the calendar Class I'll have to convert the numeric representation of the month to a String where the above lends itself to the format of how the date is represented in my csv file as mm/dd/yyyy. Again thanks.
Re: Calendar/date lookup table
Reply #3 - Jul 4th, 2008, 10:57am
 
run away!

rather than using random code from the internet that you have to translate yourself, why not use the tried and tested java libraries that do all this for you?

(ok, that example i posted before wasn't the best...)

SimpleDateFormat lets you specify a pattern for parsing dates...
http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("M/d/y");
Date date = sdf.parse(mmddyyyy);
cal.setTime(date);
System.out.println("Day Of Week: " + cal.get(Calendar.DAY_OF_WEEK));

where mmddyyyy is your string, for instance "10/31/1999".

output is a bit strange, 6 = friday, but you can change this using cal.setFirstDayOfWeek(Calendar.SUNDAY) or whatever.
Page Index Toggle Pages: 1