Java Date to String dateToStr(Date date)

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

Description

date To Str

License

Open Source License

Declaration

public static String dateToStr(Date date) 

Method Source Code

//package com.java2s;
/*//w w w .j a  va  2  s  .c o m
 *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

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

public class Main {
    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");
    }

    public static String dateToStr(Date date, int type) {
        switch (type) {
        case 0:
            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);
    }
}

Related

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