Java Hour Format formatNewDate(String format)

Here you can find the source of formatNewDate(String format)

Description

format New Date

License

Open Source License

Declaration

public static Date formatNewDate(String format) 

Method Source Code


//package com.java2s;

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

public class Main {
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN_SS = "yyyyMMddHHmmssSS";
    private static final String INIT_DATE_TIME = "1900-01-01 00:00:00";

    public static Date formatNewDate(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {/*from  w  w  w. j  a v  a 2s  . c om*/
            return sdf.parse(dateToString(new Date(), format));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date parse(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        try {
            if (str.length() < INIT_DATE_TIME.length()) {
                str += INIT_DATE_TIME.substring(str.length() + 1);
            }
            return sdf.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String dateToString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    public static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return sdf.format(date);
    }

    public static String format(Date date, String pattern) {
        return date == null ? " " : new SimpleDateFormat(pattern).format(date);
    }

    public static String format() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_SS);
        return sdf.format(new Date());
    }
}

Related

  1. formatInterval(long intervalMsec)
  2. formatLastModified(long lastModifiedTime)
  3. formatMessage(String message)
  4. formatMM(Date date)
  5. formatNanosTimeUSEastern(long nanos)
  6. formatOffset(int offset)
  7. formatOptimal(Date d)
  8. formatPrice(double price, String style)
  9. formatRawDateTime(int unixtime)