×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    36

    Regarding datefiled

    Hi All,

    I am reading the datefield into a Date object. But then I want to get the day, month (numeric form) and years separately.

    So I am doing this -
    -------------------------
    int day, month, year;

    Date dt = dateField.getDate ();

    /* Here I want to get day, month and year out of dt. */
    ----------------------------

    Could you please tell me how to do that?

    Thanks in advance
    asahlot

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    36

    Re: Regarding datefiled

    Guys it is sort of urgent. Could someone plz suggest something quickly?

  3. #3
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Regarding datefiled

    Hi,

    You could use Calendar to do that. Something like this:
    Code:
    Calendar c=Calendar.getInstance();
    c.setTime(dt);
    int day=c.get(Calendar.DAY_OF_WEEK);
    int month=c.get(Calendar.MONTH);
    int year=c.get(Calendar.YEAR);

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    36

    Re: Regarding datefiled

    I tried your code but it gives a different date from what dt gives.
    Here is the code:
    --------------------
    DateField dateField = new DateField ("Enter Date", DateField.DATE);

    Date dt = dateField.getDate ();
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);

    int day = cal.get(Calendar.DAY_OF_WEEK);
    int month = cal.get(Calendar.MONTH);
    int year = cal.get(Calendar.YEAR);

    System.out.println("date on form:" + dt.toString ());
    System.out.println ("gettime:" + dt.getTime ());
    System.out.println ("Reading date aand about to print");
    System.out.println ("day:" + day + "\n month: " + month + "\n year: " + year);
    ---------------------

    Here is the output:
    ---------------------
    date on form:Wed Oct 01 00:00:00 GMT-05:00 2008
    gettime:1222837200000
    Reading date aand about to print
    day:4
    month: 9
    year: 2008
    ----------

    Even if I comment out the line:
    cal.setTime(dt);
    I get the same output so in a way I think this line is not effective.

    What could be wrong?

    Thanks

Posting Permissions

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