Java Now nowString(String pattern)

Here you can find the source of nowString(String pattern)

Description

Return the current date in the specified format

License

Open Source License

Parameter

Parameter Description
strFormat a parameter

Declaration

public static String nowString(String pattern) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**// ww w.j a v a 2s  .c  o  m
     * yyyy-MM-dd
     */
    public static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd";

    public static String nowString() {
        Calendar cal = Calendar.getInstance();
        Date currDate = cal.getTime();

        return formatDate(currDate);
    }

    /**
    * Return the current date in the specified format
    * 
    * @param strFormat
    * @return
    */
    public static String nowString(String pattern) {
        Calendar cal = Calendar.getInstance();
        Date currDate = cal.getTime();

        return format(currDate, pattern);
    }

    public static String formatDate(Date d) {
        return format(d, DATE_PATTERN_DEFAULT);
    }

    public static String format(Date d, String pattern) {
        if (d == null)
            return null;

        SimpleDateFormat dateFromat = new SimpleDateFormat(pattern);
        return dateFromat.format(d);
    }
}

Related

  1. nowFormat(String format)
  2. nowFormat(String format)
  3. nowIdentity()
  4. nowInBasicFormat()
  5. nowStr()
  6. nowString(String pattern)
  7. nowTime(String formartStr)
  8. nowTime(String format)
  9. printNow()