Hi, I'm new in Java, and I'm supposed to make a program that ask the user for her/his date of birth..... I know that the months have a specific values and that the first month of the year is January which value is 0 ... so when the user enter the date....
example: 1983 02 24 he's saying February... So to get the value of the month right I made an If and decrease the month by 1 ... Now, the program works fine until you entered month 12 (eg: 1983 12 24) ...... I don't know if I'm not using right the scan.nextInt()... and why the if works with the numbers from 1 to 11 and doesn't work with 12 .....Can anyone Help me??

(Sorry 4 my english... if I wrote something wrong or something that doesn't make sense)

=========================================================
package asig1;

import java.util.Calendar;
import java.util.Scanner;



public class DateBirth
{
    
    private Calendar fechaNacimiento;
    private String message;
    
    
    Scanner scan = new Scanner(System.in);
    
    public DateBirth()
    {
        
        System.out.println("Se creo una instancia de la clase Fecha de Nacimiento");
        fechaNacimiento = Calendar.getInstance();
    }
    
    
    public void setMensaje()
    {
        System.out.println("Enter Ur Name: ");
        message = scan.nextLine();
    }
    public String getMensaje()
    {
        return message;
    }
    
    
    
            
    public void setFecha(int y, int m, int d)
    {
        System.out.println("Enter ur Date Of Birth (yyyy/mm/dd): ");
        fechaNacimiento.set(y, m, d);  // yyyy/mm/dd  
    }
   
    public String getFecha()
    {
        String fecha;
        String[] meses = new String[12];
        
        meses[0]= "January";
        meses[1]= "February";
        meses[2]= "March";
        meses[3]= "April";
        meses[4]= "May";
        meses[5]= "June";
        meses[6]= "July";
        meses[7]= "August";
        meses[8]= "September";
        meses[9]= "October";
        meses[10]= "November";
        meses[11]= "December";
      
        
        
        int month = fechaNacimiento.get(fechaNacimiento.MONTH);
      
     
             if (month > 0)
             { 
                 System.out.println("Month# " + month);
                 month--; 
             }
           
        
        int day = fechaNacimiento.get(fechaNacimiento.DAY_OF_MONTH);
        int year = fechaNacimiento.get(fechaNacimiento.YEAR);
      
        
        fecha = "\nMonth: " + meses[month] + " " + "\nDay: " + day + " " +
                "\nYear: " + year;
        
                
        return fecha;
        
    }
    
    public static void main(String[] args) 
    {
        
        Scanner scan = new Scanner(System.in);
        DateBirth fecha = new DateBirth();
              
        
        fecha.setMensaje();
        System.out.println();
        
        fecha.setFecha(1, 2, 3);
             
        fecha.fechaNacimiento.set(scan.nextInt(), scan.nextInt(), scan.nextInt());
        System.out.println();
        
        System.out.println(fecha.getMensaje() + " U were born on " + fecha.getFecha() + " ");
            
    }
    

}

Use SimpleDateFormat to parse the entered info into a Date object, rather than making any assumptions about the numerical value of any month. 0-11 is how it stands now, on nearly every system, but they do not have to have those values. That is why Classes like SimpleDateFormat exist and that is why Calendar has static Fields for nearly every possible value (i.e. Calendar.JANUARY, Calendar.MONDAY).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.