Java Timestamp to String timeStampToString(Timestamp stamp, String dateTimeFormat, TimeZone tz, Locale locale)

Here you can find the source of timeStampToString(Timestamp stamp, String dateTimeFormat, TimeZone tz, Locale locale)

Description

Localized Timestamp to String conversion.

License

Apache License

Declaration

public static String timeStampToString(Timestamp stamp, String dateTimeFormat, TimeZone tz, Locale locale) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Timestamp;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Locale;
import java.util.TimeZone;

public class Main {
    /**/*from   w w w.  j  a v a2  s. com*/
     * Localized Timestamp to String conversion. To be used in tandem with
     * stringToTimeStamp().
     */
    public static String timeStampToString(Timestamp stamp, TimeZone tz, Locale locale) {
        return timeStampToString(stamp, null, tz, locale);
    }

    /**
     * Localized Timestamp to String conversion. To be used in tandem with
     * stringToTimeStamp().
     */
    public static String timeStampToString(Timestamp stamp, String dateTimeFormat, TimeZone tz, Locale locale) {
        DateFormat dateFormat = toDateTimeFormat(dateTimeFormat, tz, locale);
        dateFormat.setTimeZone(tz);
        return dateFormat.format(stamp);
    }

    /**
     * Returns an initialized DateFormat object.
     *
     * @param dateTimeFormat
     *            optional format string
     * @param tz
     * @param locale
     *            can be null if dateTimeFormat is not null
     * @return DateFormat object
     */
    public static DateFormat toDateTimeFormat(String dateTimeFormat, TimeZone tz, Locale locale) {
        DateFormat df = null;
        if (dateTimeFormat == null) {
            df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
        } else {
            df = new SimpleDateFormat(dateTimeFormat);
        }
        df.setTimeZone(tz);
        return df;
    }
}

Related

  1. timestampToString(java.sql.Timestamp a_Timestamp, String aS_Format)
  2. timestampToString(long timestamp)
  3. timestampToString(long timestamp)
  4. timeStampToString(Timestamp date, String dateFmt)
  5. timestampToString(Timestamp now, String pattern)
  6. timestampToString(Timestamp timestamp)
  7. timestampToString(Timestamp timestamp)
  8. timestampToString(Timestamp timestamp, String pattern)
  9. timestampToString(Timestamp ts)