Android工具類--時間工具類


  1. /**  
  2.      * 獲取現在時間  
  3.      *  
  4.      * @return 返回時間類型 yyyy-MM-dd HH:mm:ss  
  5.      */  
  6.     public static Date getNowDate() {  
  7.         Date currentTime = new Date();  
  8.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  9.         String dateString = formatter.format(currentTime);  
  10.         ParsePosition pos = new ParsePosition(8);  
  11.         Date currentTime_2 = formatter.parse(dateString, pos);  
  12.         return currentTime_2;  
  13.     }  
  14.   
  15.     /**  
  16.      * 獲取現在時間  
  17.      *  
  18.      * @return返回短時間格式 yyyy-MM-dd  
  19.      */  
  20.     public static Date getNowDateShort() {  
  21.         Date currentTime = new Date();  
  22.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  23.         String dateString = formatter.format(currentTime);  
  24.         ParsePosition pos = new ParsePosition(8);  
  25.         Date currentTime_2 = formatter.parse(dateString, pos);  
  26.         return currentTime_2;  
  27.     }  
  28.   
  29.     /**  
  30.      * 獲取現在時間  
  31.      *  
  32.      * @return返回字符串格式 yyyy-MM-dd HH:mm:ss  
  33.      */  
  34.     public static String getStringDate() {  
  35.         Date currentTime = new Date();  
  36.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  37.         String dateString = formatter.format(currentTime);  
  38.         return dateString;  
  39.     }  
  40.   
  41.     /**  
  42.      * 獲取現在時間  
  43.      *  
  44.      * @return 返回短時間字符串格式 yyyy-MM-dd  
  45.      */  
  46.     public static String getStringDateShort() {  
  47.         Date currentTime = new Date();  
  48.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  49.         String dateString = formatter.format(currentTime);  
  50.         return dateString;  
  51.     }  
  52.   
  53.     /**  
  54.      * 獲取時間 小時:分;秒 HH:mm:ss  
  55.      *  
  56.      * @return  
  57.      */  
  58.     public static String getTimeShort() {  
  59.         SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");  
  60.         Date currentTime = new Date();  
  61.         String dateString = formatter.format(currentTime);  
  62.         return dateString;  
  63.     }  
  64.   
  65.     /**  
  66.      * 將長時間格式字符串轉換爲時間 yyyy-MM-dd HH:mm:ss  
  67.      *  
  68.      * @param strDate  
  69.      * @return  
  70.      */  
  71.     public static Date strToDateLong(String strDate) {  
  72.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  73.         ParsePosition pos = new ParsePosition(0);  
  74.         Date strtodate = formatter.parse(strDate, pos);  
  75.         return strtodate;  
  76.     }  
  77.   
  78.     /**  
  79.      * 將長時間格式時間轉換爲字符串 yyyy-MM-dd HH:mm:ss  
  80.      *  
  81.      * @param dateDate  
  82.      * @return  
  83.      */  
  84.     public static String dateToStrLong(java.util.Date dateDate) {  
  85.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  86.         String dateString = formatter.format(dateDate);  
  87.         return dateString;  
  88.     }  
  89.   
  90.     /**  
  91.      * 將短時間格式時間轉換爲字符串 yyyy-MM-dd  
  92.      *  
  93.      * @param dateDate  
  94.      * @return  
  95.      */  
  96.     public static String dateToStr(Date dateDate) {  
  97.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  98.         String dateString = formatter.format(dateDate);  
  99.         return dateString;  
  100.     }  
  101.   
  102.     /**  
  103.      * 將短時間格式字符串轉換爲時間 yyyy-MM-dd  
  104.      *  
  105.      * @param strDate  
  106.      * @return  
  107.      */  
  108.     public static Date strToDate(String strDate) {  
  109.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  110.         ParsePosition pos = new ParsePosition(0);  
  111.         Date strtodate = formatter.parse(strDate, pos);  
  112.         return strtodate;  
  113.     }  
  114.   
  115.     /**  
  116.      * 得到現在時間  
  117.      *  
  118.      * @return  
  119.      */  
  120.     public static Date getNow() {  
  121.         Date currentTime = new Date();  
  122.         return currentTime;  
  123.     }  
  124.   
  125.     /**  
  126.      * 提取一個月中的最後一天  
  127.      *  
  128.      * @param day  
  129.      * @return  
  130.      */  
  131.     public static Date getLastDate(long day) {  
  132.         Date date = new Date();  
  133.         long date_3_hm = date.getTime() - 3600000 * 34 * day;  
  134.         Date date_3_hm_date = new Date(date_3_hm);  
  135.         return date_3_hm_date;  
  136.     }  
  137.   
  138.     /**  
  139.      * 得到現在時間  
  140.      *  
  141.      * @return 字符串 yyyyMMdd HHmmss  
  142.      */  
  143.     public static String getStringToday() {  
  144.         Date currentTime = new Date();  
  145.         SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");  
  146.         String dateString = formatter.format(currentTime);  
  147.         return dateString;  
  148.     }  
  149.   
  150.     /**  
  151.      * 得到現在小時  
  152.      */  
  153.     public static String getHour() {  
  154.         Date currentTime = new Date();  
  155.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  156.         String dateString = formatter.format(currentTime);  
  157.         String hour;  
  158.         hour = dateString.substring(11, 13);  
  159.         return hour;  
  160.     }  
  161.   
  162.     /**  
  163.      * 得到現在分鐘  
  164.      *  
  165.      * @return  
  166.      */  
  167.     public static String getTime() {  
  168.         Date currentTime = new Date();  
  169.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  170.         String dateString = formatter.format(currentTime);  
  171.         String min;  
  172.         min = dateString.substring(14, 16);  
  173.         return min;  
  174.     }  
  175.   
  176.     /**  
  177.      * 根據用戶傳入的時間表示格式,返回當前時間的格式 如果是yyyyMMdd,注意字母y不能大寫。  
  178.      *  
  179.      * @param sformat yyyyMMddhhmmss  
  180.      * @return  
  181.      */  
  182.     public static String getUserDate(String sformat) {  
  183.         Date currentTime = new Date();  
  184.         SimpleDateFormat formatter = new SimpleDateFormat(sformat);  
  185.         String dateString = formatter.format(currentTime);  
  186.         return dateString;  
  187.     }  
  188.   
  189.     /**  
  190.      * 二個小時時間間的差值,必須保證二個時間都是”HH:MM”的格式,返回字符型的分鐘  
  191.      */  
  192.     public static String getTwoHour(String st1, String st2) {  
  193.         String[] kk = null;  
  194.         String[] jj = null;  
  195.         kk = st1.split(":");  
  196.         jj = st2.split(":");  
  197.         if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))  
  198.             return "0";  
  199.         else {  
  200.             double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;  
  201.             double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;  
  202.             if ((y - u) > 0)  
  203.                 return y - u + "";  
  204.             else  
  205.                 return "0";  
  206.         }  
  207.     }  
  208.   
  209.     /**  
  210.      * 得到二個日期間的間隔天數  
  211.      */  
  212.     public static String getTwoDay(String sj1, String sj2) {  
  213.         SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
  214.         long day = 0;  
  215.         try {  
  216.             java.util.Date date = myFormatter.parse(sj1);  
  217.             java.util.Date mydate = myFormatter.parse(sj2);  
  218.             day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
  219.         } catch (Exception e) {  
  220.             return "";  
  221.         }  
  222.         return day + "";  
  223.     }  
  224.   
  225.     /**  
  226.      * 時間前推或後推分鐘,其中JJ表示分鐘.  
  227.      */  
  228.     public static String getPreTime(String sj1, String jj) {  
  229.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  230.         String mydate1 = "";  
  231.         try {  
  232.             Date date1 = format.parse(sj1);  
  233.             long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;  
  234.             date1.setTime(Time * 1000);  
  235.             mydate1 = format.format(date1);  
  236.         } catch (Exception e) {  
  237.         }  
  238.         return mydate1;  
  239.     }  
  240.   
  241.     /**  
  242.      * 得到一個時間延後或前移幾天的時間,nowdate爲時間,delay爲前移或後延的天數  
  243.      */  
  244.     public static String getNextDay(String nowdate, String delay) {  
  245.         try {  
  246.             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  247.             String mdate = "";  
  248.             Date d = strToDate(nowdate);  
  249.             long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;  
  250.             d.setTime(myTime * 1000);  
  251.             mdate = format.format(d);  
  252.             return mdate;  
  253.         } catch (Exception e) {  
  254.             return "";  
  255.         }  
  256.     }  
  257.   
  258.     /**  
  259.      * 判斷是否潤年  
  260.      *  
  261.      * @param ddate  
  262.      * @return  
  263.      */  
  264.     public static boolean isLeapYear(String ddate) {  
  265.   
  266. /**  
  267.  * 詳細設計: 1.被400整除是閏年,否則: 2.不能被4整除則不是閏年 3.能被4整除同時不能被100整除則是閏年  
  268.  * 3.能被4整除同時能被100整除則不是閏年  
  269.  */  
  270.         Date d = strToDate(ddate);  
  271.         GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();  
  272.         gc.setTime(d);  
  273.         int year = gc.get(Calendar.YEAR);  
  274.         if ((year % 400) == 0)  
  275.             return true;  
  276.         else if ((year % 4) == 0) {  
  277.             if ((year % 100) == 0)  
  278.                 return false;  
  279.             else  
  280.                 return true;  
  281.         } else  
  282.             return false;  
  283.     }  
  284.   
  285.     /**  
  286.      * 返回美國時間格式 26 Apr 2006  
  287.      *  
  288.      * @param str  
  289.      * @return  
  290.      */  
  291.     public static String getEDate(String str) {  
  292.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  293.         ParsePosition pos = new ParsePosition(0);  
  294.         Date strtodate = formatter.parse(str, pos);  
  295.         String j = strtodate.toString();  
  296.         String[] k = j.split(" ");  
  297.         return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);  
  298.     }  
  299.   
  300.     /**  
  301.      * 獲取一個月的最後一天  
  302.      *  
  303.      * @param dat  
  304.      * @return  
  305.      */  
  306.     public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd  
  307.         String str = dat.substring(0, 8);  
  308.         String month = dat.substring(5, 7);  
  309.         int mon = Integer.parseInt(month);  
  310.         if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {  
  311.             str += "31";  
  312.         } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {  
  313.             str += "30";  
  314.         } else {  
  315.             if (isLeapYear(dat)) {  
  316.                 str += "29";  
  317.             } else {  
  318.                 str += "28";  
  319.             }  
  320.         }  
  321.         return str;  
  322.     }  
  323.   
  324.     /**  
  325.      * 判斷二個時間是否在同一個周  
  326.      *  
  327.      * @param date1  
  328.      * @param date2  
  329.      * @return  
  330.      */  
  331.     public static boolean isSameWeekDates(Date date1, Date date2) {  
  332.         Calendar cal1 = Calendar.getInstance();  
  333.         Calendar cal2 = Calendar.getInstance();  
  334.         cal1.setTime(date1);  
  335.         cal2.setTime(date2);  
  336.         int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);  
  337.         if (0 == subYear) {  
  338.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))  
  339.                 return true;  
  340.         } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {  
  341. // 如果12月的最後一週橫跨來年第一週的話則最後一週即算做來年的第一週  
  342.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))  
  343.                 return true;  
  344.         } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {  
  345.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))  
  346.                 return true;  
  347.         }  
  348.         return false;  
  349.     }  
  350.   
  351.     /**  
  352.      * 產生周序列,即得到當前時間所在的年度是第幾周  
  353.      *  
  354.      * @return  
  355.      */  
  356.     public static String getSeqWeek() {  
  357.         Calendar c = Calendar.getInstance(Locale.CHINA);  
  358.         String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));  
  359.         if (week.length() == 1)  
  360.             week = "0" + week;  
  361.         String year = Integer.toString(c.get(Calendar.YEAR));  
  362.         return year + week;  
  363.     }  
  364.   
  365.     /**  
  366.      * 獲得一個日期所在的周的星期幾的日期,如要找出2002年2月3日所在周的星期一是幾號  
  367.      *  
  368.      * @param sdate  
  369.      * @param num  
  370.      * @return  
  371.      */  
  372.     public static String getWeek(String sdate, String num) {  
  373. // 再轉換爲時間  
  374.         Date dd = DateFormat.strToDate(sdate);  
  375.         Calendar c = Calendar.getInstance();  
  376.         c.setTime(dd);  
  377.         if (num.equals("1")) // 返回星期一所在的日期  
  378.             c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
  379.         else if (num.equals("2")) // 返回星期二所在的日期  
  380.             c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);  
  381.         else if (num.equals("3")) // 返回星期三所在的日期  
  382.             c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);  
  383.         else if (num.equals("4")) // 返回星期四所在的日期  
  384.             c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);  
  385.         else if (num.equals("5")) // 返回星期五所在的日期  
  386.             c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);  
  387.         else if (num.equals("6")) // 返回星期六所在的日期  
  388.             c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);  
  389.         else if (num.equals("0")) // 返回星期日所在的日期  
  390.             c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);  
  391.         return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());  
  392.     }  
  393.   
  394.     /**  
  395.      * 根據一個日期,返回是星期幾的字符串  
  396.      *  
  397.      * @param sdate  
  398.      * @return  
  399.      */  
  400.     public static String getWeek(String sdate) {  
  401. // 再轉換爲時間  
  402.         Date date = DateFormat.strToDate(sdate);  
  403.         Calendar c = Calendar.getInstance();  
  404.         c.setTime(date);  
  405. // int hour=c.get(Calendar.DAY_OF_WEEK);  
  406. // hour中存的就是星期幾了,其範圍 1~7  
  407. // 1=星期日 7=星期六,其他類推  
  408.         return new SimpleDateFormat("EEEE").format(c.getTime());  
  409.     }  
  410.   
  411.     public static String getWeekStr(String sdate) {  
  412.         String str = "";  
  413.         str = DateFormat.getWeek(sdate);  
  414.         if ("1".equals(str)) {  
  415.             str = "星期日";  
  416.         } else if ("2".equals(str)) {  
  417.             str = "星期一";  
  418.         } else if ("3".equals(str)) {  
  419.             str = "星期二";  
  420.         } else if ("4".equals(str)) {  
  421.             str = "星期三";  
  422.         } else if ("5".equals(str)) {  
  423.             str = "星期四";  
  424.         } else if ("6".equals(str)) {  
  425.             str = "星期五";  
  426.         } else if ("7".equals(str)) {  
  427.             str = "星期六";  
  428.         }  
  429.         return str;  
  430.     }  
  431.   
  432.     /**  
  433.      * 兩個時間之間的天數  
  434.      *  
  435.      * @param date1  
  436.      * @param date2  
  437.      * @return  
  438.      */  
  439.     public static long getDays(String date1, String date2) {  
  440.         if (date1 == null || date1.equals(""))  
  441.             return 0;  
  442.         if (date2 == null || date2.equals(""))  
  443.             return 0;  
  444. // 轉換爲標準時間  
  445.         SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");  
  446.         java.util.Date date = null;  
  447.         java.util.Date mydate = null;  
  448.         try {  
  449.             date = myFormatter.parse(date1);  
  450.             mydate = myFormatter.parse(date2);  
  451.         } catch (Exception e) {  
  452.         }  
  453.         long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);  
  454.         return day;  
  455.     }  
  456.   
  457.     /**  
  458.      * 形成如下的日曆 , 根據傳入的一個時間返回一個結構 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是當月的各個時間  
  459.      * 此函數返回該日曆第一行星期日所在的日期  
  460.      *  
  461.      * @param sdate  
  462.      * @return  
  463.      */  
  464.     public static String getNowMonth(String sdate) {  
  465. // 取該時間所在月的一號  
  466.         sdate = sdate.substring(0, 8) + "01";  
  467.   
  468. // 得到這個月的1號是星期幾  
  469.         Date date = DateFormat.strToDate(sdate);  
  470.         Calendar c = Calendar.getInstance();  
  471.         c.setTime(date);  
  472.         int u = c.get(Calendar.DAY_OF_WEEK);  
  473.         String newday = DateFormat.getNextDay(sdate, (1 - u) + "");  
  474.         return newday;  
  475.     }  
  476.   
  477.     /**  
  478.      * 取得數據庫主鍵 生成格式爲yyyymmddhhmmss+k位隨機數  
  479.      *  
  480.      * @param k 表示是取幾位隨機數,可以自己定  
  481.      */  
  482.   
  483.     public static String getNo(int k) {  
  484.   
  485.         return getUserDate("yyyyMMddhhmmss") + getRandom(k);  
  486.     }  
  487.   
  488.     /**  
  489.      * 返回一個隨機數  
  490.      *  
  491.      * @param i  
  492.      * @return  
  493.      */  
  494.     public static String getRandom(int i) {  
  495.         Random jjj = new Random();  
  496. // int suiJiShu = jjj.nextInt(9);  
  497.         if (i == 0)  
  498.             return "";  
  499.         String jj = "";  
  500.         for (int k = 0; k < i; k++) {  
  501.             jj = jj + jjj.nextInt(9);  
  502.         }  
  503.         return jj;  
  504.     }  
  505.   
  506.     /**  
  507.      * @param date  
  508.      */  
  509.     public static boolean RightDate(String date) {  
  510.   
  511.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  512.         ;  
  513.         if (date == null)  
  514.             return false;  
  515.         if (date.length() > 10) {  
  516.             sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  517.         } else {  
  518.             sdf = new SimpleDateFormat("yyyy-MM-dd");  
  519.         }  
  520.         try {  
  521.             sdf.parse(date);  
  522.         } catch (ParseException pe) {  
  523.             return false;  
  524.         }  
  525.         return true;  
  526.     }</span>  



