Java Timestamp Format formatTimestampWithSlashes( java.sql.Timestamp tsZeitpunkt)

Here you can find the source of formatTimestampWithSlashes( java.sql.Timestamp tsZeitpunkt)

Description

format Timestamp With Slashes

License

Open Source License

Declaration

public static String formatTimestampWithSlashes(
            java.sql.Timestamp tsZeitpunkt) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * HELIUM V, Open Source ERP software for sustained success
 * at small and medium-sized enterprises.
 * Copyright (C) 2004 - 2014 HELIUM V IT-Solutions GmbH
 * /*  ww w.  ja v a 2  s.com*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published 
 * by the Free Software Foundation, either version 3 of theLicense, or 
 * (at your option) any later version.
 * 
 * According to sec. 7 of the GNU Affero General Public License, version 3, 
 * the terms of the AGPL are supplemented with the following terms:
 * 
 * "HELIUM V" and "HELIUM 5" are registered trademarks of 
 * HELIUM V IT-Solutions GmbH. The licensing of the program under the 
 * AGPL does not imply a trademark license. Therefore any rights, title and
 * interest in our trademarks remain entirely with us. If you want to propagate
 * modified versions of the Program under the name "HELIUM V" or "HELIUM 5",
 * you may only do so if you have a written permission by HELIUM V IT-Solutions 
 * GmbH (to acquire a permission please contact HELIUM V IT-Solutions
 * at trademark@heliumv.com).
 * 
 * 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contact: developers@heliumv.com
 ******************************************************************************/

import java.util.Calendar;

public class Main {
    public static String formatTimestampWithSlashes(
            java.sql.Timestamp tsZeitpunkt) {
        if (tsZeitpunkt != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(tsZeitpunkt.getTime());

            String s = "";
            int iMonat = cal.get(Calendar.MONTH) + 1;
            s = cal.get(Calendar.DAY_OF_MONTH)
                    + "/"
                    + iMonat
                    + "/"
                    + cal.get(Calendar.YEAR)
                    + " "
                    + cal.get(Calendar.HOUR_OF_DAY)
                    + ":"
                    + cal.get(Calendar.MINUTE)
                    + ":"
                    + cal.get(Calendar.SECOND)
                    + "."
                    + fitString2LengthAlignRight(
                            cal.get(Calendar.MILLISECOND) + "", 3, '0');

            return s;
        } else {
            return null;
        }
    }

    public static String fitString2LengthAlignRight(String string2Fit,
            int newLength, char fillUpChar) {
        if (string2Fit == null || string2Fit.length() == newLength) {
            return string2Fit;
        }

        String retVal = null;
        if (string2Fit.length() > newLength) {
            retVal = string2Fit.substring(0, newLength);
        } else {
            StringBuffer sb = new StringBuffer(string2Fit);
            while (sb.length() < newLength) {
                sb.insert(0, fillUpChar);
            }
            retVal = sb.toString();
        }
        return retVal;
    }
}

Related

  1. formatTimestamp(Timestamp ts, int dateStyle, int timeStyle, Locale locale)
  2. formatTimeStampMsec(long epochTime)
  3. formatTimestampTime(Timestamp ts, boolean colon)
  4. formatTimestampToIndex(java.sql.Timestamp timestamp)
  5. formatTimestampToLong(final String timestampStr)
  6. formatTimeToString(long timestamp)
  7. formatToLong(String format)
  8. formatToString(long timestamp, String datePattern)
  9. formatValue(Object obj, boolean isboolean)