2016-08-16 6 views
-2

特定の日付(dd/MM/yyyy)を入力していますが、出力は何か他のものを表示しています。SimpleDateFormatが間違っています。

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); 
     } 
    } 
} 

私と私の二人の友人は、問題の似たようなものを持っています。これがなぜ起こっているのか分かりません。あなたに - 入力は廃止され、すべての

 new Date(int year, int month, int date) 

Successfully Login on Date : 3894/06/04 
+1

Dateコンストラクタは、3つのintを使って使用するべきではありません。これは、それが削除されているためです。 – Jens

+1

'SimpleDateFormat'で何を_do_しますか?さらに、空のキャッチブロックを決して使用するべきではありません。 P.S.あなたのコードに対するコメントはせいぜい役に立たず、最悪の場合、実際には読みにくいです。うん@BoristheSpider 1900 –

+4

は、ドキュメントhttp://docs.oracle.com/javase/6/docs/api/java/util/Date.html#Date(int、int型、int)を読みますman..thoseコメントはイェンス – Jens

答えて

2

最初のものであるとしても、問題は何を見つけることができませんでした先生は

出力は

Successfully Login on Date : 1994/05/04 

であるべきそれを使用しないでください

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を1年として渡す必要があります。 と月は範囲0-11からintとして表現されるので、あなたは5を渡すと5月6月を表していないので、それは、あなたのケースでは「06」としてフォーマットされています。

+0

ありがとう、本当の助け... – Priyank

関連する問題