[VB6][VB.Net][C#][JAVA] 利用DateSerial 和 DateTime.IsLeapYear 判斷 平年 閏年 Leap Year

  • 15242
  • 0
  • C#
  • 2008-11-17

摘要:[VB6][VB.Net][C#][JAVA] 利用DateSerial 和 DateTime.IsLeapYear 判斷 平年 閏年 Leap Year

比較常使用潤年規則來判斷是否為潤年,如下
C# &JAVA:
        private bool CheckLeap(int year)
        {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
                return true;
            else return false;
        }

VB:
Private Function JudgeLeap(ByVal intYear As Integer) As Boolean
    If (intYear Mod 400 = 0) Or (intYear Mod 4 = 0 And intYear Mod 100 <> 0) Then
        JudgeLeap = True
    Else
        JudgeLeap = False
    End If
End Function
 

而微軟本身就已經提供好用的函數,程式設計者跟本不用寫落落長的判斷條件。      
VB6&VBA:利用DateSerial可以回傳該年的月日,利用此功能來判定2月是否有29日,如果有29表示該年為潤年,MonthToLeapYear與IsLeap都是用來判斷是否有潤月

Private Sub Command1_Click()
    If MonthToLeapYear(Text1.Text) Then MsgBox Text1.Text & "年是閏年。" Else MsgBox Text1.Text & "不是閏年"
    If IsLeap(Text1.Text) Then MsgBox Text1.Text & "年是閏年。" Else MsgBox Text1.Text & "不是閏年"
End Sub

Private Function MonthToLeapYear(ByVal Yea As Integer) As Boolean
    MonthToLeapYear = Day(DateSerial(Yea, 2, 29)) = 29
End Function

Private Function IsLeap(sYear As String) As Integer
    If IsDate("02/29/" & sYear) Then
        IsLeap = True
    Else
        IsLeap = False
    End If
End Function

VB.Net:寫法稍有不同把Day換成VisualBasic.DateAndTime.Day

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If MonthToLeapYear(TextBox1.Text) Then MsgBox(TextBox1.Text & "年是閏年。") Else MsgBox(TextBox1.Text & "不是閏年")
        If IsLeap(TextBox1.Text) Then MsgBox(TextBox1.Text & "年是閏年。") Else MsgBox(TextBox1.Text & "不是閏年")
    End Sub

    Private Function MonthToLeapYear(ByVal Yea As Integer) As Boolean
        Return Microsoft.VisualBasic.DateAndTime.Day(DateSerial(Yea, 2, 29)) = 29
    End Function

    Function IsLeap(ByVal sYear As String) As Boolean
        If IsDate("02/29/" & sYear) Then
            IsLeap = True
        Else
            IsLeap = False
        End If
    End Function

End Class

.NET Framework:DateTime.IsLeapYear 方法
利用DateTime.IsLeapYear 方法,來判斷是否為潤年。

        private void button5_Click(object sender, EventArgs e)
        {
            string MyString = "2006-06-30";
            DateTime MyDate = Convert.ToDateTime(MyString);
            string MyInfo = MyDate.Year + "年是:";
            if (DateTime.IsLeapYear(MyDate.Year))
            {
                MyInfo += "潤年。";
            }

            else
            {
                MyInfo += "平年。";
            }

            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

 JAVA:利用Date類別判斷該年2月是否有29日

import java.util.*;
class TAS
{
    @SuppressWarnings("deprecation")
    public static void main(String[] args)
    {
        Date date = new Date(2000 , 2 , 0);
       System.out.println(date.getDate());
    }

}

 利用Calendar.IsLeapYear 方法,來判斷是否為潤年。
http://java.sun.com/j2se/1.3/docs/api/java/util/GregorianCalendar.html

import java.util.*;
import java.io.*;
public class leapyear {

    /**
  * @param args
  */
    public static void main(String[] args) {
  // TODO Auto-generated method stub
  Calendar cal = Calendar.getInstance();
  boolean booleanleapYear = ( (GregorianCalendar)cal ).isLeapYear(2008);
  if(booleanleapYear)
  {
      System.out.println(" is a leap year");
  }
  else
  {
      System.out.println(" is Not a leap year");
  }
    }
}

 

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo