Servlet Localization: Date : I18N « Servlets « Java






Servlet Localization: Date

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.ListResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DateLocaleServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    //Get the client's Locale
    Locale locale = request.getLocale();
    ResourceBundle bundle = ResourceBundle.getBundle("i18n.WelcomeBundle",
        locale);
    String welcome = bundle.getString("Welcome");
    String date = DateFormat.getDateTimeInstance(DateFormat.FULL,
        DateFormat.SHORT, locale).format(new Date());

    //Display the locale
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>" + welcome + "</title></head><body>");

    out.println("<h2>" + bundle.getString("Hello") + " "
        + bundle.getString("and") + " " + welcome + "</h2>");

    out.println(date + "<br /><br />");

    java.util.Enumeration e = bundle.getKeys();
    while (e.hasMoreElements()) {

      out.println((String) e.nextElement());
      out.println("<br /><br />");

    }

    out.println("Locale: ");
    out.println(locale.getLanguage() + "_" + locale.getCountry());

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

  } //end doGet

}
// file: WelcomeBundle_es_ES.java
// import java.util.ListResourceBundle;
class WelcomeBundle_es_ES extends ListResourceBundle {

  static final Object[][] contents = {

  { "Welcome", "Huan Ying" } };

  public Object[][] getContents() {
    return contents;
  }

}
           
       








Related examples in the same category

1.Internationalization I18n
2.Servlet Localization
3.Another Servlet Localization: Currency
4.Servlet localization display
5.Set I18N Response Header
6.Hello world in Japanese