根据地区和时区获取当地时间

本例创建一个处理GET请求和POST请求的servlet

servlet TimeForm中的GET方法显示一个窗体:

根据地区和时区获取当地时间

Submit后的结果:

根据地区和时区获取当地时间

源代码:

 1 import javax.servlet.*;

 2 import javax.servlet.http.*;

 3 import java.io.*;

 4 import java.text.DateFormat;

 5 import java.util.*;

 6 public class TimeForm extends HttpServlet{

 7     private static final String CONTENT_TYPE="text/html;charset=GBK";

 8     private Locale[] allLocale=Locale.getAvailableLocales();

 9     private String[] allTimeZone=TimeZone.getAvailableIDs();

10     

11     public void doGet(HttpServletRequest request,HttpServletResponse response)

12     throws ServletException,IOException{

13         //解决显示中文的问题

14         response.setContentType(CONTENT_TYPE);

15         request.setCharacterEncoding("GBK");

16         PrintWriter out=response.getWriter();

17         out.println("<h3>Choose locale and time zone</h3>");

18         out.println("<form method=\"post\" action="+"/LEARN/TimeForm>");

19         out.println("Locale <select size=\"1\" name=\"locale\">");

20         for(int i=0;i<allLocale.length;++i){

21             out.println("<option value=\""+i+"\">"+

22         allLocale[i].getDisplayName()+"</option>");

23         }

24         out.println("</select>");

25         

26         out.println("<p>Time Zone<select size=\"1\" name=\"timezone\"");

27         for(int i=0;i<allTimeZone.length;++i){

28             out.println("<option value=\""+allTimeZone[i]+"\">"+allTimeZone[i]

29                     +"</option>");

30         }

31         out.println("</select");

32         

33         out.println("<p><input type=\"submit\" value=\"Submit\">");

34         out.println("<input type=\"reset\" value=\"Reset\"></p>");

35         out.println("</Form>");

36         out.close();

37     }

38     

39     public void doPost(HttpServletRequest request,HttpServletResponse response)

40     throws ServletException,IOException{

41         response.setContentType(CONTENT_TYPE);

42         //response.setContentType( "text/html;charset=GBK "); 

43         request.setCharacterEncoding("GBK");

44         PrintWriter out=response.getWriter();

45         int localeIndex=Integer.parseInt(request.getParameter("locale"));

46         String timeZoneID = request.getParameter("timezone");

47         out.println("<html>");

48         out.println("<head><title>Current Time</title></head>");

49         out.println("<body>");

50         Calendar calendar=new GregorianCalendar(allLocale[localeIndex]);

51         

52         //TimeZone 表示时区偏移量,也可以计算夏令时。

53         TimeZone timezone=TimeZone.getTimeZone(timeZoneID);

54         // 获取日期/时间格式器,该格式器具有给定语言环境的给定格式化风格。

55         DateFormat dateFormat=DateFormat.getDateTimeInstance(DateFormat.FULL

56                 , DateFormat.FULL, allLocale[localeIndex]);

57         dateFormat.setTimeZone(timezone);

58         out.println("Current time is "+dateFormat.format(calendar.getTime())+"</p>");

59         out.println("</body></html>");

60         out.close();

61     }

62 }

 

你可能感兴趣的:(时间)