Here you can find the source of date2String(Date date, int _iType)
public static String date2String(Date date, int _iType)
//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; } }