Java Date to String dateToStr(Date date, int type)

Here you can find the source of dateToStr(Date date, int type)

Description

date To Str

License

Open Source License

Declaration

public static String dateToStr(Date date, int type) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static String dateToStr(Date date, int type) {
        switch (type) {
        case 0:/*from   ww w . ja v a2  s.  c  o  m*/
            return dateToStr(date);
        case 1:
            return dateToStr(date, "yyyy/MM");
        case 2:
            return dateToStr(date, "yyyyMMdd");
        case 11:
            return dateToStr(date, "yyyy-MM-dd");
        case 3:
            return dateToStr(date, "yyyyMM");
        case 4:
            return dateToStr(date, "yyyy/MM/dd HH:mm:ss");
        case 5:
            return dateToStr(date, "yyyyMMddHHmmss");
        case 6:
            return dateToStr(date, "yyyy/MM/dd HH:mm");
        case 7:
            return dateToStr(date, "HH:mm:ss");
        case 8:
            return dateToStr(date, "HH:mm");
        case 9:
            return dateToStr(date, "HHmmss");
        case 10:
            return dateToStr(date, "HHmm");
        case 12:
            return dateToStr(date, "yyyy-MM-dd HH:mm:ss");
        }
        throw new IllegalArgumentException("Type undefined : " + type);
    }

    public static String dateToStr(Date date, String pattern) {
        if ((date == null) || (date.equals("")))
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    public static String dateToStr(Date date) {
        return dateToStr(date, "yyyy/MM/dd");
    }
}

Related

  1. dateToRFC3339(Date d)
  2. dateToStr(Date date)
  3. dateToStr(Date date)
  4. dateToStr(Date date)
  5. dateToStr(Date date)
  6. dateToStr(Date date, String format)
  7. dateToStr(Date date, String format)
  8. dateToStr(Date date, String formatstr)
  9. dateToStr(Date date, String formatType)