Java Locale Format formatDate2Date(Date date, String format)

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

Description

format Date Date

License

Open Source License

Declaration

public static final Date formatDate2Date(Date date, String format) 

Method Source Code

//package com.java2s;

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

import java.util.Date;

import java.util.Locale;

public class Main {
    public static final Date formatDate2Date(Date date, String format) {
        return formatString2Date(formatDate2String(date, format), format);
    }//from  w  w w  .ja  va2s .  c  o m

    public static final Date formatDate2Date(Date date, String format, Locale locale) {
        return formatString2Date(formatDate2String(date, format, locale), format, locale);
    }

    public static final Date formatString2Date(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date datum = new Date();
        try {
            datum = sdf.parse(dateString);
            return datum;
        } catch (ParseException e) {
            //logger.error(e.getMessage());
            return null;
        }

    }

    public static final Date formatString2Date(String dateString, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date datum = new Date();
        try {
            datum = sdf.parse(dateString);
            return datum;
        } catch (ParseException e) {
            //logger.error(e.getMessage());
            return null;
        }

    }

    public static final Date formatString2Date(String dateString, String format, Locale locale) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
        Date datum = new Date();
        try {
            datum = sdf.parse(dateString);
            return datum;
        } catch (ParseException e) {
            //logger.error(e.getMessage());
            return null;
        }

    }

    public static final String formatDate2String(Date date, String format) {
        if (isNull(date))
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dateString = "";
        try {
            dateString = sdf.format(date);
        } catch (Exception e) {
            //logger.error(e.getMessage());
        }
        return dateString;
    }

    public static final String formatDate2String(Date date, String format, Locale locale) {
        if (isNull(date))
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
        String dateString = "";
        try {
            dateString = sdf.format(date);
        } catch (Exception e) {
            //logger.error(e.getMessage());
        }
        return dateString;
    }

    public static final boolean isNull(Object str) {
        return (str == null || str.toString().trim().length() == 0);
    }
}

Related

  1. formatDate(String _Date)
  2. formatDate(String dateStr, String format, String toFormat)
  3. formatDate(String pattern, Date date, String valueForNull)
  4. formatDate(String strDate, Locale locale)
  5. formatDate(String value, Locale locale)
  6. formatDate2String(Date date, String format)
  7. formatDateByFormat(Date date, String format)
  8. formatDateTime(final Date date)
  9. formatDateTime(java.util.Date date, String format, String locale, String timeZone)