Java Date Now getCurrentDay()

Here you can find the source of getCurrentDay()

Description

Method: get current day[year-month-day] type string

License

Apache License

Return

String

Declaration

public static String getCurrentDay() 

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 = "yyyy-MM-dd";
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyy-MM-dd HH:mm:ss";

    /**/*from w  w  w.ja  va  2  s  .  co m*/
     * <p>
     * Method: get current day[year-month-day] type string
     * </p>
     * 
     * @return String
     */
    public static String getCurrentDay() {
        return formatCurrentTime(YEAR_MONTH_DAY);
    }

    /**
     * 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. getCurrentDateTimeString(String returnFormat)
  2. getCurrentDateTimeStringValue()
  3. getCurrentDateToString(String pattern)
  4. getCurrentDateWithDot()
  5. getCurrentDay()
  6. getCurrentDay()
  7. getCurrentDay()
  8. getCurrentDay()
  9. getCurrentDay()