Java Timestamp Format getTimeStamp(String format)

Here you can find the source of getTimeStamp(String format)

Description

Gets a timestamp using the given format.

License

Open Source License

Parameter

Parameter Description
format The format of the timestamp.

Return

The current timestamp.

Declaration

public static String getTimeStamp(String format) 

Method Source Code

//package com.java2s;
/**/*from  w  w w  .j  a  v a  2 s .  c  o m*/
 * ? Copyright IBM Corporation 2016.
 * LICENSE: Apache License, Version 2.0 https://www.apache.org/licenses/LICENSE-2.0
 */

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

public class Main {
    private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd_HH-mm-ss";

    /** Gets a timestamp using the default format.
     * @return The current timestamp.
     */
    public static String getTimeStamp() {
        return getTimeStamp(TIMESTAMP_FORMAT);
    }

    /**
     * Gets a timestamp using the given format.
     * @param format The format of the timestamp.
     * @return The current timestamp.
     */
    public static String getTimeStamp(String format) {
        Date date = new Date();
        SimpleDateFormat sdf;

        try {
            sdf = new SimpleDateFormat(format);
        } catch (NullPointerException | IllegalArgumentException e) {
            sdf = new SimpleDateFormat(TIMESTAMP_FORMAT);
        }
        return sdf.format(date);
    }
}

Related

  1. getFormattedTimestamp(@Nonnull Date date)
  2. getFormattedTimestamp(File file)
  3. getFormattedTimeStamp(long timestamp, String formatString)
  4. getFullTimestampFormatter(Locale locale)
  5. getTimestamp(boolean useTwentyFourFormat)
  6. getTimestamp(String format)
  7. getTimestampFormat()
  8. getTimeStampFormat(Date date)
  9. getTimeStampFormat(Date date)