Java SQL Time Format formatDateYM(Date date, String datePtn)

Here you can find the source of formatDateYM(Date date, String datePtn)

Description

format Date YM

License

Open Source License

Declaration

public static String formatDateYM(Date date, String datePtn) 

Method Source Code


//package com.java2s;
import java.sql.Date;
import java.sql.Time;

import java.text.*;

public class Main {
    public static final String DEFAULT_DATE_YMD_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_DATE_YM_FORMAT = "yyyyMM";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    public static String formatDateYM(Date date, String datePtn) {

        if (date == null) {
            return null;
        }/*from  w w w.  j a  va  2s.  co  m*/
        return toString(date, getYMFormat(datePtn));
    }

    public static String formatDateYM(String str, String datePtn) {

        if (str == null || str.trim().length() == 0) {
            return null;
        }
        return formatDateYM(formatCharDateYMD(str, DEFAULT_DATE_YM_FORMAT), datePtn);
    }

    public static String formatDateYM(String yy, String mm, String datePtn) {

        if (yy == null || mm == null || yy.trim().length() == 0 || mm.trim().length() == 0) {
            return null;
        }
        return formatDateYM(yy + (mm != null && mm.length() == 1 ? "0" + mm : mm), datePtn);
    }

    public static String toString(Date date) {
        return toString((java.util.Date) date);
    }

    public static String toString(java.util.Date date) {
        return toString(date, DEFAULT_DATE_YMD_FORMAT);
    }

    public static String toString(Date date, String format) {
        return toString((java.util.Date) date, format);
    }

    public static String toString(java.util.Date date, String format) {

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

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(date);
    }

    public static String toString(Time time, String format) {

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

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(time);
    }

    public static String toString(Time time) {
        return toString(time, DEFAULT_TIME_FORMAT);
    }

    public static String getYMFormat(String datePtn) {
        final String[][] DATE_FORMAT_YM_LIST = { { "1", "yyyy/MM" }, { "2", "yyyy.MM" }, { "3", "yyyy-MM" },
                { "4", "MM/yyyy" }, { "5", "MM.yyyy" }, { "6", "MM-yyyy" }, { "7", "MM/yyyy" }, { "8", "MM.yyyy" },
                { "9", "MM-yyyy" }, { "A", "MM yyyy" }, { "B", "yyyyMM" } };
        String format = null;

        for (int i = 0; i < DATE_FORMAT_YM_LIST.length; i++) {
            if (DATE_FORMAT_YM_LIST[i][0].equals(datePtn)) {
                format = DATE_FORMAT_YM_LIST[i][1];
                break;
            }
        }

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }
        return format;
    }

    public static Date formatCharDateYMD(String str) {
        return formatCharDateYMD(str, DEFAULT_DATE_YMD_FORMAT);
    }

    public static Date formatCharDateYMD(String str, String format) {

        if (str == null || str.trim().length() == 0) {
            return null;
        }

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        ParsePosition pos = new ParsePosition(0);

        java.util.Date date = sdf.parse(str, pos);

        if (date == null) {
            return null;
        }
        return new Date(date.getTime());
    }

    public static Date formatCharDateYMD(String yy, String mm, String dd) {

        if (yy == null || mm == null || dd == null || yy.trim().length() == 0 || mm.trim().length() == 0
                || dd.trim().length() == 0) {
            return null;
        }
        return formatCharDateYMD(yy + "-" + (mm != null && mm.length() == 1 ? "0" + mm : mm) + "-"
                + (dd != null && dd.length() == 1 ? "0" + dd : dd));
    }
}

Related

  1. formatCharDateYMD(String str)
  2. formatCharDateYMDHMS(String str)
  3. formatDate(String Format)
  4. formatDateWithSlashes(java.sql.Date date)
  5. formatDateYMD(String str, String datePtn)
  6. formatDefaultValue(Object o)
  7. formatExceptionForToolsWithLimitedCharacterSet(String exceptionLabel)
  8. formatTime(Date date)