Java SQL Date Create getCurrentDateString()

Here you can find the source of getCurrentDateString()

Description

get Current Date String

License

Open Source License

Declaration

public static String getCurrentDateString() throws Exception 

Method Source Code


//package com.java2s;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    public static String getCurrentDateString() throws Exception {
        return getCurrentDateString("yyyyMMdd");
    }//  w w  w . j a va 2s .c o m

    public static String getCurrentDateString(String pattern) throws Exception {
        return convertToString(getCurrentTimeStamp(), pattern);
    }

    public static String convertToString(Timestamp dateData) throws Exception {

        return convertToString(dateData, "yyyy/MM/dd");

    }

    public static String convertToString(Timestamp dateData, String pattern) throws Exception {
        return convertToString(dateData, pattern, java.util.Locale.CHINA);
    }

    public static String convertToString(Timestamp dateData, String pattern, java.util.Locale locale)
            throws Exception {
        try {

            if (dateData == null) {
                return null;
            }

            SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
            //formatter.applyPattern( pattern );

            return formatter.format(dateData);
        } catch (Exception e) {
            throw new Exception("[DateUtil][convertToString]" + e.getMessage(), e);
        }

    }

    public static Timestamp getCurrentTimeStamp() throws Exception {

        try {
            Calendar cal = new GregorianCalendar();
            Timestamp result = new Timestamp(cal.getTime().getTime());
            return result;
        } catch (Exception e) {
            throw new Exception("[DateUtil][getCurrentTimeStamp]" + e.getMessage(), e);
        }

    }
}

Related

  1. getCurrentDate()
  2. getCurrentDate()
  3. getCurrentDate()
  4. getCurrentDateStart()
  5. getCurrentDateStr(String strFormat)
  6. getCurrentDateWithMinutePrecision()
  7. getCurrentDayStartDate(Date date)
  8. getCurrentDBDate()
  9. getCurrentSQLDate()