Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: DATE.java!

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DATE.java!

    It compiles fine if the user inputs mm/dd/yyyy, but if they input m/d/yyyy it wont work. Any ideas? I am going to upload the directions that I was given. Code is all done just a couple errors : (

    Thanks in advance!
    Joey

    import java.util.Scanner;
     
    public class Activity2 {
      public static void main(String[]args) {
     
    Scanner sc = new Scanner(System.in);
    String date, year;
    int day, month;
    boolean leapYear;
    System.out.println("Please enter a date in the format of mm/dd/yyyy.");
    date = sc.nextLine();
    day= Integer.parseInt(formatDay(date));
    month= flag(formatMonth(date));
    year=formatYear(date);
    leapYear=leapYear(date);
     
     
    switch(month)
    {
          case 1:System.out.println("The date is: January "+day+", "+year); break;
      case 2:if(day<=28){
        System.out.println("The date is: February "+day+", "+year); }
            break;
          case 3:System.out.println("The date is: March "+day+", "+year); break;
          case 4:System.out.println("The date is: April "+day+", "+year); break;
          case 5:System.out.println("The date is: May "+day+", "+year); break;
          case 6:System.out.println("The date is: June "+day+", "+year); break;
          case 7:System.out.println("The date is: July "+day+", "+year); break;
          case 8:System.out.println("The date is: August "+day+", "+year); break;
          case 9:System.out.println("The date is: September "+day+", "+year); break;
          case 10:System.out.println("The date is: October "+day+", "+year); break;
          case 11:System.out.println("The date is: November "+day+", "+year); break;
          case 12:System.out.println("The date is: December "+day+", "+year); break;
          default: System.out.println("What planet are you living on with that kind of date? Try again!");
    }
     
    System.out.println("It is "+leapYear+" that the year "+year+" is a leap year.");
    }
     
     
     
     
    public static String formatDay(String q)
    {
    String day;
    int d;
    day =q.substring(3,5);
    d=Integer.parseInt(day);
    return day;
    }
     
     
     
     
    public static int formatMonth(String q)
    {
    String month = q.substring(0,2);
    int m = flag(Integer.parseInt(month));
    return m;
    }
     
     public static int flag(int month) 
        {
          if(month==1)
            month=1;
             if(month==2)
            month=2;
             if(month==3)
            month=3;
             if(month==4)
            month=4;
            if(month==5)
            month=5;
            if(month==6)
            month=6;
            if(month==7)
            month=7;
            if(month==8)
            month=8;
            if(month==9)
            month=9;
            if(month==10)
            month=10;
            if(month==11)
            month=11;
            if(month==12)
            month=12;
            return month;
      }
     
     
     
     
    public static String formatYear(String q)
    {
    String year;
    year = q.substring(6,10);
    return year;
    }
     
     
     
     
    public static boolean leapYear(String q)
    {
    String year=q.substring(6,10);
    int leap=Integer.parseInt(year);
    {
    if ((leap%4)==0 && (leap%100)!=0)
      return true;
    if ((leap%4)==0&&(leap%100)==0&&(leap%400)==0)
      return true;
    else
      return false;
    }
     
     
     
    }
    }
    Attached Images Attached Images

  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: DATE.java!

    Hi hiimjoey11,
    if user inserts a date with format m/d/yyyy, method formatDay has an error
    because you retrive day with a substring(3,5).

    If you want that user can insert date with format mm/dd/yyyy or m/d/yyyy, you can modify
    your method formatDate in this mode:

    public static String formatDay(String q){
    String day = "";
    String[] elements = q.split("/");
    if (elements[1].length()==1){
    day = "0" + elements[1];
    }else{
    day = elements[1];
    }

    return day;
    }

    This method splits the date inserted by user and if the string for day has length =1, concats "0" to day string.

    I dont' understand why method formatMonth returns an int, and formaDay returns a String.
    An other thing: Why do you want that your code runs with 2 format of date, but you tell user insert a date with format mm/dd/yyyy?
    Let me know!
    Bye

Similar Threads

  1. Calendar date
    By joshft91 in forum Object Oriented Programming
    Replies: 11
    Last Post: November 6th, 2010, 02:47 AM
  2. Declaring a date
    By Akim827 in forum Java Theory & Questions
    Replies: 7
    Last Post: October 12th, 2010, 11:47 AM
  3. sql date problem
    By realosso in forum JDBC & Databases
    Replies: 2
    Last Post: June 3rd, 2010, 08:32 AM
  4. How to set expiry date for cms?
    By khodam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 4th, 2009, 01:15 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM