Java Timestamp unformattedFromTimestamp(java.sql.Timestamp t)

Here you can find the source of unformattedFromTimestamp(java.sql.Timestamp t)

Description

Convert from a Timestamp to an unlocalized string with the format yyyymmddhhmmss.

License

Open Source License

Parameter

Parameter Description
t A <i>Timestamp</i>.

Return

The string.

Declaration

public static String unformattedFromTimestamp(java.sql.Timestamp t) 

Method Source Code

//package com.java2s;
/*// w w w . ja  va 2 s .  c o  m
 * Copyright (C) 2015 Miquel Sas
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.text.SimpleDateFormat;

public class Main {
    /**
     * Convert from a <i>Timestamp</i> to an unlocalized string with the format <b>yyyymmddhhmmss</b>.
     * 
     * @return The string.
     * @param t A <i>Timestamp</i>.
     */
    public static String unformattedFromTimestamp(java.sql.Timestamp t) {
        return unformattedFromTimestamp(t, true, false);
    }

    /**
     * Convert from a <i>Timestamp</i> to an unlocalized string with the format <b>yyyymmddhhmmss</b> or
     * <b>yyyymmddhhmmssnnn</b>
     * 
     * @return The string.
     * @param timestamp A <code>Timestamp</code>.
     * @param millis A <code>boolean</code> to include milliseconds.
     */
    public static String unformattedFromTimestamp(java.sql.Timestamp timestamp, boolean millis) {
        return unformattedFromTimestamp(timestamp, millis, false);
    }

    /**
     * Convert from a <i>Timestamp</i> to an unlocalized string with the format <b>yyyymmddhhmmss</b> or
     * <b>yyyymmddhhmmssnnn</b>
     * 
     * @return The string.
     * @param timestamp A <code>Timestamp</code>.
     * @param millis A <code>boolean</code> to include milliseconds.
     * @param separators A boolean to include standard separators
     */
    public static String unformattedFromTimestamp(java.sql.Timestamp timestamp, boolean millis,
            boolean separators) {
        return unformattedFromTimestamp(timestamp, true, true, true, true, true, true, millis, separators);
    }

    /**
     * Convert from a <i>Timestamp</i> to an unlocalized string with the format <b>yyyymmddhhmmss</b> or
     * <b>yyyymmddhhmmssnnn</b>
     * 
     * @return The string.
     * @param timestamp A <code>Timestamp</code>.
     * @param year A <code>boolean</code> to include year.
     * @param month A <code>boolean</code> to include month.
     * @param day A <code>boolean</code> to include day.
     * @param hour A <code>boolean</code> to include hour.
     * @param minute A <code>boolean</code> to include minute.
     * @param second A <code>boolean</code> to include second.
     * @param millis A <code>boolean</code> to include milliseconds.
     * @param separators A boolean to include standard separators
     */
    public static String unformattedFromTimestamp(java.sql.Timestamp timestamp, boolean year, boolean month,
            boolean day, boolean hour, boolean minute, boolean second, boolean millis, boolean separators) {
        if (timestamp == null) {
            return "";
        }
        StringBuilder pattern = new StringBuilder();
        if (year) {
            pattern.append("yyyy");
        }
        if (month) {
            if (separators && pattern.length() != 0) {
                pattern.append("-");
            }
            pattern.append("MM");
        }
        if (day) {
            if (separators && pattern.length() != 0) {
                pattern.append("-");
            }
            pattern.append("dd");
        }
        if (hour) {
            if (separators && pattern.length() != 0) {
                pattern.append(" ");
            }
            pattern.append("HH");
        }
        if (minute) {
            if (separators && pattern.length() != 0) {
                pattern.append(":");
            }
            pattern.append("mm");
        }
        if (second) {
            if (separators && pattern.length() != 0) {
                pattern.append(":");
            }
            pattern.append("ss");
        }
        if (millis) {
            if (separators && pattern.length() != 0) {
                pattern.append(".");
            }
            pattern.append("SSS");
        }
        SimpleDateFormat df = new SimpleDateFormat(pattern.toString());
        return df.format(timestamp);
    }
}

Related

  1. timestampPlusDay2DDMMYYYY(java.sql.Timestamp ts, int iDayPlus)
  2. trunc(Timestamp dayTime, String trunc)
  3. truncateFractionalSeconds(Timestamp timestamp)
  4. truncateTimestamp(Timestamp timestamp, String period)
  5. truncNanos(Timestamp timestamp)
  6. utcToTimestamp(String utcTimestamp)
  7. weekNumber(Timestamp stamp, TimeZone timeZone, Locale locale)
  8. writeTimestamp(ByteBuffer logBuf, Timestamp time)
  9. writeTimestamp(Timestamp stamp, ObjectOutput out, long nullFlag)