Java Millisecond Format formatTimestamp(long millis)

Here you can find the source of formatTimestamp(long millis)

Description

Returns a string representation of the specified timestamp value in yyyy-mm-dd hh:mm:ss.SSS format in the local time zone.

License

MIT License

Parameter

Parameter Description
millis a timestamp in milliseconds.

Return

the string representation of the specified timestamp.

Declaration

public static String formatTimestamp(long millis) 

Method Source Code

//package com.java2s;
/*//from   w ww.ja va  2 s.co m
 * Copyright (c) 2015-2016 QuartzDesk.com.
 * Licensed under the MIT license (https://opensource.org/licenses/MIT).
 */

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    private static final String TIMESTAMP_FORMAT_YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final String WITH_TIME_ZONE = " z";

    /**
     * Returns a string representation of the specified timestamp value
     * in yyyy-mm-dd hh:mm:ss.SSS format in the local time zone.
     *
     * @param millis a timestamp in milliseconds.
     * @return the string representation of the specified timestamp.
     */
    public static String formatTimestamp(long millis) {
        SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT_YYYY_MM_DD_HH_MM_SS_SSS);
        return f.format(new Date(millis));
    }

    /**
     * Returns a string representation of the specified timestamp value
     * in yyyy-mm-dd hh:mm:ss.SSS format in the specified time zone.
     *
     * @param millis a timestamp in milliseconds.
     * @param zone   a time zone. If null the local timezone is assumed.
     * @return the string representation of the specified timestamp.
     */
    public static String formatTimestamp(long millis, TimeZone zone) {
        SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT_YYYY_MM_DD_HH_MM_SS_SSS + WITH_TIME_ZONE);
        f.setTimeZone(zone != null ? zone : TimeZone.getDefault());
        return f.format(new Date(millis));
    }
}

Related

  1. formatTime(long millis)
  2. formatTime(long millis)
  3. formatTimeInMillis(long millis)
  4. formatTimeMillis(long millis)
  5. formatTimeMillis(long timeMillis)
  6. formatTimeWithMillis(double time)
  7. getFormatedDateTime(long millis)
  8. getFormattedAPILastAccessDate(long lastAccessTimeMillis)
  9. getMillisAsFormattedSeconds(long millis)