Java Hour Format formatDateTime(Date myDate)

Here you can find the source of formatDateTime(Date myDate)

Description

format Date Time

License

Apache License

Declaration

public static String formatDateTime(Date myDate) 

Method Source Code


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

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    private static final String patternDate = "yyyy-MM-dd";
    private static final String patternDateTime = "yyyy-MM-dd HH:mm:ss";

    public static String formatDateTime(Date myDate, String pattern) {
        myDate = isDate(myDate);// w w  w .j a va2s .  co m
        // SimpleDateFormat fd = new SimpleDateFormat(pattern, Locale.CHINA);
        SimpleDateFormat fd = new SimpleDateFormat(pattern);
        return (fd.format(myDate));
    }

    public static String formatDateTime(String myDate, String pattern) {
        if (myDate.length() > 10)
            myDate = myDate.substring(0, 10);
        Date date = getDateByString(myDate, "yyyy-MM-dd");
        return formatDateTime(date, pattern, Locale.US);
    }

    public static String formatDateTime(Date myDate, String pattern, Locale localcode) {
        myDate = isDate(myDate);
        SimpleDateFormat fd = new SimpleDateFormat(pattern, localcode);
        return (fd.format(myDate));
    }

    public static String formatDateTime(Date myDate) {
        return formatDateTime(myDate, patternDateTime);
    }

    public static Date isDate(Date myDate) {
        // if (myDate == null)
        // return new Date();
        return myDate;
    }

    public static Date getDateByString(String rq) {
        // DateFormat df = DateFormat.getDateInstance();
        DateFormat df = new SimpleDateFormat(patternDate);
        Date d = null;
        try {
            d = df.parse(rq);
        } catch (Exception e) {
            // e.printStackTrace();
        }
        return d;
    }

    public static Date getDateByString(String str, String pattern) {
        SimpleDateFormat sdf = null;
        try {
            sdf = new SimpleDateFormat(pattern);
        } catch (Exception ex) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        try {
            return sdf.parse(str);
        } catch (Exception e) {
        }
        return null;
    }

    public static Date parse(String param) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(patternDate);
        try {
            date = sdf.parse(param);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return date;
    }
}

Related

  1. formatDateTime(Date date)
  2. formatDateTime(Date date)
  3. formatDatetime(Date date)
  4. formatDateTime(Date date)
  5. formatDateTime(Date dateTime)
  6. formatDateTime(final Calendar cal)
  7. formatDateTime(final Date dat)
  8. formatDateTime(final Date date)
  9. formatDateTime(java.util.Date d)