Results 1 to 2 of 2
  1. #1
    aappee is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default Get the day of the week & month in word from constructor into main with input

    Hi

    i need to modify this code to fit the requirement.....any help and sample would be appreciated..
    For this program, i need to: take in day, month,year of a date as int argument, return the day of the week for the date entered in App()

    In Main: call the App() to get the day of the week to display when user enter the date in dd/mm/yyyy
    StringTokenizer is required for this program.

    - How can the arguments pass back to main by returning of month & day of the week?
    - Program cant take in int and return as string
    - Date cant display day of the week

    Program output: 29 Aug 2010 is a Sunday

    Java Code:
    import java.util.*;   
        
    public class App2 {   
           
        private int day;   
        private int month;   
        private int year;   
           
            private int inputDay;   
            private int inputMonth;   
            private int inputYear;   
           
        public static String App2(String day2, String month2, String year2) { // this is wrong should pass int in   
           
            String date = day2 + month2 + year2;   
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");      
                          
            try  
            {   
            Date today = df.parse(date);                   
            System.out.println("Today = " + df.format(today));    
            }   
            catch(ParseException e)   
            {   
            }   
                   
              if(month2.equals("01") || month2.equals("1")){month2 = "January";}                  
              else if(month2.equals("02") || month2.equals("2")){month2 = "February";}                  
              else if(month2.equals("03") || month2.equals("3")){month2 = "March";}                  
              else if(month2.equals("04") || month2.equals("4")){month2 = "April";}                  
              else if(month2.equals("05") || month2.equals("5")){month2 = "May";}                  
              else if(month2.equals("06") || month2.equals("6")){month2 = "June";}                  
              else if(month2.equals("07") || month2.equals("7")){month2 = "July";}                  
              else if(month2.equals("08") || month2.equals("8")){month2 = "August";}                  
              else if(month2.equals("09") || month2.equals("9")){month2 = "September";}                  
              else if(month2.equals("10")){month2 = "October";}                  
              else if(month2.equals("11")){month2 = "November";}                  
              else if(month2.equals("12")){month2 = "December";}   
                 
            return month2;   // can only return month, how abt day of week?
        }   
        public static void main (String [ ] args){   
            Scanner sc = new Scanner(System.in);   
            System.out.print("Enter the date in dd/mm/yyyy: ");   
            String date = sc.nextLine();   
            StringTokenizer st = new StringTokenizer(date, "/");    
            String day = st.nextToken();   
            String month = st.nextToken();   
            String year = st.nextToken();   
            App2(day, month, year);   
            String test = App2(day, month, year);   
                System.out.print(test);   //can get month Aug
        }   
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    First, to get the day of the week, create a second SimpleDateFormat and use the E format to get the day of the week. (See SimpleDateFormat (Java 2 Platform SE v1.4.2).)

    Second, to pass them as integers, you'll want to take a look at Integer.parseInt (Java 2 Platform SE v1.4.2).

    Third, you only need to call App2(day,month,year) once (the second one is correct); you call it twice. If this is a school assignment you may be marked down for calling it twice.

Similar Threads

  1. No main method, constructor/object not working
    By sinista in forum New To Java
    Replies: 9
    Last Post: 10-14-2009, 03:25 PM
  2. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 06:23 AM
  3. calendar with week numbers of a current month
    By sridharnr in forum Java Servlet
    Replies: 1
    Last Post: 03-27-2009, 12:02 PM
  4. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 07:24 PM
  5. Replies: 0
    Last Post: 12-19-2007, 10:10 AM

Posting Permissions

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