Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Use the value of Date in "if" statement

  1. #1
    Jatin.Shetty is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Use the value of Date in "if" statement

    Hi,

    I am new to java so my question might come across as discrete or random :D

    anyways, I was wondering how to use the current date value in the if statement.

    For example, i retrieve the current date using System.currentTimeMillis().

    Now using this date value that i get, i wana use an if statement or some kinda statement where if its before 12 noon, i sud print "Good Mornin" and if its after 12 it sud print "good afternoon".

    Basically i jus want to know how to compare the date value.. In an if statement.

    Sorry if the question sounds silly :D


    This is what i have done so far.


    Java Code:
    import java.util.*;
    
    
    
    public class Time {
        
        public static void main(String[] args){
            
            Date d=new Date(System.currentTimeMillis());
    
            //System.out.println(d);
            
            Time t=new Time();
            
            t.greet(d);
            
            
        }
        
        public void greet(Date d) {
    
            // This is were i want the "if" statement to come in
    
        
            //System.out.println(d);
            
            
        
        }
                
        }
    Last edited by Jatin.Shetty; 07-16-2013 at 08:56 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Use the value of Date in "if" statement

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Jatin.Shetty is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Use the value of Date in "if" statement

    Done!! And thanks!!

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,674
    Rep Power
    5

    Default Re: Use the value of Date in "if" statement

    In stead of Date, I would investigate the Calendar class in stead.

    Calendar (Java Platform SE 7 )

    PS: you only need new Date() to get a date for the current date and time.
    Jatin.Shetty likes this.

  5. #5
    Jatin.Shetty is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Use the value of Date in "if" statement

    gimbal2,

    Thank you for the suggestion, used the Calendar class and got what i wanted.

    Herez wat i did, and can some1 temme if ther is any other shorter way of doing what i just did?

    All these questions and codes are for my learning purpose :) jus trying to understand how Java works.

    Thanks in advance.

    Java Code:
    
    import java.util.Calendar;
     
    public class Time {
     
        public static void main(String[] args) {
    
            Calendar c = Calendar.getInstance();
            
         if(12 <= c.get(Calendar.HOUR_OF_DAY) && 18 >= c.get(Calendar.HOUR_OF_DAY))
            
            	System.out.println("Afternoon :) ");
            
               else {
            	
            	if(18 <= c.get(Calendar.HOUR_OF_DAY) && 24 >= c.get(Calendar.HOUR_OF_DAY)){
            		
            		System.out.println("Evening :) ");
            }
            	
            	else{
            		System.out.println("Morning :)");
            	}
            
        }
    }
    }

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,674
    Rep Power
    5

    Default Re: Use the value of Date in "if" statement

    Quote Originally Posted by Jatin.Shetty View Post
    temme if ther is any other shorter way of doing what i just did?
    Why do you care about that? I would focus on writing code YOU understand for now.

    So how could you make the code a little more readable? Well you could capture the hour in a variable.

    Java Code:
    Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    
    if(hour >= 12 && hour < 18){
    ...
    You may also want to check your if-else structure again. Is it really necessary that the second if is INSIDE the first if?

    Finally: it would be more logical to check for morning first, afternoon second and evening last, wouldn't you think? Then the code follows the flow of the day.

  7. #7
    Jatin.Shetty is offline Member
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: Use the value of Date in "if" statement

    super... and about the order, was just pushing things around and checking :D so thats y Morning came in the end :D

    And the "capturing value in a variable" thing.... Thanks.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  4. Replies: 3
    Last Post: 05-25-2009, 08:50 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •