發現一個很全的java處理時間日期類型的類

來源:互聯網
上載者:User
又收藏到一個好的java時間格式大全,這個是做成了類的

java.util. * ;
import  java.text. * ;
import  java.util.Calendar; 

public   class  VeDate {
  /**
  * 擷取現在時間
  * 
  *  @return  返回時間類型 yyyy-MM-dd HH:mm:ss
   */
  public   static  Date getNowDate() {
  Date currentTime  =   new  Date();
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
  String dateString  =  formatter.format(currentTime);
  ParsePosition pos  =   new  ParsePosition( 8 );
  Date currentTime_2  =  formatter.parse(dateString, pos);
   return  currentTime_2;
 } 

  /**
  * 擷取現在時間
  * 
  * @return返回短時間格式 yyyy-MM-dd
   */
  public   static  Date getNowDateShort() {
  Date currentTime  =   new  Date();
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd " );
  String dateString  =  formatter.format(currentTime);
  ParsePosition pos  =   new  ParsePosition( 8 );
  Date currentTime_2  =  formatter.parse(dateString, pos);
   return  currentTime_2;
 } 

  /**
  * 擷取現在時間
  * 
  * @return返回字串格式 yyyy-MM-dd HH:mm:ss
   */
  public   static  String getStringDate() {
  Date currentTime  =   new  Date();
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
  String dateString  =  formatter.format(currentTime);
   return  dateString;
 } 

  /**
  * 擷取現在時間
  * 
  *  @return  返回短時間字串格式yyyy-MM-dd
   */
  public   static  String getStringDateShort() {
  Date currentTime  =   new  Date();
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd " );
  String dateString  =  formatter.format(currentTime);
   return  dateString;
 } 

  /**
  * 擷取時間 小時:分;秒 HH:mm:ss
  * 
  *  @return
   */
  public   static  String getTimeShort() {
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " HH:mm:ss " );
  Date currentTime  =   new  Date();
  String dateString  =  formatter.format(currentTime);
   return  dateString;
 } 

  /**
  * 將長時間格式字串轉換為時間 yyyy-MM-dd HH:mm:ss
  * 
  *  @param  strDate
  *  @return
   */
  public   static  Date strToDateLong(String strDate) {
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
  ParsePosition pos  =   new  ParsePosition( 0 );
  Date strtodate  =  formatter.parse(strDate, pos);
   return  strtodate;
 } 

  /**
  * 將長時間格式時間轉換為字串 yyyy-MM-dd HH:mm:ss
  * 
  *  @param  dateDate
  *  @return
   */
  public   static  String dateToStrLong(java.util.Date dateDate) {
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
  String dateString  =  formatter.format(dateDate);
   return  dateString;
 } 

  /**
  * 將短時間格式時間轉換為字串 yyyy-MM-dd
  * 
  *  @param  dateDate
  *  @param  k
  *  @return
   */
  public   static  String dateToStr(java.util.Date dateDate) {
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd " );
  String dateString  =  formatter.format(dateDate);
   return  dateString;
 } 

  /**
  * 將短時間格式字串轉換為時間 yyyy-MM-dd 
  * 
  *  @param  strDate
  *  @return
   */
  public   static  Date strToDate(String strDate) {
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd " );
  ParsePosition pos  =   new  ParsePosition( 0 );
  Date strtodate  =  formatter.parse(strDate, pos);
   return  strtodate;
 } 

  /**
  * 得到現在時間
  * 
  *  @return
   */
  public   static  Date getNow() {
  Date currentTime  =   new  Date();
   return  currentTime;
 } 

  /**
  * 提取一個月中的最後一天
  * 
  *  @param  day
  *  @return
   */
  public   static  Date getLastDate( long  day) {
  Date date  =   new  Date();
   long  date_3_hm  =  date.getTime()  -   3600000   *   34   *  day;
  Date date_3_hm_date  =   new  Date(date_3_hm);
   return  date_3_hm_date;
 } 

  /**
  * 得到現在時間
  * 
  *  @return  字串 yyyyMMdd HHmmss
   */
  public   static  String getStringToday() {
  Date currentTime  =   new  Date();
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyyMMdd HHmmss " );
  String dateString  =  formatter.format(currentTime);
   return  dateString;
 } 

  /**
  * 得到現在小時
   */
  public   static  String getHour() {
  Date currentTime  =   new  Date();
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
  String dateString  =  formatter.format(currentTime);
  String hour;
  hour  =  dateString.substring( 11 ,  13 );
   return  hour;
 } 

  /**
  * 得到現在分鐘
  * 
  *  @return
   */
  public   static  String getTime() {
  Date currentTime  =   new  Date();
  SimpleDateFormat formatter  =   new  SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
  String dateString  =  formatter.format(currentTime);
  String min;
  min  =  dateString.substring( 14 ,  16 );
   return  min;
 } 

  /**
  * 根據使用者傳入的時間表示格式,返回目前時間的格式 如果是yyyyMMdd,注意字母y不能大寫。
  * 
  *  @param  sformat
  *            yyyyMMddhhmmss
  *  @return
   */
  public   static  String getUserDate(String sformat) {
  Date currentTime  =   new  Date();
  SimpleDateFormat formatter  =   new  SimpleDateFormat(sformat);
  String dateString  =  formatter.format(currentTime);
   return  dateString;
 } 

  /**
  * 二個小時時間間的差值,必須保證二個時間都是"HH:MM"的格式,返回字元型的分鐘
   */
  public   static  String getTwoHour(String st1, String st2) {
  String[] kk  =   null ;
  String[] jj  =   null ;
  kk  =  st1.split( " : " );
  jj  =  st2.split( " : " );
   if  (Integer.parseInt(kk[ 0 ])  <  Integer.parseInt(jj[ 0 ]))
    return   " 0 " ;
   else  {
    double  y  =  Double.parseDouble(kk[ 0 ])  +  Double.parseDouble(kk[ 1 ])  /   60 ;
    double  u  =  Double.parseDouble(jj[ 0 ])  +  Double.parseDouble(jj[ 1 ])  /   60 ;
    if  ((y  -  u)  >   0 )
     return  y  -  u  +   "" ;
    else
     return   " 0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.