天天看点

SimpleDateFormat 按照不同要求去改写时间的格式,或者把时间串变成date类型

package cn.edu.hactcm.cfcms.utils;

import java.text.parseexception;

import java.text.simpledateformat;

import java.util.date;

/**

 * cfms    :computer files management system 

 * version :1.0 2013-2-20 下午12:30:23

 */

public final class dateformatutils {

 /**

  * @return 返回的日期格式类似:2012年12月10日 星期一<br/>

  * <br/>

  * <b>letter  date or time component  presentation  examples</b>  <br/>

  * g  era designator  text  ad  <br/>

     * y  year  year  1996; 96  <br/>

     * m  month in year  month  july; jul; 07  <br/>

     * w  week in year  number  27  <br/>

     * w  week in month  number  2  <br/>

     * d  day in year  number  189  <br/>

     * d  day in month  number  10  <br/>

     * f  day of week in month  number  2  <br/>

     * e  day in week  text  tuesday; tue  <br/>

     * a  am/pm marker  text  pm  <br/>

     * h  hour in day (0-23)  number  0  <br/>

     * k  hour in day (1-24)  number  24  <br/>

     * k  hour in am/pm (0-11)  number  0  <br/>

     * h  hour in am/pm (1-12)  number  12  <br/>

     * m  minute in hour  number  30  <br/>

     * s  second in minute  number  55  <br/>

     * s  millisecond  number  978  <br/>

     * z  time zone  general time zone  pacific standard time; pst; gmt-08:00  <br/>

     * z  time zone  rfc 822 time zone  -0800 <br/>

     * <br/>

     * <b>date and time pattern  result</b>  <br/>

     * "yyyy.mm.dd g 'at' hh:mm:ss z"  2001.07.04 ad at 12:08:56 pdt <br/>

     * "eee, mmm d, ''yy"  wed, jul 4, '01  <br/>

     * "h:mm a"  12:08 pm  <br/>

     * "hh 'o''clock' a, zzzz"  12 o'clock pm, pacific daylight time  <br/>

     * "k:mm a, z"  0:08 pm, pdt  <br/>

     * "yyyyy.mmmmm.dd ggg hh:mm aaa"  02001.july.04 ad 12:08 pm  <br/>

     * "eee, d mmm yyyy hh:mm:ss z"  wed, 4 jul 2001 12:08:56 -0700  <br/>

     * "yymmddhhmmssz"  010704120856-0700  <br/>

     * "yyyy-mm-dd't'hh:mm:ss.sssz"  2001-07-04t12:08:56.235-0700  <br/>

  */

 public string getdateformatyyyymmdde(){  

  simpledateformat simpledateformat = new simpledateformat("yyyy年mm月dd日 e");

  string formatyyyymmddee = simpledateformat.format(new date());

  return formatyyyymmddee;

 }

  * @return 返回的日期格式类似:2012-12-10 13:44:03

 public string getdateformatyyyymmddhhmmss(){  

  simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");

  string formatyyyymmddhhmmss = simpledateformat.format(new date());

  return formatyyyymmddhhmmss;

 public date getdateformatyyyymmddhhmmss2(){ 

  string formatyyyymmddhhmmss = null;

  simpledateformat simpledateformat = null;

  date date = null;

  try {

   simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");

   formatyyyymmddhhmmss = simpledateformat.format(new date());

   date = simpledateformat.parse(formatyyyymmddhhmmss);

  } catch (exception e) {

   e.printstacktrace();

  }

  return date;

  * @return 返回的日期格式类似:2012年12月10日 13时50分50秒

 public string getdateformatoverrideyyyymmddhhmmss(){

  simpledateformat simpledateformat = new simpledateformat("yyyy年mm月dd日 hh时mm分ss秒");

  return simpledateformat.format(new date());

  * @param date:这是要格式化的日期

  * @return 返回的日期格式类似:2012-12-10

 public string getdateformatyyyymmdd(date date){

  simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd");

  return simpledateformat.format(date);

  * @param milliseconds:传入的毫秒值

  * @return 返回一个按照想要格式转换后的字符串。

 public string getdateformatyyyymmddhhmmssfrommilliseconds(long milliseconds){

  simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");

  string datetime = df.format(milliseconds);

  return datetime;

  * 如果字符串是否和条件的那么就返回毫秒值

  * @param timestring

  * @return

 @suppresswarnings("null")

 public long getmillisecondsbytimestring(string timestring){

  if (timestring != null || timestring.trim() != null) {

   simpledateformat sf = new simpledateformat("yyyy年mm月dd日");

   date d = null;

   try {

    d = sf.parse(timestring);

   } catch (parseexception e) {

    e.printstacktrace();

   }

   return d.gettime();

  return -1;

}