Timestamping On An Class Object, Check To See If Date Is Valid

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 641 Views - Last Post: 01 March 2013 - 01:21 PM Rate Topic: -----

#1 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Timestamping On An Class Object, Check To See If Date Is Valid

Posted 26 February 2013 - 02:54 PM

Hi guys,

i have a class which is an ID Card System, on the cards i have a variable which accepts a date as a string in the format "dd/mm/yyyy", This works fine and gets passed through as a string.

On the ID now these are only active for some days so i have a method called isTimeValid, which will extract this date and check to see if the date on the card is valid with real time data like the time now.

Heres My Code:

public void isTimeValid()
    {

        Calendar dateTime = Calendar.getInstance();
        String dateStr = this.dateValid;
        DateFormat formatter;
        Date date;

        formatter = new SimpleDateFormat("dd-MM-yy");
        date = (Date) formatter.parse(dateStr);

        if (date.equals(dateTime))
        {
            System.out.println("Card Is Valid");
        } else
        {
            System.out.println("Card Not Valid");
        }

    }


This doesn't work, for me what am i doing incorrect?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Timestamping On An Class Object, Check To See If Date Is Valid

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2779
  • View blog
  • Posts: 11,764
  • Joined: 20-September 08

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 26 February 2013 - 03:09 PM

You're using the wrong date format. Look at the pattern you talk about then at the pattern in your code. A tip: get used to using ISO date format - they are the best (yyyy-MM-dd)
Was This Post Helpful? 0
  • +
  • -

#3 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 01:05 AM

View Postg00se, on 26 February 2013 - 03:09 PM, said:

You're using the wrong date format. Look at the pattern you talk about then at the pattern in your code. A tip: get used to using ISO date format - they are the best (yyyy-MM-dd)


It states That theres an error in this part:

date = (Date) formatter.parse(dateStr);


"Unrepoted exeption ParseExeption must be declared or thrown"
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2779
  • View blog
  • Posts: 11,764
  • Joined: 20-September 08

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 03:59 AM

Quote

date = (Date) formatter.parse(dateStr);

There's no need for a typecast there.

Yes, that method throws an exception, which must be caught. If you're unsure about exceptions, take a Java tutorial about exceptions
Was This Post Helpful? 0
  • +
  • -

#5 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 11:44 AM

On netbeans it states it can add a try catch statements on it this prints put an exeption all the time, Why doent the conversion work thats what im having trouble with.

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2779
  • View blog
  • Posts: 11,764
  • Joined: 20-September 08

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 11:51 AM

Please post current code
Was This Post Helpful? 0
  • +
  • -

#7 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 12:26 PM

    public void isValid()
    {
        try
        {
            Calendar dateTime = Calendar.getInstance();
            String dateStr = this.dateValid;
            DateFormat formatter;
            Date date;

            formatter = new SimpleDateFormat("yyyy-MM-dd");
            date = (Date) formatter.parse(dateStr);

            if (date.equals(dateTime))
            {
                System.out.println("Visitor ID Card Is Valid");
            } else
            {
                System.out.println("Card Not Valid");
            }
        } catch (ParseException ex)
        {
            Logger.getLogger(VisitorIDCard.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3618
  • View blog
  • Posts: 12,465
  • Joined: 12-December 12

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 01:21 PM

A problem is that formatter.parse() returns a string DateFormat that needs to be cast to a date (if required). To compare dates you need two date instances.

The following code I found may help:

Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date;
        try {
            date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00");
            formatter = new SimpleDateFormat("yyyy-MM-dd");
            String s = formatter.format(date);
            System.out.println(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }


SO reference

This post has been edited by andrewsw: 27 February 2013 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2779
  • View blog
  • Posts: 11,764
  • Joined: 20-September 08

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 01:24 PM

Quote

 if (date.equals(dateTime))
is your problem. A Calendar will never equal a Date. You need
if (date.equals(dateTime).getTime())

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 02:06 PM

View Postandrewsw, on 27 February 2013 - 03:21 PM, said:

DateFormat that needs to be cast to a date



            date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00");
        


As DateFormat extends java.text.Format wich extends java.lan.Object

how can you cast a DateFormat into a Date ???
Was This Post Helpful? 0
  • +
  • -

#11 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 02:16 PM

How would i get it to check the date are valid though?
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3618
  • View blog
  • Posts: 12,465
  • Joined: 12-December 12

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 03:05 PM

View Postpbl, on 27 February 2013 - 02:06 PM, said:

View Postandrewsw, on 27 February 2013 - 03:21 PM, said:

DateFormat that needs to be cast to a date



            date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00");
        


As DateFormat extends java.text.Format wich extends java.lan.Object

how can you cast a DateFormat into a Date ???

That code is from the SO link that I provided. However, I have run it and it works. It is not casting a DateFormat to a Date, it is casting the formatter to a DateFormat.
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3618
  • View blog
  • Posts: 12,465
  • Joined: 12-December 12

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 03:38 PM

Well this works for me, using getTime()

    public void isTimeValid() {

        Calendar dateTime = Calendar.getInstance();
        Date currDate = dateTime.getTime();
        String dateStr = "27/02/2013";
        DateFormat formatter;
        Date date;

        formatter = new SimpleDateFormat("dd/MM/yyyy");
        try {
            date = (Date) ((DateFormat) formatter).parse(dateStr);
        } catch (ParseException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }

        if (date.before(currDate))
        {
            System.out.println("Card Is Valid");
        } else
        {
            System.out.println("Card Not Valid");
        }

    }

but only if I use before() rather than equals(), because getTime() includes a time element.

If you need to use equals() instead then you'll need to remove the time element (or use another approach).

This post has been edited by andrewsw: 27 February 2013 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2779
  • View blog
  • Posts: 11,764
  • Joined: 20-September 08

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 27 February 2013 - 05:32 PM

Don't describe the code you think you need or the steps. Describe the GOAL

If your goal is to determine if 'dateValid' is today, try the following

    static boolean isValid(String dateValid) {
      return dateValid.equals(new java.sql.Date(System.currentTimeMillis()).toString());
    }


This post has been edited by g00se: 27 February 2013 - 05:33 PM
Reason for edit:: format

Was This Post Helpful? 0
  • +
  • -

#15 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Timestamping On An Class Object, Check To See If Date Is Valid

Posted 28 February 2013 - 01:45 AM

View Postg00se, on 27 February 2013 - 05:32 PM, said:

Don't describe the code you think you need or the steps. Describe the GOAL

If your goal is to determine if 'dateValid' is today, try the following

    static boolean isValid(String dateValid) {
      return dateValid.equals(new java.sql.Date(System.currentTimeMillis()).toString());
    }



With this one it needs the dateValid variable passed in, But on my IDCardTester class there is no variable called dateValid, This is only on the IDCard Class it self
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2