Java Hour Format formatCurrentTime(final String format)

Here you can find the source of formatCurrentTime(final String format)

Description

format the current time

License

Apache License

Parameter

Parameter Description
format a parameter

Return

String

Declaration

public static String formatCurrentTime(final String format) 

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 ww w .j ava2 s .co  m
     * 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. formatCompleteDate(Date date)
  2. formatCreationDate(String creationDate)
  3. formatCurrentDateForDebug()
  4. formatCurrentTime()
  5. formatCurrentTime()
  6. formatDate(Calendar currentDate, String pattern)
  7. formatDate(Calendar time)
  8. formatDate(Date d, String pattern, TimeZone tz)
  9. formatDate(Date d, TimeZone tz)