常用java程式碼片段

來源:互聯網
上載者:User

1、JSP網頁中的中文亂碼問題:
  通常是把使用者的輸入通過request從一個網頁傳到另一個網頁時出現問題
   如果request.getParameter()得到的中文字元不能正確顯示,如:
        String userName = request.getParameter("UserName");
   中username不能正確顯示,則加上下面一句:
        userName=new String(userName.getBytes("iso-8859-1"),"gb2312");

2、得到現在時間
    public static String getTime(){
      String format="MM-dd-yyyy kk:mm a";//格式化日期和時間
      SimpleDateFormat formatter=new SimpleDateFormat(format);
      Date nowdate=new Date();
      return formatter.format(nowdate);
   }

3、把時間進行格式化顯示
   originaltime表示傳入的時間,desttime表示格式轉換候的時間
    public static String timeFormat(String originaltime) {
        Date date=null;
        String desttime=null;
        //先設定時間格式
        SimpleDateFormat bartDateFormat =new SimpleDateFormat("yyyy-MM-dd kk:mm");
        try {
            //用這種格式獲得字串時間,儲存為date對象
            date = bartDateFormat.parse(originaltime);
            //要轉成想要的時間類型字串,還須format
            desttime=bartDateFormat.format(date);
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return desttime;
    }

4、遍曆request中所有元素
    java.util.Enumeration e=request.getParameterNames();
    while(e.hasMoreElements()){
      String param=(String)e.nextElement();
      //接下來對元素的操作
    }

5、利用表單的隱藏欄位自動認可值
<input type="hidden" name="ID" value="XXXXX" >
這樣當使用者提交表單時,這個值就填充到request的相關位置中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.