代码改变世界

判断时间格式是否正确的代码

2007-09-14 19:07  Zhuang miao  阅读(1096)  评论(0编辑  收藏  举报

分别用C#,C++,Vb代码实现了一下,感觉这段代码还算精炼有更优秀的还请指教!!

c#

using System;
using System.Collections.Generic;
using System.Text;

namespace isdata
{
    class dataa
    {
        public static bool IsData(int y, int m, int d)
        {
            if (y <= 1000 || y >= 10000) return false;
            if (m < 1 || m > 12) return false;
            int[] month =new int[12]  {31,0,31,30,31,30,31,31,30,31,30,31};
            if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
                month[1] = 29;
            else
                month[1] = 28;
           return   (d>=1)&&(d<=month[m-1]); 
        }

    }
}

 

VB

Imports System
Imports System.Collections.Generic
Imports System.Text
 
Namespace isdata
    Class dataa
        Public Shared Function IsData(ByVal y As Integer, ByVal m As Integer, ByVal d As Integer) As Boolean
            If y <= 1000 Or y >= 10000 Then
              Return False
            End If
            If m < 1 Or m > 12 Then
              Return False
            End If
            Dim month() As Integer = New Integer(12) {31,0,31,30,31,30,31,31,30,31,30,31}

            If y % 4 = Decimal.Remainder( 0 And y ,  100 )<> 0 Or y % 400 = 0 Then
                month(1) = 29
            Else
                month(1) = 28
            End If
           Return   (d>=1)&&(d<=month(m-1))
        End Function
 
    End Class
End Namespace

c++

 

 bool   legalday(int   y,int   m,int   d)  
   {     if(y<=0)   return   false;  
   if(m<1||m>12)   return   false;  
   int   month[]=  
   {31,0,31,30,31,30,31,31,30,31,30,31};  
   
   if(y%4==0&&y%100!=0   ||   y%400==0)  
      month[1]=29;  
   else   month[1]=28;  
   return   (d>=1)&&(d<=month[m-1]);  
   }