Java Date Now getCurrentDateString()

Here you can find the source of getCurrentDateString()

Description

get Current Date String

License

Open Source License

Declaration

public static String getCurrentDateString() 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**/*w  ww . j  av  a 2  s .c  o  m*/
     * Expanded ISO 8601 Date format yyyy-MM-dd i.e., 2002-12-25 for the 25th day of December in the year 2002
     */
    public static final String ISO_EXPANDED_DATE_FORMAT = "yyyy-MM-dd";

    public static String getCurrentDateString(String pattern) {
        return dateToString(getCurrentDateTime(), pattern);
    }

    public static String getCurrentDateString() {
        return dateToString(getCurrentDateTime(), ISO_EXPANDED_DATE_FORMAT);
    }

    public static String dateToString(Date date, String pattern) {

        if (date == null) {

            return null;
        }

        try {

            SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
            sfDate.setLenient(false);

            return sfDate.format(date);
        } catch (Exception e) {

            return null;
        }
    }

    public static String dateToString(Date date) {
        return dateToString(date, ISO_EXPANDED_DATE_FORMAT);
    }

    public static Date getCurrentDateTime() {
        java.util.Calendar calNow = java.util.Calendar.getInstance();
        java.util.Date dtNow = calNow.getTime();

        return dtNow;
    }
}

Related

  1. getCurrentDateStr()
  2. getCurrentDateStr()
  3. getCurrentDateStr()
  4. getCurrentDateStr()
  5. getCurrentDateStr(String pattern)
  6. getCurrentDateString()
  7. getCurrentDateString()
  8. getCurrentDateString(String dateFormat)
  9. getCurrentDateString(String format)