java常用的日期函數介紹(1/2)

來源:互聯網
上載者:User

1)String類型轉換成日期(Date類型):

         SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
         java.util.Date date = myFormatter.parse(sj1);  
         java.util.Date mydate = myFormatter.parse(sj2);  
    (2) 根據一個日期,返回是星期幾的字串 
       public static String getWeek(String sdate) {  
        // 再轉換為時間  
        Date date = TimeTest.strToDate(sdate);  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        // int hour=c.get(Calendar.DAY_OF_WEEK);  
        // hour中存的就是星期幾了,其範圍 1~7  
        // 1=星期日 7=星期六,其他類推  
        return new SimpleDateFormat("EEEE").format(c.getTime());  
    }  
 
   (3) 將短時間格式字串轉換為時間 yyyy-MM-dd  
    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;  
    }  
 
    (4) 兩個時間之間的天數之差
    public static long getDays(String date1, String date2) {  
        if (date1 == null || date1.equals(""))  
         return 0;  
        if (date2 == null || date2.equals(""))  
         return 0;  
        // 轉換為標準時間  
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
        java.util.Date date = null;  
        java.util.Date mydate = null;  
        try {  
         date = myFormatter.parse(date1);  
         mydate = myFormatter.parse(date2);  
        } catch (Exception e) {  
        }  
        long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
        return day;  
    }  
    (5) 獲得本周星期日的日期    
    public String getCurrentWeekday() {  
        weeks = 0;  
        int mondayPlus = this.getMondayPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE, mondayPlus+6);  
        Date monday = currentDate.getTime();  
          
        DateFormat df = DateFormat.getDateInstance();  
        String preMonday = df.format(monday);  
        return preMonday;  
    }  
      
      
   (6)擷取當天時間   
        Date   now   =   new   Date();     
     


上面基於date函數的

