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 6 of 6

Thread: Help with time

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Help with time

    I am trying to make something like an alarm and it goes like this
        while(run){
            System.out.println(temp.getTime());
            if(temp.getTime()=="11:52"){
    	    run = false;
    	    JFrame frame = new JFrame("Alarm");
    	    frame.add(new Screen());
    	    frame.setSize(1280, 755);
    	    frame.setLocationRelativeTo(null);
    	    frame.setVisible(true);
    	    frame.setAlwaysOnTop(true);
    	    frame.setResizable(false);
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
        }

    So when it is 11:52, it doesn't show up.
    And when its 11:52, the console is still showing 11:51 however when i run it with only System.out.println(temp.getTime); again and again it changes. What can i do?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help with time

    My guess is that temp.getTime() returns a String, right?
    To compare Strings (and other objects) for equivalency, you need to use the .equals() method. So, instead of:
    if(temp.getTime()=="11:52"){
    you would need:
    if(temp.getTime().equals("11:52"){
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Help with time

    The output is 11:51 some hundred thousand times and when time changes on computer to 11:52, its still 11:51.
    It has to go like 11:51 thousand times and when time changes on computer to 11:52, it should print 11:52 one time and show the window.
    Here is the getTime() method
    public String getTime(){
        return getHoursMinutes();
    } 
    // and here is getHoursMinutes()
                private String getHoursMinutes(){
    		String time;
    		if((date.getHours()-3)<12&&(date.getHours()-3)>0){
    			if((date.getMinutes()+30)<60){
    				time = (date.getHours()-3)+":"+(date.getMinutes()+30);
    				return time;
    			}else{
    				time = (date.getHours()-2)+":"+((date.getMinutes()+30)-60);
    				return time;
    			}
    		}else{
    			if((date.getMinutes()+30)<60){
    				time = (date.getHours()-3)-12+":"+(date.getMinutes()+30);
    				return time;
    			}else{
    				time = (date.getHours()-2)-12+":"+((date.getMinutes()+30)-60);
    				return time;
    			}
    		}
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with time

    Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Help with time

    i got the problem a while ago but cause of that while loop i had the pc crash with like 104486528745647560 windows but i got it now

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with time

    Glad you got it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Time taken for execution is more
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2011, 09:48 AM
  2. Run Time Error.
    By seymour001 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 10th, 2011, 12:10 PM
  3. First Time
    By rayoung17 in forum Member Introductions
    Replies: 1
    Last Post: April 4th, 2011, 01:52 PM
  4. First time in forum
    By prasanth1216 in forum Member Introductions
    Replies: 0
    Last Post: December 19th, 2010, 01:45 PM
  5. Using time
    By ellias2007 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 4th, 2010, 08:48 AM

Tags for this Thread