Java Timestamp to String toString(Timestamp t, String precision)

Here you can find the source of toString(Timestamp t, String precision)

Description

to String

License

Open Source License

Declaration

public static String toString(Timestamp t, String precision) 

Method Source Code

//package com.java2s;
/************************************************************************
 * This file is part of jsnap.                                          *
 *                                                                      *
 * jsnap 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.                                  *
 *                                                                      *
 * jsnap 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 jsnap.  If not, see <http://www.gnu.org/licenses/>.       *
 ************************************************************************/

import java.sql.Timestamp;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final String TIMESTAMP_FORMAT = "dd/MM/yyyy HH:mm:ss";
    private static final String MINUTE_FORMAT = "dd/MM/yyyy HH:mm";
    private static final String HOUR_FORMAT = "dd/MM/yyyy HH";
    private static final String DAY_FORMAT = "dd/MM/yyyy";
    private static final String MONTH_FORMAT = "MM/yyyy";
    private static final String YEAR_FORMAT = "yyyy";

    public static String toString(Timestamp t, String precision) {
        if (t == null)
            return null;
        SimpleDateFormat sdf;/*from   w  w w  .j a  v  a2 s .  c o  m*/
        if (precision.equals("minute"))
            sdf = new SimpleDateFormat(MINUTE_FORMAT);
        else if (precision.equals("hour"))
            sdf = new SimpleDateFormat(HOUR_FORMAT);
        else if (precision.equals("day"))
            sdf = new SimpleDateFormat(DAY_FORMAT);
        else if (precision.equals("month"))
            sdf = new SimpleDateFormat(MONTH_FORMAT);
        else if (precision.equals("year"))
            sdf = new SimpleDateFormat(YEAR_FORMAT);
        else
            sdf = new SimpleDateFormat(TIMESTAMP_FORMAT);
        return sdf.format(new Date(t.getTime()));
    }

    public static String toString(long millis) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP_FORMAT);
        return sdf.format(new Date(millis));
    }
}

Related

  1. timestampToString(Timestamp timestamp, String pattern)
  2. timestampToString(Timestamp ts)
  3. timestampToString(Timestamp ts, Calendar cal)
  4. timestampToStringFF(Timestamp date)
  5. timestampToUTC(Timestamp marcTimestamp)
  6. toString(Timestamp value)
  7. toStringFormatGMTTime(Timestamp time, String format)