欢迎光临
我们一直在努力

一个简单编程思想在php与java中的实现比较:日期类!-PHP教程,PHP应用

建站超值云服务器,限时71元/月

以前用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> 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个简单编程思想在php与java中的实现比较:日期类!-PHP教程,PHP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章