Date類可以在java.util包中找到,用一個long類型的值表示一個指定的時刻。它的一個有用的建構函式是Date(),它建立一個表示建立時刻的對象。getTime()方法返回Date對象的long值。在下面的程式中,我使用Date()建構函式建立一個表示程式運行時刻的對象,並且利用getTime()方法找到這個日期代表的毫秒數量:


      

    (7) 獲得當前日期與本周日相差的天數  
    private int getMondayPlus() {  
        Calendar cd = Calendar.getInstance();  
        // 獲得今天是一周的第幾天,星期日是第一天,星期二是第二天......  
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;         //因為按中國禮拜一作為第一天所以這裡減1  
        if (dayOfWeek == 1) {  
            return 0;  
        } else {  
            return 1 - dayOfWeek;  
        }  
    }  
      
    (8)獲得本周一的日期  
    public String getMondayOFWeek(){  
         weeks = 0;  
         int mondayPlus = this.getMondayPlus();  
         GregorianCalendar currentDate = new GregorianCalendar();  
         currentDate.add(GregorianCalendar.DATE, mondayPlus);  
         Date monday = currentDate.getTime();  
           
         DateFormat df = DateFormat.getDateInstance();  
         String preMonday = df.format(monday);  
         return preMonday;  
    }  
      
 (9)獲得相應周的周六的日期  
    public String getSaturday() {  
        int mondayPlus = this.getMondayPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);  
        Date monday = currentDate.getTime();  
        DateFormat df = DateFormat.getDateInstance();  
        String preMonday = df.format(monday);  
        return preMonday;  
    }  
      
 (10) 獲得上周星期日的日期  
    public String getPreviousWeekSunday() {  
        weeks=0;  
        weeks--;  
        int mondayPlus = this.getMondayPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE, mondayPlus+weeks);  
        Date monday = currentDate.getTime();  
        DateFormat df = DateFormat.getDateInstance();  
        String preMonday = df.format(monday);  
        return preMonday;  
    }  
 
 (11)獲得上周星期一的日期  
    public String getPreviousWeekday() {  
        weeks--;  
        int mondayPlus = this.getMondayPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);  
        Date monday = currentDate.getTime();  
        DateFormat df = DateFormat.getDateInstance();  
        String preMonday = df.format(monday);  
        return preMonday;  
    }  
      
   (12) 獲得下周星期一的日期  
    public String getNextMonday() {  
        weeks++;  
        int mondayPlus = this.getMondayPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);  
        Date monday = currentDate.getTime();  
        DateFormat df = DateFormat.getDateInstance();  
        String preMonday = df.format(monday);  
        return preMonday;  
    }  
      
 (13)獲得下周星期日的日期  
    public String getNextSunday() {  
        int mondayPlus = this.getMondayPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6);  
        Date monday = currentDate.getTime();  
        DateFormat df = DateFormat.getDateInstance();  
        String preMonday = df.format(monday);  
        return preMonday;  
    }  
      
   (14)獲得上月最後一天的日期  
    public String getPreviousMonthEnd(){  
        String str = "";  
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
 
       Calendar lastDate = Calendar.getInstance();  
      lastDate.add(Calendar.MONTH,-1);//減一個月  
      lastDate.set(Calendar.DATE, 1);//把日期設定為當月第一天   
      lastDate.roll(Calendar.DATE, -1);//日期復原一天,也就是本月最後一天   
       str=sdf.format(lastDate.getTime());  
       return str;    
    }  
      
  (15)獲得下個月第一天的日期  
    public String getNextMonthFirst(){  
        String str = "";  
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
 
       Calendar lastDate = Calendar.getInstance();  
      lastDate.add(Calendar.MONTH,1);//減一個月  
      lastDate.set(Calendar.DATE, 1);//把日期設定為當月第一天   
       str=sdf.format(lastDate.getTime());  
       return str;    
    }  
      
  (16)獲得下個月最後一天的日期  
    public String getNextMonthEnd(){  
        String str = "";  
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
 
       Calendar lastDate = Calendar.getInstance();  
      lastDate.add(Calendar.MONTH,1);//加一個月  
      lastDate.set(Calendar.DATE, 1);//把日期設定為當月第一天   
      lastDate.roll(Calendar.DATE, -1);//日期復原一天,也就是本月最後一天   
       str=sdf.format(lastDate.getTime());  
       return str;    
    }  
      
    (17)獲得明年最後一天的日期  
    public String getNextYearEnd(){  
        String str = "";  
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
 
      Calendar lastDate = Calendar.getInstance();  
      lastDate.add(Calendar.YEAR,1);//加一個年  
      lastDate.set(Calendar.DAY_OF_YEAR, 1);  
     lastDate.roll(Calendar.DAY_OF_YEAR, -1);  
       str=sdf.format(lastDate.getTime());  
       return str;    
    }  
      
  (18)獲得明年第一天的日期  
    public String getNextYearFirst(){  
        String str = "";  
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
 
      Calendar lastDate = Calendar.getInstance();  
      lastDate.add(Calendar.YEAR,1);//加一個年  
      lastDate.set(Calendar.DAY_OF_YEAR, 1);  
           str=sdf.format(lastDate.getTime());  
      return str;    
          
    }  
      
  (19)獲得本年有多少天  
    private int getMaxYear(){  
        Calendar cd = Calendar.getInstance();  
        cd.set(Calendar.DAY_OF_YEAR,1);//把日期設為當年第一天  
        cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期復原一天。  
        int MaxYear = cd.get(Calendar.DAY_OF_YEAR);   
        return MaxYear;  
    }  
      }  
  (20)獲得本年第一天的日期  
    public String getCurrentYearFirst(){  
        int yearPlus = this.getYearPlus();  
        GregorianCalendar currentDate = new GregorianCalendar();  
        currentDate.add(GregorianCalendar.DATE,yearPlus);  
        Date yearDay = currentDate.getTime();  
        DateFormat df = DateFormat.getDateInstance();  
        String preYearDay = df.format(yearDay);  
        return preYearDay;  
    }  
      
      
 (21)獲得本年最後一天的日期 *  
    public String getCurrentYearEnd(){  
        Date date = new Date();  
        SimpleDateFormat   dateFormat   =   new   SimpleDateFormat("yyyy");//可以方便地修改日期格式     
        String  years  = dateFormat.format(date);     
        return years+"-12-31";  
    }  
      

首頁 1 2 末頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.