Java yyyy format(Date date, String pattern)

Here you can find the source of format(Date date, String pattern)

Description

format

License

Apache License

Declaration

public static String format(Date date, String pattern) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public final static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String format(Date date, String pattern) {
        if (date == null)
            return "";
        else/* w ww .j  ava2 s .c om*/
            return getFormatter(pattern).format(date);
    }

    public static String format(Date date) {
        if (date == null)
            return "";
        else
            return getFormatter(YYYY_MM_DD).format(date);
    }

    public static Date format(String strDate) {
        Date d = null;
        if (strDate == "")
            return null;
        else
            try {
                d = getFormatter(YYYY_MM_DD).parse(strDate);
            } catch (ParseException pex) {
                return null;
            }
        return d;
    }

    public static Date format(String strDate, String f) {
        Date d = null;
        if (strDate == "")
            return null;
        else
            try {
                d = getFormatter(f).parse(strDate);
            } catch (ParseException pex) {
                return null;
            }
        return d;
    }

    private static SimpleDateFormat getFormatter(String parttern) {
        return new SimpleDateFormat(parttern);
    }

    public static Date parse(String strDate, String pattern) throws ParseException {
        try {
            return getFormatter(pattern).parse(strDate);
        } catch (ParseException pe) {
            throw new ParseException("Method parse in Class DateUtil err: parse strDate fail.",
                    pe.getErrorOffset());
        }
    }
}

Related

  1. format(Date date)
  2. format(Date date)
  3. format(Date date)
  4. format(Date date, String format)
  5. format(Date date, String pattern)
  6. format(Date pDt)
  7. format(final Date date, final String pattern)
  8. format(final java.util.Date date, final String sFormat)
  9. format(java.util.Date date)