Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Alarm Clock (How to figure out how much time left till alarm will sound)

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Alarm Clock (How to figure out how much time left till alarm will sound)

    I'm very new to Java, and have no idea on what to do to get the return of how many hours and minutes are left till the alarm sounds from what the user input.

    This is the code I have now:

    // Sean Kilbane
     
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Calendar;
     
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
     
    public class AlarmClock {
     
      public static void main(String[] args) throws MalformedURLException {
     
    		int retry = JOptionPane.YES_NO_OPTION;
     
    		JOptionPane.showMessageDialog(null, "Listing 4.14 P. 163\nAlarm Clock", "Information", JOptionPane.INFORMATION_MESSAGE);
     
    		while (retry == JOptionPane.YES_NO_OPTION) {
     
    			// Asking user for time
    			int hours = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the hours \nExample: 8"));
    			int minutes = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the minutes \nExample: 36"));
    			String prefix = JOptionPane.showInputDialog(null, "Enter AM or PM");
     
    			// Asking the user for date
    			int year = Integer.parseInt(JOptionPane.showInputDialog(null, "Pelase enter the year \nExample: 1992"));
    			int month = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the month \nExample: 02"));
    			int day = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the date \nExample: 26"));
     
     
    			Calendar calendar = Calendar.getInstance();
    			calendar.set(year, month, day, hours, minutes);
    			//System.out.println(calendar.get(Calendar.DAY_OF_MONTH + Calendar.DAY_OF_YEAR + Calendar.MONTH));
     
     
     
     
    			long time = System.currentTimeMillis();
    			long totalSeconds = time / 1000;
    			long currentSecond = totalSeconds % 60;
    			long totalMinutes = totalSeconds / 60;
    			long currentMinute = totalMinutes % 60;
    			long totalHours = totalMinutes / 60;
    			long currentHour = totalHours % 24;
    			System.out.println(currentHour);
     
     
     
     
    			// Reporting the time left till the alarm
     
     
    			// Asking the user if they like to set up another alarm
    			retry = JOptionPane.showConfirmDialog(null, "Would you like to set another alarm?", "Question", JOptionPane.YES_NO_OPTION);
     
    		}
    			// Good Bye Message
    		final ImageIcon icon = new ImageIcon(new URL("URL IS HERE"));
    		JOptionPane.showMessageDialog(null, "Thanks for using the Alarm Clock Program!", "Good Bye", JOptionPane.INFORMATION_MESSAGE, icon);
    	}
    }

    This is what the requirements left I need. Any help is grateful!

    Create a Calendar object with the time of the alarm.
    Check to see if the alarm will go off today or tomorrow or another day, or when? Use the Calendar.Field Constants. Calculate how many hours and minutes will elapse until the alarm sounds.
    Report the time to the alarm sounding in hours and minutes.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Alarm Clock (How to figure out how much time left till alarm will sound)

    Your requirements pretty much spell out exactly what you need to do. What exactly are you confused about, specifically?

    --- Update ---

    Nevermind, looks like I'm wasting my time.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/81399-alarm-clock-help.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    jps (September 7th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Alarm Clock (How to figure out how much time left till alarm will sound)

    Yes I'm aware of what I need to do, but I don't have a clue how to approach it. I been looking for hours on how to get this going. That's why I'm here.

  5. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Alarm Clock (How to figure out how much time left till alarm will sound)

    can't you get remaining time by subtracting alarm time and current time?

Similar Threads

  1. Get input from the user till he type quit
    By felixb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 27th, 2013, 07:50 AM
  2. Ill keep asking till i get an answer
    By iamgonge in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2013, 08:58 AM
  3. Playing sound each time mouse is clicked (not working)
    By BestSanchez in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 24th, 2012, 09:51 PM
  4. Replies: 0
    Last Post: January 12th, 2012, 02:54 PM
  5. Replies: 1
    Last Post: September 15th, 2011, 02:38 PM