当前位置:编程学习 > JSP >>

一个简单编程思想在java与php的相同实现比较!

答案:以前用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";
        }
     &n

上一个:to loveyourvv !servlet 传对象给jsp 的最简单的例子
下一个:利用Oracle自带的连接缓冲类的一个例子.

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,