O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Program: Reminder Service

The ReminderService program provides a simple reminder service. The load( ) method reads a plain text file containing a list of appointments like the ones shown here, using a SimpleDateFormat:

1999 07 17 10 30 Get some sleep.
1999 07 18 01 27 Finish this program 
1999 07 18 01 29 Document this program

Example 6-4 shows the full program.

Example 6-4. ReminderService.java

import java.io.*; import java.text.*; import java.util.*; import javax.swing.*; /** * Read a file of reminders, sleep until each is due, beep. */ public class ReminderService { class Item { Date due; String message; Item(Date d, String m) { due = d; message = m; } } ArrayList l = new ArrayList( ); public static void main(String argv[]) throws IOException { ReminderService rs = new ReminderService( ); rs.load( ); rs.run( ); } protected void load( ) throws IOException { BufferedReader is = new BufferedReader( new FileReader("ReminderService.txt")); SimpleDateFormat formatter = new SimpleDateFormat ("yyyy MM dd hh mm"); String aLine; while ((aLine = is.readLine( )) != null) { ParsePosition pp = new ParsePosition(0); Date date = formatter.parse(aLine, pp); if (date == null) { message("Invalid date in " + aLine); continue; } String mesg = aLine.substring(pp.getIndex( )); l.add(new Item(date, mesg)); } } public void run( ) { System.out.println("ReminderService: Starting at " + new Date( )); while (!l.isEmpty( )) { Date d = new Date( ); Item i = (Item)l.get(0); long interval = i.due.getTime() - ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required