Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help:covt date and return as string

    I hav question relating to JAVA programming. I tried to solve it by my self, but did'nt got it fully correct. So if you can, plz help me. The question is given below:



    Write a static Java method named convertDate that takes THREE parameters representing the date, month and year.
    The method should first validate the date and month eg. date should be from 1-31 for Jan, March, May, July, August, October and December and ranges from 1- 30 for the other months with the exception of Feb. If a year can be divided by 4, the month of Feb will have 29 days instead of 28 days.

    Then convert and return the date as a string in the following format: DD/MM/YYYY short eg. 11/05/2002.
    If any of the parameters are invalid the value “Invalid” should be returned.

    Above is question which i am trying to solve. I have solved half of it which i am giving below,

    class Dates
    {

    String date="MM/DD/YYYY";
    public static boolean covertDate( int day,int month,int year)
    {
    switch(month)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    if(day<=31)
    return true;
    case 4: case 6: case 9: case 11:
    if (day<=30)
    return true;
    case 2:
    if (day<=28)
    return true;
    default:
    return false;
    } // end switch
    } // end main
    } // end class dates

    Can anyone help me to solve this program. thanks in advance

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    You are in the wrong section.

    This area of the site is for JavaScript, not Java.
    LovesWar

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    out of curiousity though.........

    ........why do you have 3 parameters, when it looks from your script that you will only be using one of your 3?

    Since you are declaring your method as a boolean, you will only get a true, or false result. Secondly, do you know the difference between a switch statement, and an if statement? If you want a variable that is a boolean, you will need to declare it as such in your method, and then apply the conditions underwhich the method is supposed to return the appropriate value. I do not see from your above that you have even declared your variable.

    It's been a while since i have done Java, but if i had to take a guess, your method should look something like this,

    Code:
    class Dates 
    { 
    
    String date="MM/DD/YYYY";
    public static boolean covertDate(int month )
    {
    
    private boolean theValue=true;
      {
      if( month <=31)
      return theValue ;
      else if ( month <=30)
      return theValue ;
      else if ( month <=28)
      return theValue ;
      else
      return !theValue;
      } // end main
    } // end class dates
    I think thats how it would have to be.
    LovesWar


  •  

    Posting Permissions

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