Java获取各种常用时间方法

本人之前做的项目中积累常用的时间方法,贴出来与大家共享,如果哪里有错误或更有效的方法请尽管拍砖!哈哈

Java代码 复制代码
  1. package com.hefeng.test;  
  2.   
  3.   
  4. import java.text.DateFormat;  
  5. import java.text.ParsePosition;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Calendar;  
  8. import java.util.Date;  
  9. import java.util.GregorianCalendar;  
  10.   
  11. public class TimeTest {  
  12.     //用来全局控制 上一周,本周,下一周的周数变化  
  13.     private  int weeks = 0;  
  14.     private int MaxDate;//一月最大天数  
  15.     private int MaxYear;//一年最大天数  
  16.       
  17.       
  18.     /**
  19.       * @param args
  20.       */  
  21.     public static void main(String[] args) {  
  22.          TimeTest tt = new TimeTest();  
  23.          System.out.println("获取当天日期:"+tt.getNowTime("yyyy-MM-dd"));  
  24.          System.out.println("获取本周一日期:"+tt.getMondayOFWeek());  
  25.          System.out.println("获取本周日的日期~:"+tt.getCurrentWeekday());  
  26.          System.out.println("获取上周一日期:"+tt.getPreviousWeekday());  
  27.          System.out.println("获取上周日日期:"+tt.getPreviousWeekSunday());  
  28.          System.out.println("获取下周一日期:"+tt.getNextMonday());  
  29.          System.out.println("获取下周日日期:"+tt.getNextSunday());  
  30.          System.out.println("获得相应周的周六的日期:"+tt.getNowTime("yyyy-MM-dd"));  
  31.          System.out.println("获取本月第一天日期:"+tt.getFirstDayOfMonth());  
  32.          System.out.println("获取本月最后一天日期:"+tt.getDefaultDay());  
  33.          System.out.println("获取上月第一天日期:"+tt.getPreviousMonthFirst());  
  34.          System.out.println("获取上月最后一天的日期:"+tt.getPreviousMonthEnd());  
  35.          System.out.println("获取下月第一天日期:"+tt.getNextMonthFirst());  
  36.          System.out.println("获取下月最后一天日期:"+tt.getNextMonthEnd());  
  37.          System.out.println("获取本年的第一天日期:"+tt.getCurrentYearFirst());  
  38.          System.out.println("获取本年最后一天日期:"+tt.getCurrentYearEnd());  
  39.          System.out.println("获取去年的第一天日期:"+tt.getPreviousYearFirst());  
  40.          System.out.println("获取去年的最后一天日期:"+tt.getPreviousYearEnd());  
  41.          System.out.println("获取明年第一天日期:"+tt.getNextYearFirst());  
  42.          System.out.println("获取明年最后一天日期:"+tt.getNextYearEnd());  
  43.          System.out.println("获取本季度第一天到最后一天:"+tt.getThisSeasonTime(11));  
  44.          System.out.println("获取两个日期之间间隔天数2008-12-1~2008-9.29:"+TimeTest.getTwoDay("2008-12-1","2008-9-29"));  
  45.      }  
  46.       
  47.       
  48.     /**
  49.          * 得到二个日期间的间隔天数
  50.          */  
  51.     public static String getTwoDay(String sj1, String sj2) {  
  52.          SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
  53.         long day = 0;  
  54.         try {  
  55.           java.util.Date date = myFormatter.parse(sj1);  
  56.           java.util.Date mydate = myFormatter.parse(sj2);  
  57.           day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
  58.          } catch (Exception e) {  
  59.          return "";  
  60.          }  
  61.         return day + "";  
  62.      }  
  63.   
  64.   
  65.     /**
  66.          * 根据一个日期,返回是星期几的字符串
  67.          *
  68.          * @param sdate
  69.          * @return
  70.          */  
  71.     public static String getWeek(String sdate) {  
  72.         // 再转换为时间  
  73.          Date date = TimeTest.strToDate(sdate);  
  74.          Calendar c = Calendar.getInstance();  
  75.          c.setTime(date);  
  76.         // int hour=c.get(Calendar.DAY_OF_WEEK);  
  77.         // hour中存的就是星期几了,其范围 1~7  
  78.         // 1=星期日 7=星期六,其他类推  
  79.         return new SimpleDateFormat("EEEE").format(c.getTime());  
  80.      }  
  81.   
  82.     /**
  83.          * 将短时间格式字符串转换为时间 yyyy-MM-dd
  84.          *
  85.          * @param strDate
  86.          * @return
  87.          */  
  88.     public static Date strToDate(String strDate) {  
  89.          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  90.          ParsePosition pos = new ParsePosition(0);  
  91.          Date strtodate = formatter.parse(strDate, pos);  
  92.         return strtodate;  
  93.      }  
  94.   
  95.     /**
  96.          * 两个时间之间的天数
  97.          *
  98.          * @param date1
  99.          * @param date2
  100.          * @return
  101.          */  
  102.     public static long getDays(String date1, String date2) {  
  103.         if (date1 == null || date1.equals(""))  
  104.          return 0;  
  105.         if (date2 == null || date2.equals(""))  
  106.          return 0;  
  107.         // 转换为标准时间  
  108.          SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
  109.          java.util.Date date = null;  
  110.          java.util.Date mydate = null;  
  111.         try {  
  112.           date = myFormatter.parse(date1);  
  113.           mydate = myFormatter.parse(date2);  
  114.          } catch (Exception e) {  
  115.          }  
  116.         long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
  117.         return day;  
  118.      }  
  119.   
  120.   
  121.   
  122.       
  123.     // 计算当月最后一天,返回字符串  
  124.     public String getDefaultDay(){    
  125.         String str = "";  
  126.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
  127.   
  128.         Calendar lastDate = Calendar.getInstance();  
  129.         lastDate.set(Calendar.DATE,1);//设为当前月的1号  
  130.         lastDate.add(Calendar.MONTH,1);//加一个月,变为下月的1号  
  131.         lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天  
  132.          
  133.         str=sdf.format(lastDate.getTime());  
  134.        return str;    
  135.      }  
  136.       
  137.     // 上月第一天  
  138.     public String getPreviousMonthFirst(){    
  139.         String str = "";  
  140.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
  141.   
  142.         Calendar lastDate = Calendar.getInstance();  
  143.         lastDate.set(Calendar.DATE,1);//设为当前月的1号  
  144.         lastDate.add(Calendar.MONTH,-1);//减一个月,变为下月的1号  
  145.        //lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天  
  146.          
  147.         str=sdf.format(lastDate.getTime());  
  148.        return str;    
  149.      }  
  150.       
  151.     //获取当月第一天  
  152.     public String getFirstDayOfMonth(){    
  153.         String str = "";  
  154.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
  155.   
  156.         Calendar lastDate = Calendar.getInstance();  
  157.         lastDate.set(Calendar.DATE,1);//设为当前月的1号  
  158.         str=sdf.format(lastDate.getTime());  
  159.        return str;    
  160.      }  
  161.       
  162.     // 获得本周星期日的日期    
  163.     public String getCurrentWeekday() {  
  164.          weeks = 0;  
  165.         int mondayPlus = this.getMondayPlus();  
  166.          GregorianCalendar currentDate = new GregorianCalendar();  
  167.          currentDate.add(GregorianCalendar.DATE, mondayPlus+6);  
  168.          Date monday = currentDate.getTime();  
  169.           
  170.          DateFormat df = DateFormat.getDateInstance();  
  171.          String preMonday = df.format(monday);  
  172.         return preMonday;  
  173.      }  
  174.       
  175.       
  176.     //获取当天时间   
  177.     public String getNowTime(String dateformat){  
  178.          Date    now    =   new    Date();     
  179.          SimpleDateFormat    dateFormat    =   new    SimpleDateFormat(dateformat);//可以方便地修改日期格式     
  180.          String   hehe   = dateFormat.format(now);     
  181.         return hehe;  
  182.      }  
  183.       
  184.     // 获得当前日期与本周日相差的天数  
  185.     private int getMondayPlus() {  
  186.          Calendar cd = Calendar.getInstance();  
  187.         // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......  
  188.         int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;         //因为按中国礼拜一作为第一天所以这里减1  
  189.         if (dayOfWeek == 1) {  
  190.             return 0;  
  191.          } else {  
  192.             return 1 - dayOfWeek;  
  193.          }  
  194.      }  
  195.       
  196.     //获得本周一的日期  
  197.     public String getMondayOFWeek(){  
  198.           weeks = 0;  
  199.          int mondayPlus = this.getMondayPlus();  
  200.           GregorianCalendar currentDate = new GregorianCalendar();  
  201.           currentDate.add(GregorianCalendar.DATE, mondayPlus);  
  202.           Date monday = currentDate.getTime();  
  203.            
  204.           DateFormat df = DateFormat.getDateInstance();  
  205.           String preMonday = df.format(monday);  
  206.          return preMonday;  
  207.      }  
  208.       
  209.   //获得相应周的周六的日期  
  210.     public String getSaturday() {  
  211.         int mondayPlus = this.getMondayPlus();  
  212.          GregorianCalendar currentDate = new GregorianCalendar();  
  213.          currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);  
  214.          Date monday = currentDate.getTime();  
  215.          DateFormat df = DateFormat.getDateInstance();  
  216.          String preMonday = df.format(monday);  
  217.         return preMonday;  
  218.      }  
  219.       
  220. // 获得上周星期日的日期  
  221.     public String getPreviousWeekSunday() {  
  222.          weeks=0;  
  223.          weeks--;  
  224.         int mondayPlus = this.getMondayPlus();  
  225.          GregorianCalendar currentDate = new GregorianCalendar();  
  226.          currentDate.add(GregorianCalendar.DATE, mondayPlus+weeks);  
  227.          Date monday = currentDate.getTime();  
  228.          DateFormat df = DateFormat.getDateInstance();  
  229.          String preMonday = df.format(monday);  
  230.         return preMonday;  
  231.      }  
  232.   
  233. // 获得上周星期一的日期  
  234.     public String getPreviousWeekday() {  
  235.          weeks--;  
  236.         int mondayPlus = this.getMondayPlus();  
  237.          GregorianCalendar currentDate = new GregorianCalendar();  
  238.          currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);  
  239.          Date monday = currentDate.getTime();  
  240.          DateFormat df = DateFormat.getDateInstance();  
  241.          String preMonday = df.format(monday);  
  242.         return preMonday;  
  243.      }  
原文地址:https://www.cnblogs.com/danghuijian/p/4400772.html