Java Today today()

Here you can find the source of today()

Description

Get the current date value.

License

Open Source License

Return

Date, default pattern yyyyMMdd.

Declaration

public static Date today() 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    /** this date format for internal use. **/
    private static final String DATEFORMAT_YYYYMMDD = "yyyyMMdd";

    /**//from  w w  w. j av  a2s  .c  om
     * Get the current date value.
     * 
     * <pre>
     * DateUtils.today() = 20120629
     * </pre>
     * 
     * @return Date, default pattern yyyyMMdd.
     */
    public static Date today() {
        return today(DATEFORMAT_YYYYMMDD);
    }

    /**
     * To get the current time value in accordance with the format specified.
     * 
     * <pre>
     * <code>
     * DateUtils.today("yyyyMMdd") = 20120629
     * </code>
     * </pre>
     * 
     * @param pattern date pattern to truncate the time.
     * @return Date
     */
    public static Date today(String pattern) {
        Date date = getDate();
        if (pattern == null) {
            return date;
        }
        try {
            DateFormat df = getDateFormat(pattern);
            date = df.parse(df.format(getDate()));
        } catch (Exception e) {
            return date;
        }
        return date;
    }

    /**
     * get date.
     * 
     * @return Date.
     */
    public static Date getDate() {
        return new Date();
    }

    /**
     * return date format with Locale US.
     * 
     * @param pattern date format pattern.
     * @return DateFormat
     */
    public static DateFormat getDateFormat(String pattern) {
        return new SimpleDateFormat(pattern, Locale.US);
    }
}

Related

  1. today()
  2. today()
  3. today()
  4. today()
  5. today()
  6. toDay()
  7. today()
  8. today()
  9. today()