Java SimpleDateFormat给出了错误的日期

Java SimpleDateFormat给出了错误的日期,java,date,simpledateformat,Java,Date,Simpledateformat,我正在输入一个特定的日期(dd/MM/yyyy),在显示该日期时,输出的是其他内容。 import java.text.*; import java.io.*; import java.util.*; class InvalidUsernameException extends Exception //Class InvalidUsernameException { InvalidUsernameException(String s) { super(

我正在输入一个特定的日期(dd/MM/yyyy),在显示该日期时,输出的是其他内容。

import java.text.*;
import java.io.*;
import java.util.*;
class InvalidUsernameException extends Exception       //Class InvalidUsernameException
{
    InvalidUsernameException(String s)
    {
        super(s);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////
class InvalidPasswordException extends Exception      //Class InvalidPasswordException
{
    InvalidPasswordException(String s)
    {
        super(s);
    }
}
///////////////////////////////////////////////////////////////////////////////////////////
class InvalidDateException extends Exception      //Class InvalidPasswordException
{
    InvalidDateException(String s)
    {
        super(s);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////
class EmailIdb1                                   //Class Email Id b1
{
    String username, password;
    int domainid;
    Date dt;

    EmailIdb1()
    {
        username = "";
        domainid = 0;
        password = "";
        dt = new Date();
    }


    EmailIdb1(String u, String pwd, int did, int d, int m, int y)
    {
        username = u;
        domainid = did;
        password = pwd;
        dt = new Date(y,m,d);       // I think There is a problem
        SimpleDateFormat formater = new SimpleDateFormat ("yyyy/MM/dd"); //Or there can be a  problem

        try{
            if((username.equals("User")))
            {
                throw new InvalidUsernameException("Invalid Username");
            }
            else if((password.equals("123")))
            {
                throw new InvalidPasswordException("Invalid Password");
            }
            else{
                System.out.println("\nSuccesfully Login on Date : "+formater.format(dt));

            }           
        }
        catch(Exception e)
        {

        }
    }
}


///////////////////////////////////////////////////////////////////////////////////////////
class EmailId                                    //Class Email Id
{
    public static void main(String args[])
    {
        int d,m,y,did;
        String usn,pwd;
        EmailIdb1 eml;

        try{
            usn = args[0];
            pwd = args[1];
            did = Integer.parseInt(args[2]);
            d   = Integer.parseInt(args[3]);
            m   = Integer.parseInt(args[4]);
            y   = Integer.parseInt(args[5]);

            switch(m)
            {
                case 2: if(d==29 && y%4 == 0)
                        {
                            eml = new EmailIdb1(usn,pwd,did,d,m,y);
                        }
                        else if(d<=28 && d>=1)
                        {
                            eml = new EmailIdb1(usn,pwd,did,d,m,y);
                        }
                        else{
                            throw new InvalidDateException("Wrong Date.");
                        }
                        break;

                case 1: case 3: case 5: case 7: case 8: case 10:
                case 12: if(d>=1 && d<=31)
                         {
                            eml = new EmailIdb1(usn,pwd,did,d,m,y);
                         }
                         else
                         {
                             throw new InvalidDateException("Invalid Date");
                         }
                    break;
                case 4: case 6: case 9:
                case 11: if(d>=1 && d<=30)
                         {
                             eml = new EmailIdb1(usn,pwd,did,d,m,y);
                         }
                         else
                         {
                             throw new InvalidDateException("Invalid Date");
                         }
                    break;
                default : throw new InvalidDateException("Invalid Date");
            }


        }
        catch(InvalidDateException ed)
        {
            System.out.println(ed);
        }
    }
}
因为输入是

Successfully Login on Date : 3894/06/04
首先

      new Date(int year, int month, int date) 
已弃用-您不应该使用它

其次,根据javadoc:

/**
 * Allocates a <code>Date</code> object and initializes it so that
 * it represents midnight, local time, at the beginning of the day
 * specified by the <code>year</code>, <code>month</code>, and
 * <code>date</code> arguments.
 *
 * @param   year    the year minus 1900.
 * @param   month   the month between 0-11.
 * @param   date    the day of the month between 1-31.
 * @see     java.util.Calendar
 * @deprecated As of JDK version 1.1,
 * replaced by <code>Calendar.set(year + 1900, month, date)</code>
 * or <code>GregorianCalendar(year + 1900, month, date)</code>.
 */
因此,如果你通过1994年,你将得到日期为“3894年”。如果你想得到“1994”,你应该通过94作为一年。
月份表示为0-11范围内的整数,因此如果超过5,则在本例中格式为“06”,因为5表示六月而不是五月。

您不应将日期构造函数与三个整数一起使用,因为它不受欢迎。
SimpleDateFormat
您如何处理?此外,永远不要使用空的挡块。另外,对代码的注释充其量也无济于事,充其量也会损害可读性。阅读文档,int,int):年份-年份减去1900。@Boristeider Yeah man..这些注释也相当令人不安-太多的代码。这是你的责任剥离你的代码,使一个。谢谢,一个真正的帮助。。。
/**
 * Allocates a <code>Date</code> object and initializes it so that
 * it represents midnight, local time, at the beginning of the day
 * specified by the <code>year</code>, <code>month</code>, and
 * <code>date</code> arguments.
 *
 * @param   year    the year minus 1900.
 * @param   month   the month between 0-11.
 * @param   date    the day of the month between 1-31.
 * @see     java.util.Calendar
 * @deprecated As of JDK version 1.1,
 * replaced by <code>Calendar.set(year + 1900, month, date)</code>
 * or <code>GregorianCalendar(year + 1900, month, date)</code>.
 */