[html] view plain copy
  1. /**  
  2.  * 日期時間工具類  
  3.  */  
  4. public final class DateUtils {  
  5.     public static final String YMD_PATTERN = "yyyy-MM-dd";  
  6.     public static final String YMD_PATTERN2 = "yyyy/MM/dd";  
  7.     public static final String YMD_PATTERN3 = "yyyy/MM/dd HH:mm";  
  8.     public static final String YMD_HMS_PATTERN = "yyyy-MM-dd HH:mm:ss";  
  9.     public static final String YMD_HMS_PATTERN2 = "yyyy-MM-dd HH:mm";  
  10.     public static final String[] weeks = new String[]{"週日", "週一", "週二", "週三", "週四", "週五", "週六"};  
  11.     private static final String TAG = DateUtils.class.getSimpleName();  
  12.     private static final SimpleDateFormat mDataFormat = new SimpleDateFormat(YMD_PATTERN);  
  13.     private static final SimpleDateFormat mTimeFormat = new SimpleDateFormat(YMD_HMS_PATTERN);  
  14.   
  15.   
  16.     //    private long mToday;  
  17. //  
  18. //    {  
  19. //        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");  
  20. //        Date date = null;  
  21. //        try {  
  22. //            date = format.parse(format.format(new Date()));  
  23. //            mToday = date.getTime();  
  24. //        } catch (ParseException e) {  
  25. //            e.printStackTrace();  
  26. //        }  
  27. //    }  
  28.   
  29.     private static final int ONEDAY = 24 * 60 * 60 * 1000;  
  30.   
  31.     /**  
  32.      * 通過先將 當前時間 格式化成 年月日形式 ,然後又將所得到的字符串 解析成 Date類型 即 當天0點  
  33.      *  
  34.      * @return 當天 0 點 時的毫秒值  
  35.      * @throws ParseException  
  36.      */  
  37.     public static long getDateToTime(Date date) throws ParseException {  
  38.         return mDataFormat.parse(mDataFormat.format(date)).getTime();  
  39.     }  
  40.   
  41.     /**  
  42.      * 通過毫秒值  獲取 當天 0 點的毫秒值  
  43.      *  
  44.      * @param millis  
  45.      * @return 當天 0 點時的毫秒值  
  46.      * @throws ParseException  
  47.      */  
  48.     public static long getMillisToTime(long millis) throws ParseException {  
  49.         return getDateToTime(new Date(millis));  
  50.     }  
  51.   
  52.     /**  
  53.      * 通過 秒值 獲取當天 0 點的毫秒值  
  54.      *  
  55.      * @param seconds  
  56.      * @return 當天 0 點時的毫秒值  
  57.      * @throws ParseException  
  58.      */  
  59.     public static long getSecondsToTime(int seconds) throws ParseException {  
  60.         return getMillisToTime(seconds * 1000L);  
  61.     }  
  62.   
  63.     /**  
  64.      * 根據毫秒值 與 傳入的日期  顯示與之相近的日期格式的顯示  
  65.      *  
  66.      * @param millis  
  67.      * @param date  
  68.      * @return 具體 (日期自己傳入)對應日期格式顯示的字符串  
  69.      * @throws ParseException  
  70.      */  
  71.     public static String getMillisToDate(long millis, Date date) throws ParseException {  
  72.   
  73.         Log.d("tag", mDataFormat.format(new Date(millis)));  
  74.         if (millis > getDateToTime(date)) {  
  75.             return millisecondToDate(millis, "H:dd");  
  76.         } else if ((millis + 1 * ONEDAY) >= getDateToTime(date)) {  
  77.             return "昨天" + millisecondToDate(millis, "H:dd");  
  78.         } else if ((millis + 3 * ONEDAY) >= getDateToTime(date)) {  
  79.             return millisecondToDate(millis, "E H:dd");  
  80.         } else {  
  81.             return millisecondToDate(millis, "yyyy年M月d日 H:dd");  
  82.         }  
  83.     }  
  84.   
  85.     /**  
  86.      * 根據  秒值 與 傳入的日期  顯示與之相近的日期格式的顯示  
  87.      *  
  88.      * @param seconds  
  89.      * @param date  
  90.      * @return 具體 (日期自己傳入) 對應日期格式顯示的字符串  
  91.      * @throws ParseException  
  92.      */  
  93.     public static String getSecondsToDate(int seconds, Date date) throws ParseException {  
  94.         return getMillisToDate(seconds * 1000L, date);  
  95.     }  
  96.   
  97.     /**  
  98.      * 根據毫秒值  顯示與之   當前日期   相近的日期格式的顯示  
  99.      *  
  100.      * @param millis  
  101.      * @return 具體(當前日期) 對應日期格式顯示的字符串  
  102.      * @throws ParseException  
  103.      */  
  104.     public static String getMillisToDate(long millis) throws ParseException {  
  105.         Long time = System.currentTimeMillis();  
  106.         return getMillisToDate(millis, new Date(time));  
  107.     }  
  108.   
  109.     /**  
  110.      * 根據秒值  顯示與之   當前日期   相近的日期格式的顯示  
  111.      *  
  112.      * @param seconds  
  113.      * @return 具體(當前日期) 對應日期格式顯示的字符串  
  114.      * @throws ParseException  
  115.      */  
  116.     public static String getSecondsToDate(int seconds) throws ParseException {  
  117.         return getMillisToDate(seconds * 1000L);  
  118.     }  
  119.   
  120.     public static long getStringToMills(String dateStr, String pattern) {  
  121.         SimpleDateFormat format = new SimpleDateFormat(pattern);  
  122.         Date date = null;  
  123.         try {  
  124.             date = format.parse(dateStr);  
  125.             return date.getTime();  
  126.         } catch (ParseException e) {  
  127.             e.printStackTrace();  
  128.         }  
  129.         return 0;  
  130.     }  
  131.   
  132.     /**  
  133.      * 計算兩個日期相差的天數,是否取絕對值  
  134.      *  
  135.      * @param date1 第一個日期  
  136.      * @param date2 第二個日期  
  137.      * @param isAbs 是否去絕對值  
  138.      * @return 相差天數  
  139.      */  
  140.     public static int getDaysUnAbs(String date1, String date2, boolean isAbs) {  
  141.         int day = 0;  
  142.         if (StringUtils.isEmpty(date1) || StringUtils.isEmpty(date2))  
  143.             return 0;  
  144.         try {  
  145.             Date date = mDataFormat.parse(date1);  
  146.             Date myDate = mDataFormat.parse(date2);  
  147.             day = (int) ((date.getTime() - myDate.getTime()) / (24 * 60 * 60 * 1000));  
  148.         } catch (ParseException e) {  
  149.             e.printStackTrace();  
  150.             Logger.e(TAG, e);  
  151.         }  
  152.         return isAbs ? Math.abs(day) : day;  
  153.     }  
  154.   
  155.     /**  
  156.      * 計算兩個日期相差的天數,是否取絕對值  
  157.      *  
  158.      * @param date1 第一個日期  
  159.      * @param date2 第二個日期  
  160.      * @return 相差天數  
  161.      */  
  162.     public static int getDaysUnAbs(String date1, String date2) {  
  163.         return getDaysUnAbs(date1, date2, true);  
  164.     }  
  165.   
  166.     /**  
  167.      * 將長時間格式字符串轉換爲時間 yyyy-MM-dd HH:mm:ss  
  168.      *  
  169.      * @param date 日期  
  170.      * @return 長時間格式日期  
  171.      */  
  172.     public static Date strToDateLong(String date) {  
  173.         ParsePosition pos = new ParsePosition(0);  
  174.         return mTimeFormat.parse(date, pos);  
  175.     }  
  176.   
  177.     /**  
  178.      * 獲取當前的日期時間  
  179.      *  
  180.      * @return 當前的日期時間  
  181.      */  
  182.     public static String getCurrentTime() {  
  183.         return mTimeFormat.format(new Date());  
  184.     }  
  185.   
  186.     /**  
  187.      * 獲取當前的日期  
  188.      *  
  189.      * @return 當前的日期  
  190.      */  
  191.     public static String getCurrentDate() {  
  192.         return mDataFormat.format(new Date());  
  193.     }  
  194.   
  195.     /**  
  196.      * 獲取輸入日期是星期幾  
  197.      *  
  198.      * @param date 日期  
  199.      * @return 星期幾  
  200.      */  
  201.     public static String getWeekDate(Date date) {  
  202.         Calendar calendar = Calendar.getInstance(TimeZone.getDefault());  
  203.         calendar.setTime(date);  
  204.         return weeks[calendar.get(calendar.DAY_OF_WEEK) - 1];  
  205.     }  
  206.   
  207.     /**  
  208.      * 獲取輸入日期是星期幾  
  209.      *  
  210.      * @param date 日期  
  211.      * @return 星期幾  
  212.      */  
  213.     public static String getWeekDate(String date) {  
  214.         try {  
  215.             Date d = mDataFormat.parse(date);  
  216.             return getWeekDate(d);  
  217.         } catch (ParseException e) {  
  218.             e.printStackTrace();  
  219.             Logger.e(TAG, e);  
  220.         }  
  221.         return "星期數未知";  
  222.     }  
  223.   
  224.     /**  
  225.      * 毫秒轉日期  
  226.      *  
  227.      * @param millisecond 毫秒數  
  228.      * @param pattern     正則式  
  229.      * @return 日期  
  230.      */  
  231.     public static String millisecondToDate(long millisecond, String pattern) {  
  232.         SimpleDateFormat format = new SimpleDateFormat(pattern);  
  233.         Date date = new Date(millisecond);  
  234.         return format.format(date);  
  235.     }  
  236.   
  237.     /**  
  238.      * 秒轉日期  
  239.      *  
  240.      * @param second  秒數  
  241.      * @param pattern 正則式  
  242.      * @return 日期  
  243.      */  
  244.     public static String secondToDate(int second, String pattern) {  
  245.         SimpleDateFormat format = new SimpleDateFormat(pattern);  
  246.         Date date = new Date(second * 1000L);  
  247.         return format.format(date);  
  248.     }  
  249.   
  250.     /**  
  251.      * 時間計算  
  252.      *  
  253.      * @param millisecond 毫秒數  
  254.      * @return 計算後顯示的文字  
  255.      */  
  256.     public static String timeCalculate(long millisecond) {  
  257.         String result = "";  
  258.         long diff = System.currentTimeMillis() - millisecond;//時間差  
  259.         if (diff < 1000 * 60/* && diff > 0*/)  
  260.             return "剛剛";  
  261.         else if (diff >= 1000 * 60 && diff < 1000 * 60 * 60)  
  262.             return diff / (1000 * 60) + "分鐘以前";  
  263.         else if (diff >= 1000 * 60 * 60 && diff < 1000 * 60 * 60 * 24)  
  264.             return diff / (1000 * 60 * 60) + "小時以前";  
  265.         else if (diff >= 1000 * 60 * 60 * 24)  
  266.             return diff / (1000 * 60 * 60 * 24) + "天以前";  
  267. //        else if (diff < 0)  
  268. //            return "輸入時間在當前時間之後,不可以計算";  
  269.         return result;  
  270.     }  
  271.   
  272.     /**  
  273.      * 時間計算  
  274.      *  
  275.      * @param second 秒數  
  276.      * @return 計算後顯示的文字  
  277.      */  
  278.     public static String timeCalculate(int second) {  
  279.         return timeCalculate(second * 1000L);  
  280.     }  
  281.   
  282.     /**  
  283.      * @param args  
  284.      * @throws Exception  
  285.      */  
  286.     public static void main(String[] args) throws Exception {  
  287.         Date date = mTimeFormat.parse("2015-12-8 18:08:00");  
  288.         System.out.print(timeCalculate((int) (date.getTime() / 1000)));  
  289.     }  
  290.   
  291.     /**  
  292.      * 把long時間轉換成時間格式字符串  
  293.      *  
  294.      * @param time 時間  
  295.      * @return  
  296.      */  
  297.     public static String generateTime(long time) {  
  298.         int totalSeconds = (int) (time / 1000);  
  299.         int seconds = totalSeconds % 60;  
  300.         int minutes = (totalSeconds / 60) % 60;  
  301.         int hours = totalSeconds / 3600;  
  302.   
  303.         return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);  
  304.     }  
  305.   
  306.     /**  
  307.      * 獲取年  
  308.      *  
  309.      * @param mills  
  310.      * @return  
  311.      */  
  312.     public static int getYearInMills(long mills) {  
  313.         Calendar calendar = Calendar.getInstance();  
  314.         calendar.setTimeInMillis(mills);  
  315.         return calendar.get(Calendar.YEAR);  
  316.     }  
  317.   
  318.   
  319.     /**  
  320.      * 獲取月份  
  321.      *  
  322.      * @param mills  
  323.      * @return  
  324.      */  
  325.     public static int getMonthInMills(long mills) {  
  326.         Calendar calendar = Calendar.getInstance();  
  327.         calendar.setTimeInMillis(mills);  
  328.         return calendar.get(Calendar.MONTH) + 1;  
  329.     }  
  330.   
  331.     /**  
  332.      * 獲取天數  
  333.      *  
  334.      * @param mills  
  335.      * @return  
  336.      */  
  337.     public static int getDayInMills(long mills) {  
  338.         Calendar calendar = Calendar.getInstance();  
  339.         calendar.setTimeInMillis(mills);  
  340.         return calendar.get(Calendar.DAY_OF_MONTH);  
  341.     }  
  342. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章