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

Thread: java calendar

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java calendar

    Ive made an IDCard class with a subclass called VisitorID.
    The visitor ID is only valid for a certain date so ive created a method to check this. It was working for me but now it isnt and i cant see what ive done wrong.

    Below is the method, when i call this method from my test class it returns ID card is not valid regardless of if its a correct or incorrect date. "dateValid" is the date string input when creating the Visitor ID card and is in the format dd/mm/yyyy.
    public void isValid()
        {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            if(sdf.format(cal.getTime()).equals(dateValid))
            {
                System.out.println("Visitor ID Card is Valid");
            }
            else
                System.out.println("Visitor ID Card is not valid");
        }

    Any help appreciated


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: java calendar

    The problem is that your id card is going invalid within one millisecond. [Wow!] getTime returns the long in milliseconds from 1990. [I think], you should be checking that the end date of the id is greater than the current date.

    In psuedo code
    void isValid
      thetime = current time in milliseconds
      if thetime is greater than end time
        card is not valid
      else
        card is valid

    Edit: from Calender getTime:
    "Returns a Date object representing this Calendar's time value"

    You are actually cheking the object that Calendar returns, which is not going to ever return true. You could use any of these methods [Depending on the rest of your program] for getting the time in milliseconds

    Calendar.getTimeInMillis()
    System.currentTimeMillis()
    Calendar.getTime().getTime()

    or to get the day that the Calendar 'cal' was created in this month:
    cal.get(Calendar.DAY_OF_MONTH)
    Year:
    cal.get(Calendar.YEAR)
    Whatever looks best to you.
    Last edited by Tjstretch; March 8th, 2012 at 03:41 PM.

Similar Threads

  1. [SOLVED] Weird calendar.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 3rd, 2011, 08:19 PM
  2. Calendar date
    By joshft91 in forum Object Oriented Programming
    Replies: 11
    Last Post: November 6th, 2010, 02:47 AM
  3. Calendar help
    By moonieass13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 31st, 2009, 12:43 AM
  4. calendar
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: September 29th, 2009, 11:02 PM
  5. which calendar to use?
    By rptech in forum AWT / Java Swing
    Replies: 6
    Last Post: August 30th, 2009, 03:18 PM