Here you can find the source of formatDateTime(java.util.Date date, String format, String locale, String timeZone)
Parameter | Description |
---|---|
date | the date to format |
format | the format string |
locale | the locale |
timeZone | the timezone |
public static String formatDateTime(java.util.Date date, String format, String locale, String timeZone)
//package com.java2s; /*//from w ww . j a v a 2 s .c om * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (http://h2database.com/html/license.html). * Initial Developer: H2 Group * Iso8601: * Initial Developer: Robert Rathsack (firstName dot lastName at gmx dot de) */ import java.text.SimpleDateFormat; import java.util.Locale; import java.util.TimeZone; public class Main { /** * Formats a date using a format string. * * @param date the date to format * @param format the format string * @param locale the locale * @param timeZone the timezone * @return the formatted date */ public static String formatDateTime(java.util.Date date, String format, String locale, String timeZone) { SimpleDateFormat dateFormat = getDateFormat(format, locale, timeZone); synchronized (dateFormat) { return dateFormat.format(date); } } private static SimpleDateFormat getDateFormat(String format, String locale, String timeZone) { try { // currently, a new instance is create for each call // however, could cache the last few instances SimpleDateFormat df; if (locale == null) { df = new SimpleDateFormat(format); } else { Locale l = new Locale(locale); df = new SimpleDateFormat(format, l); } if (timeZone != null) { df.setTimeZone(TimeZone.getTimeZone(timeZone)); } return df; } catch (Exception e) { throw new RuntimeException("PARSE_ERROR:" + (format + "/" + locale + "/" + timeZone), e); } } }