Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Date class in an IF statement

    This is my code:

    Java Code:
    import java.util.*;
    import java.io.*;
    import type.lib.GlobalCredit;
    import type.lib.CreditCard;
    import java.text.SimpleDateFormat;
    
    public class eCheck08A
    {
        public static void main(String[] args)
        {
            PrintStream out = System.out;
            Scanner in = new Scanner(System.in);
    
            GlobalCredit credit1 = new GlobalCredit().getRandom();
    
            out.print("Enter report range in years ... ");
            int range = in.nextInt();
            out.println("Cards expiring before " + range + " year(s) from now: ");
    
            SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
    
            for (CreditCard cc : credit1)
            {
                out.print(cc.getNumber());
                out.println("\t" + sf.format(cc.getExpiryDate()));
            }   
        }
    }
    At first, my app asks for Range years, lets say we enter "3". I want to put a " * " beside my date of any Year that is below the range number. Im assuming Java assumes that the date is as of today, so July 24, 2012. So for example, if we get 16/04/2013, I want a "*" beside it. But if we get 23/05/2016, no "*" to appear. I know we got to create a IF statement in my FOR statement somehow, Been stuck on this for days now...:(

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Date class in an IF statement

    you can use the compareTo method for this
    Java Code:
    if (oldDate.compareTo(newDate) < 0) {
    //OLD DATE IS LESS THEN NEW DATE
    }
    Or you can use the Date class' .before method
    here is the link to the Date API

  3. #3
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Date class in an IF statement

    yes i already know that. what i dont know, is how to create my newDate

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Date class in an IF statement

    What does this return?

    Java Code:
    cc.getExpiryDate()
    Wouldn't your newDate be the return of the above statement? I'm a bit confused about where you are getting the date to compare to the current date.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Date class in an IF statement

    Quote Originally Posted by shahin912 View Post
    yes i already know that. what i dont know, is how to create my newDate
    What is the newDate? Do you want to get the current date? Or create a Date object for a specific date/month/year? What is it?

  6. #6
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Date class in an IF statement

    Java Code:
    GlobalCredit credit1 = new GlobalCredit().getRandom();
    This produces some 'random' creditcard.


    Java Code:
    cc.getExpiryDate()
    returns a random expiry date, for example, "06/02/2015".


    The user enters a number at the beginning, as 'range'. Let say we enter '3'. So i want every year that is between 2012-2015 to have a '*' beside it. If we enter '5', I want every year that is between 2012-2017 to have '*' beside it.

    Im not really sure how to start this, do I have to create one oldDate & one newDate?

    Im assuming oldDate has to be 2012 (present), and newDate has to be 2012 + range?

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Date class in an IF statement

    You can just use the Date's before() and equals() method to check if the random date is before or equal to "current date + range", as jhuber151 suggested.

    Quote Originally Posted by jhuber151 View Post
    Or you can use the Date class' .before method
    here is the link to the Date API
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  8. #8
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Date class in an IF statement

    i cant add a Date to an Int.

    how do i add for example oldDate + Range.?

  9. #9
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Date class in an IF statement

    Well, I was going to point you to methods in Date that allow you to set the year, but it appears that most of the methods in the Date class are deprecated

    The Java API suggests that you actually use the Calendar class which does have methods for setting the year. Calendar has a before() method as well.

    Reference: Calendar (Java Platform SE 7 )
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  10. #10
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Date class in an IF statement

    uh man..this is so confusing. anyone else here can help me please?

  11. #11
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Date class in an IF statement

    Quote Originally Posted by shahin912 View Post
    uh man..this is so confusing.
    Can you explain what you are confused about? I'll try to clarify anything if I was unclear.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  12. #12
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Date class in an IF statement

    basically the whole thing. i did all of the code except this part.

    the if statement is really hard to make. and relating 'range' and oldDate and newDate is pretty confusing.

  13. #13
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Date class in an IF statement

    If you are confused about the syntax of an if-statement, then this will help: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    For "current date plus range" and the randomly-generated date, I suggest that you make 2 Calendar objects. Set 1 Calendar to the current date plus the range in years, and set the other to the randomly-generated date. Information on the constructor for a Calendar object and the set() method can be found here: Calendar (Java Platform SE 7 )

    Once you have the two objects, use the before() method to test if the randomly-generated date is before the "current date plus range".

    If you have read the links that I provided and you still have questions, I can help you out.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  14. #14
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Date class in an IF statement

    I got to add whatever the randomly-generated date is to the range.

    i havent used Calendar class before, what does "protected" mean? I cant use

    Calendar x1 = new Calendar()

  15. #15
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Date class in an IF statement

    1. Don't you need to add the range to the current date? Since the range is in years, you just have to add the range to the value you pass in as year when you call the set() method.

    2. Protected is an access-control tag which means that the member can be accessed by classes in the same package or in a subclass.

    3. Since Calender is an abstract class, you need to extend it for your purposes. You could make a subclass called ExpirationDate or something like that. You can't create an instance of an abstract class.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  16. #16
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Date class in an IF statement

    ok. I got all the calendar part right.

    this is my code right now

    Java Code:
    import java.util.*;
    import java.io.*;
    import type.lib.GlobalCredit;
    import type.lib.CreditCard;
    import java.text.SimpleDateFormat;
    
    
    public class eCheck08A
    
    {
    	public static void main(String[] args)
    	
    	{
    		PrintStream out = System.out;
    		Scanner in = new Scanner(System.in);
    		
    		GlobalCredit credit1 = new GlobalCredit().getRandom();
    		
    		out.print("Enter report range in years ... ");
    		int range = in.nextInt();
    		out.println("Cards expiring before " + range + " year(s) from now: ");
    		
    		SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
    		 
    		for (CreditCard cc : credit1)
    		{
    		
    		Calendar c = Calendar.getInstance();
    		c.setTime(cc.getExpiryDate());
    		c.add(Calendar.YEAR, range);
    		Date newDate = c.getTime();
    		
    			if (cc.getExpiryDate().compareTo(newDate) < range)
    			{
    				if(cc.getExpiryDate().compareTo(newDate) > range)
    				{
    					out.print("*");
    				}
    				out.print(cc.getNumber());
    				out.println("\t" + sf.format(cc.getExpiryDate()));
    
    			}
    		}
    		
    		
    	
    	}
    }
    Now in the IF statements, if I want to have stars if the year is between cc.getExpiryDate() and newDate, how do i construct the IF statement. The IF statement i have right now, isnt working.

  17. #17
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Date class in an IF statement

    You can simply get the year value from your cc.getExpiryDate() and then check to see if it is between the current year and current year + range. You can use the Calender object to get the year value from a Date.

  18. #18
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Date class in an IF statement

    First, I suggest that you simply use the Calendar's set(int year, int month, int date) method. For the "current date plus range", just add the range to the value that you input as year. You will have to extend Calendar, but it will ultimately pay off in the end.

    Once you have your 2 Calendar objects, all you have to do in order to test if a date comes before another date is use the before() method. The before() method returns a boolean value, and this boolean value can function as the test for your if-statement. The body of your if-statement can then add the star.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  19. #19
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Date class in an IF statement

    im sorry, but i just got lost on what you just said.

    'newDate' already includes cc.ExpiryDate() + Range. thats my new Date.
    so confusing...


    This is output that I want: an example:

    Enter report range in years ... 3
    Cards expiring before 3 years from now:

    561561-8 20/11/2015
    045645-7 22/02/2017
    456462-3 16/04/2013 *
    546548-5 19/08/2016


    The current year is 2012
    The person enters '3' as range.
    so any year that is from 2012-2015 should have a "*". Like the output above, 2013 has a "*".
    now making an IF statement for that is a miracle.

  20. #20
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Date class in an IF statement

    I'm suggesting that you remove all of your statements that have to do with Calendar. Then,

    1. Create a subclass of Calendar. Let's pretend that you name it "ExpirationDate."

    2. Create 2 ExpirationDate objects using its default constructor. Let's say that you name 1 date1 and the other date2 (but you should use more descriptive names.)

    3. Using the set(int year, int month, int date) method, set date1 as the random date.

    4. Using the set(int year, int month, int date) method, set date2 as x years after the current date. x is the range.

    5. Create an if-else statement. In the body of the if-statement, put what you want to happen if the random date is within the range. In the body of the else-statement, put what you want to happen if the random date is not within the range.

    6. As the test of if-statement, put this:

    Java Code:
    date1.before(date2) || date1.equals(date2)
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help using date class
    By enigmaenigma in forum New To Java
    Replies: 1
    Last Post: 05-01-2012, 01:14 AM
  2. SQL Prepared Statement with date
    By Dcalladi in forum New To Java
    Replies: 3
    Last Post: 02-18-2012, 03:37 PM
  3. Date() class to get current date
    By bigmac15 in forum New To Java
    Replies: 1
    Last Post: 06-28-2011, 09:17 PM
  4. Help creating a Date class
    By Meta in forum New To Java
    Replies: 17
    Last Post: 04-30-2010, 09:28 AM
  5. problem with date class
    By gabriel in forum New To Java
    Replies: 3
    Last Post: 08-03-2007, 01:28 PM

Tags for this Thread

Posting Permissions

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