`
Listen_ing
  • 浏览: 35342 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

DateFormatTool(日期格式和字符串格之间转换类,日期转换器用到此类)

阅读更多
public class DateFormatTool {
    private static final Log log = LogFactory.getLog(DateFormatTool.class);
    private static SimpleDateFormat dateFormat;


    /**
     * 将字符串类型的日期转换为timestamp(时间戳记java.sql.Timestamp
     *
     * @param dateString
     *          转换为timestamp的字符串
     *@return string2TimeStamp
     */
    public final static java.sql.Timestamp string2TimeStamp(String dateString) {
       try {
           if(dateString.length()>12)
           {
              dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss",
                     Locale.CHINA);// 设定格式
           }else{
              dateFormat = new SimpleDateFormat("yyyy-MM-dd",
                     Locale.CHINA);// 设定格式
           }
           dateFormat.setLenient(false);
           java.util.Date timeDate = dateFormat.parse(dateString);// util类型
           java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate
                  .getTime());// Timestamp类型,timeDate.getTime()
           return dateTime;
       } catch (ParseException pe) {
           log.error("date type convert failed", pe);
           return null;
       }
    }


    /**
     *method 将日期Timestamp转换为字符串类型
     *
     * @param timeStamp
     *           转换为String的字符串
     *@return timestamp2String
     */
    public final static String timestamp2String(java.sql.Timestamp timeStamp){
       String dateTime="";
       if(timeStamp.toString().indexOf("00:00:00")!=-1){
           dateTime=timeStamp.toString().substring(0,timeStamp.toString().indexOf(" "));
       }else{
           dateTime=timeStamp.toString().substring(0,timeStamp.toString().lastIndexOf("."));
       }
       return dateTime;
    }


    /**
     * 字符串转换为yyyy-MM-dd格式
     *
     * @param date
     *            字符
     * @return Date
     */
    public static Date getDate(String date) {
       try {
           dateFormat = new SimpleDateFormat("yyyy-MM-dd");
           if ("".equals(date) || null == date)
              return null;
           return dateFormat.parse(date);
       } catch (ParseException pe) {
           log.error("date type convert failed", pe);
           return null;
       }
    }


    /**
     * yyyy-MM-dd转换为字符串格式
     *
     * @param date
     *            Date类型
     * @return 字符
     */
    public static String getDate(Date date) {
       dateFormat = new SimpleDateFormat("yyyy-MM-dd");
       return dateFormat.format(date);
    }


    /**
     * 字符串转换为yyyy-MM-dd HH:mm:ss格式
     *
     * @param date
     *            字符
     * @return Date
     */
    public static Date getLongDate(String date) {
       try {
           dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           return dateFormat.parse(date);
       } catch (ParseException pe) {
           log.error("date type convert failed", pe);
           return null;
       }
    }


    /**
     * yyyy-MM-dd HH:mm:ss转换为字符串格式
     *
     * @param date
     *            Date类型
     * @return 字符
     */
    public static String getLongDate(Date date) {
       dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       return dateFormat.format(date);
    }


    /**
     * 毫秒转换为yyyy-MM-dd HH:mm:ss日期格式
     *
     * @param timeMillis
     *            毫秒
     * @return Date
     * @since Aug 25, 2009
     */
    public static Date getLongDate(long timeMillis) {
       try {
           dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           return dateFormat.parse(String.valueOf(timeMillis));
       } catch (Exception e) {
           log.error("date type convert failed", e);
           return null;
       }


    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics