Java Timestamp to String timestamp2Str(Timestamp time, String format)

Here you can find the source of timestamp2Str(Timestamp time, String format)

Description

timestamp Str

License

Open Source License

Declaration

public static String timestamp2Str(Timestamp time, String format) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public final static String PATTERN_YMDHMS = "yyyy-MM-dd HH:mm:ss";

    public static String timestamp2Str(Timestamp time, String format) {
        Date date = null;/*  ww w. j a  v  a2  s .c  o  m*/
        if (null != time) {
            if (format == null) {
                format = PATTERN_YMDHMS;
            }
            date = new Date(time.getTime());
        }
        return dateToString(date, format);
    }

    public static String dateToString(Date inputDateTime, String format) {
        String outDateTime = "0000-00-00 00:00:00";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            outDateTime = formatter.format(inputDateTime);
        } catch (Exception ex) {
        }
        return outDateTime;
    }
}

Related

  1. timestamp2String(final java.sql.Timestamp value)
  2. timestamp2String(Timestamp timestamp, String pattern)
  3. timestamptoStr(Timestamp time)
  4. timeStampToString(final Timestamp timestamp, final String format)