转载自http://blog.csdn.net/stoppig/article/details/22081089

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.lyh.face.tools;  

  2.   

  3. import java.text.ParseException;  

  4. import java.text.SimpleDateFormat;  

  5. import java.util.Calendar;  

  6. import java.util.Date;  

  7. import java.util.Locale;  

  8.   

  9. public class TimeUtil {  

  10.   

  11.     private static final String FORMAT_Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";  

  12.     private static SimpleDateFormat sdf = null;  

  13.     private static Calendar calendar = null;  

  14.   

  15.     /** 

  16.      * 将字符串转化为Date 

  17.      *  

  18.      * @param str 

  19.      *            传入字符串时间 

  20.      * @return 

  21.      */  

  22.     public static Date getStringToDate(String str) {  

  23.   

  24.         sdf = new SimpleDateFormat(FORMAT_Y_M_D_H_M_S, Locale.getDefault());  

  25.         try {  

  26.             return sdf.parse(str);  

  27.         } catch (ParseException e) {  

  28.             e.printStackTrace();  

  29.         }  

  30.         return null;  

  31.     }  

  32.       

  33.     /** 

  34.      * 获取年 

  35.      * */  

  36.     public static int getYear(Date date) {  

  37.   

  38.         calendar = Calendar.getInstance();  

  39.         calendar.setTime(date);  

  40.         return calendar.get(Calendar.YEAR);  

  41.     }  

  42.     /** 

  43.      * 获取月 

  44.      * */  

  45.     public static int getMon(Date date){  

  46.         calendar = Calendar.getInstance();  

  47.         calendar.setTime(date);  

  48.         return (calendar.get(Calendar.MONTH)+1);  

  49.     }  

  50.     /** 

  51.      * 获取日期天 

  52.      * */  

  53.     public static int getDay(Date date){  

  54.         calendar = Calendar.getInstance();  

  55.         calendar.setTime(date);  

  56.         return calendar.get(Calendar.DATE);  

  57.     }  

  58.     /** 

  59.      * 获取时间字符串 

  60.      * */  

  61.     public static String getTime(String date){  

  62.          return date.substring(11, (date.length()-3));  

  63.     }  

  64.     /** 

  65.      * 获取年月字符串 

  66.      * */  

  67.     public static String getYear_Mon(String date){  

  68.         return date.substring(07);  

  69.     }  

  70.   

  71.     /** 

  72.      * 获取当前时间 

  73.      * */  

  74.     public static String getNowDateStr() {  

  75.         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式  

  76.         String nowdate = df.format(new Date());// new Date()为获取当前系统时间  

  77.         return nowdate;  

  78.     }  

  79.     /** 

  80.      * 获取当前时间标识码精确到毫秒 

  81.      * */  

  82.     public static String getNowtimeKeyStr(){  

  83.         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");// 设置日期格式  

  84.         String nowdate = df.format(new Date());// new Date()为获取当前系统时间  

  85.         return nowdate;  

  86.     }  

  87.       

  88.     /** 

  89.      * 获取当前时间标识码精确到秒 

  90.      * */  

  91.     public static String getNowDateKeyStr(){  

  92.         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式  

  93.         String nowdate = df.format(new Date());// new Date()为获取当前系统时间  

  94.         return nowdate;  

  95.     }  

  96. }