I created a class earlier in the season which would latch the output state of a solenoid for a specified duration.
After the duration has elapsed, the output is de-energized.
This implementation makes use of the TimerTask class.
It should be pretty easy to modify this class for your own use.
https://gist.github.com/jcorcoran/7b1ffd41391144ea8df1
I've tried to comment the code sufficiently, but if you have any questions feel free to contact me.