zoukankan      html  css  js  c++  java
  • Servlet处理日期

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/handling-date.html

    使用Servlet的最重要的优势之一是可以使用核心Java中的大多数可用的方法,Java提供的Date类,该类在java.util包中是可用的,这个类封装了当前的日期和时间。

    Date类支持两个构造函数。第一个构造函数用当前日期和时间初始化对象。

    Date( )

    下面的构造函数接受一个参数,该参数等于自1970年1月1日凌晨零点以来经过的毫秒数。

    Date(long millisec)

    一旦得到一个可用的Date对象,可以调用下列任意支持的方法来使用日期:

    方法描述

    boolean after(Date date)

    如果调用的Date对象中包含的日期在date指定的日期之后,则返回true,否则返回false。

    boolean before(Date date)

    如果调用的Date对象中包含的日期在date指定的日期之前,则返回true,否则返回false。

    Object clone( )

    重复调用Date对象。

    int compareTo(Date date)

    把调用对象的值与date的值进行比较。如果两个值是相等的,则返回0。如果调用对象在date之前,则返回一个负值。如果调用对象在date之后,则返回一个正值。

    int compareTo(Object obj)

    如果obj是Date类,则操作等同于compareTo(Date)。否则,它会抛出一个ClassCastException。

    boolean equals(Object date)

    如果调用的Date对象中包含的时间和日期与date指定的相同,则返回true,否则返回false。

    long getTime( )

    返回1970年1月1日以来经过的毫秒数。

    int hashCode( )

    为调用对象返回哈希代码。

    void setTime(long time)

    设置time指定的时间和日期,这表示从1970年1月1日凌晨零点以来经过的时间(以毫秒为单位)。

    String toString( )

    转换调用的Date对象为一个字符串,并返回结果。

    一、获取当前的日期和时间

    在Java Servlet中获取当前的日期和时间是非常容易的。可以使用一个带有toString()方法的简单的Date对象来输出当前的日期和时间,如下所示:

    // Import required java libraries
    import java.io.*;
    import java.util.Date;
    import javax.servlet.*;
    import javax.servlet.http.*; 
    // Extend HttpServlet class
    public class CurrentDate extends HttpServlet { 
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
          // Set response content type
          response.setContentType("text/html"); 
          PrintWriter out = response.getWriter();
          String title = "Display Current Date & Time";
          Date date = new Date();
          String docType =
          "<!doctype html public "-//w3c//dtd html 4.0 " +
          "transitional//en">
    ";
          out.println(docType +
            "<html>
    " +
            "<head><title>" + title + "</title></head>
    " +
            "<body bgcolor="#f0f0f0">
    " +
            "<h1 align="center">" + title + "</h1>
    " +
            "<h2 align="center">" + date.toString() + "</h2>
    " +
            "</body></html>");
      }
    }

    配置web.xml:

        <servlet>
            <servlet-name>CurrentDate</servlet-name>
            <servlet-class>com.jsoft.testservletbasics.CurrentDate</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>CurrentDate</servlet-name>
            <url-pattern>/CurrentDate</url-pattern>
        </servlet-mapping>

    现在,编译上述Servlet并在web.xml文件中创建适当的条目,然后使用URL http://localhost:8080/CurrentDate来调用该Servlet。这将会产生如下所示的结果:

    尝试刷新URLhttp://localhost:8080/CurrentDate,每隔几秒刷新一次都会显示时间的差异。

    二、日期比较

    正如上面所提到的一样,可以在Servlet中使用所有可用的Java方法。如果需要比较两个日期,以下是方法:

    • 可以使用getTime()来获取两个对象自1970年1月1日凌晨零点以来经过的毫秒数,然后比较这两个值。

    • 可以使用方法before( )、after( )和equals( )。由于一个月里12号在18号之前,例如,new Date(99, 2, 12).before(new Date (99, 2, 18)) 返回true。

    • 可以使用compareTo( )方法,该方法由Comparable接口定义并由Date实现。

    三、使用SimpleDateFormat格式化日期

    SimpleDateFormat是一个以语言环境敏感的方式来格式化和解析日期的具体类。 SimpleDateFormat允许通过为日期时间格式化选择任何用户定义的模式开始。

    // Import required java libraries
    import java.io.*;
    import java.text.*;
    import java.util.Date;
    import javax.servlet.*;
    import javax.servlet.http.*;
    // Extend HttpServlet class
    public class CurrentDate extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
          // Set response content type
          response.setContentType("text/html"); 
          PrintWriter out = response.getWriter();
          String title = "Display Current Date & Time";
          Date dNow = new Date( );
          SimpleDateFormat ft =  new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
          String docType =
          "<!doctype html public "-//w3c//dtd html 4.0 " +
          "transitional//en">
    ";
          out.println(docType +
            "<html>
    " +
            "<head><title>" + title + "</title></head>
    " +
            "<body bgcolor="#f0f0f0">
    " +
            "<h1 align="center">" + title + "</h1>
    " +
            "<h2 align="center">" + ft.format(dNow) + "</h2>
    " +
            "</body></html>");
      }
    }

    再次编译上述Servlet,然后使用URL http://localhost:8080/CurrentDate来调用该Servlet。这将会产生如下所示的结果:

    四、简单的日期格式的格式代码

    要指定时间格式,那么使用时间模式的字符串。在这种模式下,所有的ASCII字母被保留为模式字母,这些字母定义如下:

    字符描述实例
    G 时代指示器 AD
    y 四位数的年 2001
    M 一年中的月 July 或 07
    d 一月中的第几天 10
    h 带有 A.M./P.M. 的小时(1~12) 12
    H 一天中的第几小时(0~23) 22
    m 一小时中的第几分 30
    s 一分中的第几秒 55
    S 毫秒 234
    E 一周中的星期几 Tuesday
    D 一年中的第几天 360
    F 一个月中的某一周的某一天 2 (second Wed. in July)
    w 一年中的第几周 40
    W 一月中的第几周 1
    a A.M./P.M. 标记 PM
    k 一天中的第几小时(1~24) 24
    K 带有 A.M./P.M. 的小时(0~11) 10
    z 时区 Eastern Standard Time
    ' Escape for text 分隔符
    " 单引号 `

    处理日期方法的可用方法的完整列表,可以参考标准的Java文档。

    测试工程:https://github.com/easonjim/5_java_example/tree/master/servletbasics/test12

  • 相关阅读:
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6977683.html
Copyright © 2011-2022 走看看