Calculating Work Days In Java

In work this week I came across a couple of problems in which I needed to performs some calculations involving dates and a number of “work days” rather than just normal numbers of days. My first few attempts filed miserably, so I did some Googling to see if I could find anyone else that had come across the same problem and of course, there were plenty of people.

The code I eventually used I found shoehorned into the middle of a coding help forum and, with a little bit of tweaking, it calculated the number of whole work days in between two dates. The code is as follows:

public static int calculateDuration(Date startDate, Date endDate)
{
  Calendar startCal = Calendar.getInstance();
  startCal.setTime(startDate);

  Calendar endCal = Calendar.getInstance();
  endCal.setTime(endDate);

  int workDays = 0;

  if (startCal.getTimeInMillis() > endCal.getTimeInMillis())
  {
    startCal.setTime(endDate);
    endCal.setTime(startDate);
  }

  do
  {
    startCal.add(Calendar.DAY_OF_MONTH, 1);
    if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)
    {
      workDays++;
    }
  }
  while (startCal.getTimeInMillis() <= endCal.getTimeInMillis());

  return workDays;
}

This code solved my first problem, however I couldn’t find any code to solve my second problem, how to calculate a final date from a start date and a specified number of work days, to do the calculation I came up with the following code:

public static Date calculateEndDate(Date startDate, int duration)
{       
  Calendar startCal = Calendar.getInstance();

  startCal.setTime(startDate);

  for (int i = 1; i < duration; i++)
  {
    startCal.add(Calendar.DAY_OF_MONTH, 1);
    while (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
    {
      startCal.add(Calendar.DAY_OF_MONTH, 1);
    }
  }

  return startCal.getTime();
}

Hope this code works as well for you as it did for me.

Check back soon.

Update: Improved the calculating code with the suggestion from Anass (below) and made some more accuracy & business logic improvements to the calculateDuration method. Enjoy.

Calculating Work Days In Java

More Java & TextMate [Updated]

Update: Unfortunately this doesn’t seem to work any more.

A while ago I wrote a short post (here) about some additions that I had made to TextMate in order to improve its compiling of Java applications, I had made the changes because I had started programming in Java at university and wasn’t happy with the way that TextMate handled the files. In the post I promised a follow up post detailing some other improvements I had made to the Java bundle, finally, that post is here, sort of.

I’ve made so many changes to various bundles and themes it has become to difficult / long winded to document the changes in a post so instead, I have decided to release the changes as a small download from the website. You can find the files you will need on the relevant software page (this does not work anymore, read the update at the top of the page). The main changes are some slight tweaks of the Blackboard theme, the main theme I use and the addition of some new snippets, the compiling shortcuts from my previous post & some menu reordering in the Java bundle.

Make sure that you follow the instructions on the download page or in the README file when installing the files, I don’t want you to go breaking your computer now.

Sorry for the delay in this one, hope it satisfies you appetite, check back soon.

More Java & TextMate [Updated]

TextMate, Java & Compiling [Updated]

For those of you that do not already know TextMate is a brilliant text editor for Mac OS X, including syntax highlighting and the most useful snippets system I have ever come across. If your a budding coder and want to try it out visit: macromates.com, and download a copy.

This post illustrates some of the bundle items I have either modified or added to TextMate to make it easier to use for Java development. If you use TextMate already then hopefully you will know how to use the bundle editor if you don’t then there is some good documentation available here.

In the code snippets below a ‘\’ at the end of a line indicates the code continues but is illustrated on the line below.

  • Compile, but no run.
echo "<h2>Compiling $TM_FILENAME</h2>"
cd "$TM_DIRECTORY"
javac -encoding UTF8 "$TM_FILENAME" &> >("${TM_RUBY:-ruby}" -rtm_parser -eTextMate.parse_errors)
  • Run in terminal.
osascript <<EOF
  tell application "Terminal"
    activate
    try
      close front window
    end try
    do script "cd '$TM_DIRECTORY'; java '${TM_FILENAME%.java}'"
  end tell
EOF
  • Compile and run in TextMate.
echo "<h2>Compiling $TM_FILENAME</h2>"
cd "$TM_DIRECTORY"
javac -encoding UTF8 "$TM_FILENAME" &> >("${TM_RUBY:-ruby}" -rtm_parser -eTextMate.parse_errors)
if (($? >= 1)); then exit; fi

{ java -Dfile.encoding=utf-8 "${TM_FILENAME%.java}" echo -e "\nProgram exited with status $?."; }|pre
  • Compile and run in terminal.
echo "<h2>Compiling $TM_FILENAME</h2>"
cd "$TM_DIRECTORY"
javac -encoding UTF8 "$TM_FILENAME" &> >("${TM_RUBY:-ruby}" -rtm_parser -eTextMate.parse_errors)
if (($? >= 1)); then exit; fi

osascript <<EOF
  tell application "Terminal"
    activate
    try
      close front window
    end try
    do script "cd '$TM_DIRECTORY'; java '${TM_FILENAME%.java}'"
  end tell
EOF
  • Compile and run package item. This also relies on you setting a CLASSPATH variable in you .bashrc, and will only work for packages one deep, still useful if you are just starting out with Java.
echo "<h2>Compiling $TM_FILENAME</h2>"
cd "$TM_DIRECTORY"
javac -encoding UTF8 "$TM_FILENAME" &> >("${TM_RUBY:-ruby}" -rtm_parser -eTextMate.parse_errors)
if (($? >= 1)); then exit; fi

export CURRENT_DIR=`pwd | sed -n 's/.*\///p'`

{ java -Dfile.encoding=utf-8 $CURRENT_DIR."${TM_FILENAME%.java}" echo -e "\nProgram exited with status $?."; }|pre

This post was just an illustration of how to make compilation in TextMate for Java more useable, in the next post I will be adding some new snippets, the ones that are missing from the original TextMate bundle.

Update: I have now (finally) expanded on this post, you can read about more changes here.

TextMate, Java & Compiling [Updated]