一個簡單編程思想在php與java中的實現比較:日期類!

來源:互聯網
上載者:User
比較|編程 以前用PHP時寫了一個簡單的class,功能主要是解決,大量頁面上需要顯示下拉式清單方塊選擇年/月/日/周之類的。希望對大家學習PHP和java能有協助。

php的實現如下:
getCurrentDate.class.php
<?php
/*
* 功能:產生下拉式清單(年/月/日/周為當前值)
* 程式員:xiangli
* 日期:2003-01-19
*/

#---------------------------------------------------#
# 修改:2003-03-18                                  #
# 修改原因:添加了周的產生                            #
#-------------------------------------------------#

class getCurrentDate{
  var    $Years = 2002;
  var    $Months = 12;
  var    $Days = 31;
  var    $Weeks = 52;
  
    /*獲得年的下拉式清單*/
    function getCurrentYear()
    {
        for ($i = Date('Y'); $i >= $this->Years; $i--)
        {
            echo "<option value='$i'>{$i}年</option>\n";
        }
    }

    /*獲得月的下拉式清單*/
    function getCurrentMonth()
    {
        for ($i = 1; $i <= $this->Months; $i++)
        {
            ($i<10)?($m="0".$i):($m=$i);            
            if($i == date('m'))
                echo "<option value='$m' selected>{$m}月</option>\n";
            else
                echo "<option value='$m'>{$m}月</option>\n";
        }
    }

    /*獲得日的下拉式清單*/
    function getCurrentDay()
    {
        for ($i = 1; $i <= $this->Days; $i++){
            if($i == date('d'))
                echo "<option value='$i' selected>{$i}日</option>\n";
            else
                echo "<option value='$i'>{$i}日</option>\n";
        }
    }
    
    /*獲得周的下拉式清單*/
    function getCurrentWeek()
    {
        for ($i = 1; $i <= $this->Weeks; $i++){
            if($i == date('W'))
                echo "<option value='$i' selected>{$i}周</option>\n";
            else
                echo "<option value='$i'>{$i}周</option>\n";
        }
    }    
}
?>

調用如下:
includ("../public/getCurrentDate.class.php");
$getCurrentDate = net getCurrentDate();
<select name ="xxxxx">
<?=$getCurrentDate->getCurrentYear()?>
</select>
//////////////////////////////////////////////////////////


java的實現方法:
getCurrentDate.java
/*
* 功能:產生下拉式清單(年/月/日/周為當前值)
* 程式員:xiangli
* 日期:2003-01-19
*/

// #---------------------------------------------------#
// # 修改:2003-03-18                                 #
// # 修改原因:添加了周的產生                         #
// #-------------------------------------------------#

import java.io.*;
import java.util.*;
import java.text.*;

public class getCurrentDate {
  public int Years = 2002;
  public int Months = 12;
  public int Days = 31;
  public int Weeks = 52;
  Date myDate = new Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd w");
  
    /*獲得年的下拉式清單*/
    public String getCurrentYear()
    {
        String Content = "";
        for (int i =  Integer.parseInt(formatter.format(myDate).toString().substring(0, 4)); i >= Years; i--)
        {
            Content += "<option value='" + i + "'>" + i + "年</option>\n";
             
        }
        return Content;
    }

    /*獲得月的下拉式清單*/
    public String getCurrentMonth()
    {
        String m;
        String Content = "";
        
        for (int i = 1; i <= Months; i++)
        {
            m=i<10?("0" + i):Integer.toString(i);
            if(i == Integer.parseInt(formatter.format(myDate).toString().substring(5, 7)))
                Content += "<option value='" + m + "' selected>" + m + "月</option>\n";
            else
                Content += "<option value='" + m + "'>" + m + "月</option>\n";
        }
        return Content;
    }

    /*獲得日的下拉式清單*/
    public String getCurrentDay()
    {
        String Content = "";
        String m;
        
        for (int i = 1; i <= Days; i++){
            m=i<10?("0" + i):Integer.toString(i);
            if(i == Integer.parseInt(formatter.format(myDate).toString().substring(8, 10)))
                Content += "<option value='" + m + "' selected>" + m + "日</option>\n";
            else
                Content += "<option value='" + m + "'>" + m + "日</option>\n";
        }
        return Content;
    }
    
    /*獲得周的下拉式清單*/
    public String getCurrentWeek()
    {
        String Content = "";
        String m;
        
        for (int i = 1; i <= Weeks; i++){
            m=i<10?("0" + i):Integer.toString(i);
            if(i == Integer.parseInt(formatter.format(myDate).toString().substring(11)))
                Content += "<option value='" + m + "' selected>" + m + "周</option>\n";
            else
                Content += "<option value='" + m + "'>" + m + "周</option>\n";
        }
        return Content;
    }    
}


調用方法:
<jsp:useBean id="getCurrentDate" scope="session" class="getCurrentDate" />
<select name="Years">
<%=getCurrentDate.getCurrentYear()%>
</select>
<select name="Months">
<%=getCurrentDate.getCurrentMonth()%>
</select>
<select name="Days">
<%=getCurrentDate.getCurrentDay()%>
</select>
<select name="Weeks">
<%=getCurrentDate.getCurrentWeek()%>
</select> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.