Java Millisecond Current Get getMillisId()

Here you can find the source of getMillisId()

Description

use time generate the id

License

Apache License

Return

String

Declaration

public static String getMillisId() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyy-MM-dd HH:mm:ss";

    /**/*from   w  w w. j ava  2 s  . c  o m*/
     * use time generate the id
     * 
     * @return String
     */
    public static String getMillisId() {
        return formatCurrentTime("yyyyMMddHHmmssSSS");
    }

    /**
     * format the current time
     * 
     * @param format
     * @return String
     */
    public static String formatCurrentTime(final String format) {
        return dateToString(getTime(), format);
    }

    /**
     * format date to string format:yyyy-MM-dd HH:mm:ss
     * 
     * @param date
     * @return String
     */
    public static String dateToString(final Date date) {
        return dateToString(date, YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
    }

    /**
     * format date to string
     * 
     * @param date
     * @param format
     * @return String
     */
    public static String dateToString(final Date date, final String format) {
        return dateToString(date, format, null);
    }

    /**
     * format date to string
     * 
     * @param date
     * @param format
     * @param locale
     * @return String
     */
    public static String dateToString(final Date date, final String format, Locale locale) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, locale == null ? Locale.getDefault() : locale);
        return sdf.format(date);
    }

    /**
     * <p>
     * Method: get current date type Date
     * </p>
     * 
     * @return java.util.Date
     */
    public static Date getTime() {
        Calendar calendar = Calendar.getInstance(Locale.getDefault());
        return calendar.getTime();
    }

    /**
     * <p>
     * Method: get current date type Date
     * </p>
     * 
     * @param locale
     * @return java.util.Date
     */
    public static Date getTime(Locale locale) {
        if (locale == null) {
            locale = Locale.getDefault();
        }
        Calendar calendar = Calendar.getInstance(locale);
        return calendar.getTime();
    }
}

Related

  1. getMilliSecondToTomorrow(Date date)
  2. getMillisFromISO8601(Object iso8601)
  3. getMillisFromRFC822String(Object rfc822)
  4. getMillisFromString(String s)
  5. getMillisFromYYYYMMDD(Object yyyymmdd)
  6. getMillisOfTowDiffDate(String p_startDate, String p_endDate)
  7. getMillon(long time)