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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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:59 AM

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

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).



Ahh thanks this helps, how would I get it to check if its equals to todays date?
Was This Post Helpful? 0
  • +
  • -

#17 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 01 March 2013 - 11:37 AM

Ive tried everything i can think of to get the .equals to work, does anyone have a suggestion?
Was This Post Helpful? 0
  • +
  • -

#18 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3478
  • View blog
  • Posts: 11,818
  • Joined: 12-December 12

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

Posted 01 March 2013 - 11:48 AM

View Postparry2411, on 01 March 2013 - 11:37 AM, said:

Ive tried everything i can think of to get the .equals to work, does anyone have a suggestion?

You can either use date formatters to compare two string versions of the dates; that is, the same date-formats but without the time element. Or you might try the following to strip the time from today's date, before comparing them:

import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static Date removeTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}

SO discussion

I think I might prefer the first option, as I feel I would have more control over the process.

View Postparry2411, on 01 March 2013 - 11:37 AM, said:

Ive tried everything i can think of to get the .equals to work, does anyone have a suggestion?

I'm not sure about this statement. I just spend two minutes Googling to find an explanation, and the above code, so I don't understand what you can have been trying.
Was This Post Helpful? 0
  • +
  • -

#19 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,443
  • Joined: 20-September 08

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

Posted 01 March 2013 - 12:22 PM

Quote

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
Sorry, i don't understand your point. You have access to 'dateValid' in the code you posted. You can either pass it in (better practice) or not, as you wish
Was This Post Helpful? 0
  • +
  • -

#20 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 01 March 2013 - 01:21 PM

Thanks for the help guys, Got it in the end
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2