Java Date to String date2String(Date date, int _iType)

Here you can find the source of date2String(Date date, int _iType)

Description

date String

License

Open Source License

Declaration

public static String date2String(Date date, int _iType) 

Method Source Code

//package com.java2s;
/*//  w  ww .  ja va 2 s. c  o m
 * Copyright 2012-2014 sammyun.com.cn. All rights reserved.
 * Support: http://www.sammyun.com.cn
 * License: http://www.sammyun.com.cn/license
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static String time_format = "yyyyMMddHHmmss";

    public static String date2String(Date date, int _iType) {
        SimpleDateFormat sdf = new SimpleDateFormat(time_format);
        String strTemp = sdf.format(date);
        SimpleDateFormat formatter = null;
        switch (_iType) {
        case 0: // yyyymmdd
            strTemp = strTemp.substring(0, 8);
            break;
        case 2: // yyyy-mm
            formatter = new SimpleDateFormat("yyyy-MM");
            strTemp = formatter.format(date);
            break;
        case 4:// yyyy
            strTemp = strTemp.substring(0, 4);
            break;
        case 6: // yymmdd
            strTemp = strTemp.substring(2, 8);
            break;
        case 8: // yyyymmdd
            strTemp = strTemp.substring(0, 8);
            break;
        case 10: // yyyy-mm-dd
            formatter = new SimpleDateFormat("yyyy-MM-dd");
            strTemp = formatter.format(date);
            break;
        case -6: // HHmmss
            strTemp = strTemp.substring(8);
            break;
        case -8: // HH:mm:ss
            formatter = new SimpleDateFormat("HH:mm:ss");
            strTemp = formatter.format(date);
            break;
        default:
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            strTemp = formatter.format(date);
            break;
        }
        return strTemp;
    }
}

Related

  1. date2string(Date date)
  2. date2String(Date date)
  3. date2String(Date date)
  4. date2String(Date date)
  5. date2String(Date date, DateFormat dateFormat)
  6. Date2String(Date date, String DateFormat)
  7. date2string(Date date, String format)
  8. date2String(Date date, String format)
  9. date2String(Date date, String format)