Tweet
|
//Set the default language English. java.util.Locale.setDefault(new java.util.Locale("en","")); //Load the resource of user language (set by the user's browser). java.util.ResourceBundle wwnaviBundle = java.util.ResourceBundle.getBundle("wwnavi_rs",request.getLocale()); //'request' is a predefined variable in JSP, or given as a method paramter in Servlet.
//Format the current date in user language (set by user's browser). DateFormat df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT,request.getLocale()); //'request' is a predefined variable in JSP, or given as a method paramter in Servlet. String date = df.format(new Date());
<%@ page contentType="text/html; charset=UTF-8" %> //JSP tag. response.setContentType("text/html; charset=UTF-8"); //Servlet code.
//Keep the timestamp in MySQL in ISO standard format, and show them in user's format. //The timestamp value in MySQL | edit_date | ------------------- 2010-01-25 00:00:00 //Get the value by SQL. String sql = "select edit_date from table1 ...";... ResultSet rs = pstmt.executeQuery();... Timestamp ts = rs.getTimestamp(0);... //Parse the value as ISO standard format and create a date object. DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date date = df.parse(ts.toString()); //Format the date object in user language (set by user's browser). DateFormat df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT,request.getLocale()); //'request' is a predefined variable in JSP, or given as a method paramter in Servlet. out.println(df.format(date));
// Get UTC Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); int offSet = TimeZone.getDefault().getOffset(cal.getTimeInMillis()); offSet = offSet / 1000; cal.add(Calendar.SECOND, -offSet); Date utcNow = cal.getTime(); // Convert UTC to a local time cal = Calendar.getInstance(); cal.setTime(utcNow); TimeZone locTz = TimeZone.getTimeZone("Asia/Japan"); (*1) int offSet = locTz.getRawOffset(); offSet = offSet / 1000; cal.add(Calendar.SECOND, offSet); if (locTz.inDaylightTime(cal.getTime())) { (*2) cal.add(Calendar.SECOND, locTz.getDSTSavings() / 1000); } Date locDate = cal.getTime(); *1)Japanese time zone is specified. Actually, you need to detect the right time zone from access area or user settings, but this process needs mapped data between time zone IDs and region specific information. The list of TimeZone IDs are given by the method, getAvailableIDs. About client time zone detection, refer to JavaScript Internationalization Programming - Locale, Format, And Time Zone. *2)Checking if the target time zone has summer time.
Refer to MSDN Library.
Go to Internationalization Programming Top