1. Java时间格式转换大全  
  2.   
  3. import java.text.*;  
  4. import java.util.Calendar;  
  5. public class VeDate {  
  6. /** 
  7.    * 获取现在时间 
  8.    *  
  9.    * @return 返回时间类型 yyyy-MM-dd HH:mm:ss 
  10.    */  
  11. public static Date getNowDate() {  
  12.    Date currentTime = new Date();  
  13.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  14.    String dateString = formatter.format(currentTime);  
  15.    ParsePosition pos = new ParsePosition(8);  
  16.    Date currentTime_2 = formatter.parse(dateString, pos);  
  17.    return currentTime_2;  
  18. }  
  19. /** 
  20.    * 获取现在时间 
  21.    *  
  22.    * @return返回短时间格式 yyyy-MM-dd 
  23.    */  
  24. DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");           
  25. DateFormat format 2new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");           
  26. Date date = null;      
  27. String str = null;                    
  28.               
  29. // String转Date      
  30. str = "2007-1-18";            
  31. try {      
  32.            date = format1.parse(str);     
  33.            data = format2.parse(str);   
  34. catch (ParseException e) {      
  35.            e.printStackTrace();      
  36. }     
  37. /** 
  38.    * 获取现在时间 
  39.    *  
  40.    * @return返回字符串格式 yyyy-MM-dd HH:mm:ss 
  41.    */  
  42. public static String getStringDate() {  
  43.    Date currentTime = new Date();  
  44.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  45.    String dateString = formatter.format(currentTime);  
  46.    return dateString;  
  47. }  
  48. /** 
  49.    * 获取现在时间 
  50.    *  
  51.    * @return 返回短时间字符串格式yyyy-MM-dd 
  52.    */  
  53. public static String getStringDateShort() {  
  54.    Date currentTime = new Date();  
  55.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  56.    String dateString = formatter.format(currentTime);  
  57.    return dateString;  
  58. }  
  59. /** 
  60.    * 获取时间 小时:分;秒 HH:mm:ss 
  61.    *  
  62.    * @return 
  63.    */  
  64. public static String getTimeShort() {  
  65.    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");  
  66.    Date currentTime = new Date();  
  67.    String dateString = formatter.format(currentTime);  
  68.    return dateString;  
  69. }  
  70. /** 
  71.    * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss 
  72.    *  
  73.    * @param strDate 
  74.    * @return 
  75.    */  
  76. public static Date strToDateLong(String strDate) {  
  77.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  78.    ParsePosition pos = new ParsePosition(0);  
  79.    Date strtodate = formatter.parse(strDate, pos);  
  80.    return strtodate;  
  81. }  
  82. /** 
  83.    * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss 
  84.    *  
  85.    * @param dateDate 
  86.    * @return 
  87.    */  
  88. public static String dateToStrLong(java.util.Date dateDate) {  
  89.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  90.    String dateString = formatter.format(dateDate);  
  91.    return dateString;  
  92. }  
  93. /** 
  94.    * 将短时间格式时间转换为字符串 yyyy-MM-dd 
  95.    *  
  96.    * @param dateDate 
  97.    * @param k 
  98.    * @return 
  99.    */  
  100. public static String dateToStr(java.util.Date dateDate) {  
  101.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  102.    String dateString = formatter.format(dateDate);  
  103.    return dateString;  
  104. }  
  105. /** 
  106.    * 将短时间格式字符串转换为时间 yyyy-MM-dd  
  107.    *  
  108.    * @param strDate 
  109.    * @return 
  110.    */  
  111. public static Date strToDate(String strDate) {  
  112.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  113.    ParsePosition pos = new ParsePosition(0);  
  114.    Date strtodate = formatter.parse(strDate, pos);  
  115.    return strtodate;  
  116. }  
  117. /** 
  118.    * 得到现在时间 
  119.    *  
  120.    * @return 
  121.    */  
  122. public static Date getNow() {  
  123.    Date currentTime = new Date();  
  124.    return currentTime;  
  125. }  
  126. /** 
  127.    * 提取一个月中的最后一天 
  128.    *  
  129.    * @param day 
  130.    * @return 
  131.    */  
  132. public static Date getLastDate(long day) {  
  133.    Date date = new Date();  
  134.    long date_3_hm = date.getTime() - 3600000 * 34 * day;  
  135.    Date date_3_hm_date = new Date(date_3_hm);  
  136.    return date_3_hm_date;  
  137. }  
  138. /** 
  139.    * 得到现在时间 
  140.    *  
  141.    * @return 字符串 yyyyMMdd HHmmss 
  142.    */  
  143. public static String getStringToday() {  
  144.    Date currentTime = new Date();  
  145.    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");  
  146.    String dateString = formatter.format(currentTime);  
  147.    return dateString;  
  148. }  
  149. /** 
  150.    * 得到现在小时 
  151.    */  
  152. public static String getHour() {  
  153.    Date currentTime = new Date();  
  154.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  155.    String dateString = formatter.format(currentTime);  
  156.    String hour;  
  157.    hour = dateString.substring(1113);  
  158.    return hour;  
  159. }  
  160. /** 
  161.    * 得到现在分钟 
  162.    *  
  163.    * @return 
  164.    */  
  165. public static String getTime() {  
  166.    Date currentTime = new Date();  
  167.    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  168.    String dateString = formatter.format(currentTime);  
  169.    String min;  
  170.    min = dateString.substring(1416);  
  171.    return min;  
  172. }  
  173. /** 
  174.    * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。 
  175.    *  
  176.    * @param sformat 
  177.    *             yyyyMMddhhmmss 
  178.    * @return 
  179.    */  
  180. public static String getUserDate(String sformat) {  
  181.    Date currentTime = new Date();  
  182.    SimpleDateFormat formatter = new SimpleDateFormat(sformat);  
  183.    String dateString = formatter.format(currentTime);  
  184.    return dateString;  
  185. }  
  186.