用于处理java当中各种使用到日期的方法

Java代码 复制代码  收藏代码
  1. package com.pdt.util;   
  2.   
  3. import java.text.ParseException;   
  4. import java.text.SimpleDateFormat;   
  5. import java.util.ArrayList;   
  6. import java.util.Calendar;   
  7. import java.util.Date;   
  8. import java.util.List;   
  9.   
  10. /**  
  11.  * 日期工具类  
  12.  * 默认使用 "yyyy-MM-dd HH:mm:ss" 格式化日期  
  13.  * @author EwinLive  
  14.  * @version1.0  
  15.  */  
  16. public class DateUtils {   
  17.     /**  
  18.      * 英文简写(默认)如:2010-12-01  
  19.      */  
  20.     public static String FORMAT_SHORT = "yyyy-MM-dd";   
  21.        
  22.     /**  
  23.      * 英文全称  如:2010-12-01 23:15:06  
  24.      */  
  25.     public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";   
  26.        
  27.     /**  
  28.      * 精确到毫秒的完整时间    如:yyyy-MM-dd HH:mm:ss.S  
  29.      */  
  30.     public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";   
  31.        
  32.     /**  
  33.      * 中文简写  如:2010年12月01日  
  34.      */  
  35.     public static String FORMAT_SHORT_CN = "yyyy年MM月dd";   
  36.        
  37.     /**  
  38.      * 中文全称  如:2010年12月01日  23时15分06秒  
  39.      */  
  40.     public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";   
  41.        
  42.     /**  
  43.      * 精确到毫秒的完整中文时间  
  44.      */  
  45.     public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";   
  46.   
  47.     /**  
  48.      * 获得默认的 date pattern  
  49.      */  
  50.     public static String getDatePattern() {   
  51.         return FORMAT_LONG;   
  52.     }   
  53.   
  54.     /**  
  55.      * 根据预设格式返回当前日期  
  56.      * @return  
  57.      */  
  58.     public static String getNow() {   
  59.         return format(new Date());   
  60.     }   
  61.        
  62.     /**  
  63.      * 根据用户格式返回当前日期  
  64.      * @param format  
  65.      * @return  
  66.      */  
  67.     public static String getNow(String format) {   
  68.         return format(new Date(), format);   
  69.     }   
  70.   
  71.        
  72.     /**  
  73.      * 使用预设格式格式化日期  
  74.      * @param date  
  75.      * @return  
  76.      */  
  77.     public static String format(Date date) {   
  78.         return format(date, getDatePattern());   
  79.     }   
  80.   
  81.     /**  
  82.      * 使用用户格式格式化日期  
  83.      * @param date 日期  
  84.      * @param pattern 日期格式  
  85.      * @return  
  86.      */  
  87.     public static String format(Date date, String pattern) {   
  88.         String returnValue = "";   
  89.         if (date != null) {   
  90.             SimpleDateFormat df = new SimpleDateFormat(pattern);   
  91.             returnValue = df.format(date);   
  92.         }   
  93.         return (returnValue);   
  94.     }   
  95.   
  96.     /**  
  97.      * 使用预设格式提取字符串日期  
  98.      * @param strDate 日期字符串  
  99.      * @return  
  100.      */  
  101.     public static Date parse(String strDate) {   
  102.         return parse(strDate, getDatePattern());   
  103.     }   
  104.   
  105.     /**  
  106.      * 使用用户格式提取字符串日期  
  107.      * @param strDate 日期字符串  
  108.      * @param pattern 日期格式  
  109.      * @return  
  110.      */  
  111.     public static Date parse(String strDate, String pattern) {   
  112.         SimpleDateFormat df = new SimpleDateFormat(pattern);   
  113.         try {   
  114.             return df.parse(strDate);   
  115.         } catch (ParseException e) {   
  116.             e.printStackTrace();   
  117.             return null;   
  118.         }   
  119.     }   
  120.        
  121.     /**  
  122.      * 使用用户格式提取字符串日期  
  123.      * @param strDate 日期字符串  
  124.      * @param pattern 日期格式  
  125.      * @return  
  126.      */  
  127.     public static String parseDate(String strDate, String pattern) {   
  128.         SimpleDateFormat df = new SimpleDateFormat(pattern);   
  129.         try {   
  130.             return df.format(df.parse(strDate));   
  131.         } catch (ParseException e) {   
  132.             e.printStackTrace();   
  133.             return null;   
  134.         }   
  135.     }   
  136.   
  137.   
  138.     /**  
  139.      * 在日期上增加数个整月  
  140.      * @param date 日期  
  141.      * @param n 要增加的月数  
  142.      * @return  
  143.      */  
  144.     public static Date addMonth(Date date, int n) {   
  145.         Calendar cal = Calendar.getInstance();   
  146.         cal.setTime(date);   
  147.         cal.add(Calendar.MONTH, n);   
  148.         return cal.getTime();   
  149.     }   
  150.   
  151.     /**  
  152.      * 在日期上增加天数  
  153.      * @param date 日期  
  154.      * @param n 要增加的天数  
  155.      * @return  
  156.      */  
  157.     public static Date addDay(Date date, int n) {   
  158.         Calendar cal = Calendar.getInstance();   
  159.         cal.setTime(date);   
  160.         cal.add(Calendar.DATE, n);   
  161.         return cal.getTime();   
  162.     }   
  163.   
  164.     /**  
  165.      * 获取时间戳  
  166.      */  
  167.     public static String getTimeString() {   
  168.         SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);   
  169.         Calendar calendar = Calendar.getInstance();   
  170.         return df.format(calendar.getTime());   
  171.     }   
  172.   
  173.     /**  
  174.      * 获取日期年份  
  175.      * @param date 日期  
  176.      * @return  
  177.      */  
  178.     public static String getYear(Date date) {   
  179.         return format(date).substring(04);   
  180.     }   
  181.        
  182.     /**  
  183.      * 按默认格式的字符串距离今天的天数  
  184.      * @param date 日期字符串  
  185.      * @return  
  186.      */  
  187.     public static int countDays (String date) {   
  188.         long t = Calendar.getInstance().getTime().getTime();   
  189.         Calendar c = Calendar.getInstance();   
  190.         c.setTime(parse(date));   
  191.         long t1 = c.getTime().getTime();   
  192.         return (int)(t/1000 - t1/1000)/3600/24;   
  193.     }   
  194.        
  195.     /**  
  196.      * 按用户格式字符串距离今天的天数  
  197.      * @param date 日期字符串  
  198.      * @param format 日期格式  
  199.      * @return  
  200.      */  
  201.     public static int countDays (String date, String format) {   
  202.         long t = Calendar.getInstance().getTime().getTime();   
  203.         Calendar c = Calendar.getInstance();   
  204.         c.setTime(parse(date, format));   
  205.         long t1 = c.getTime().getTime();   
  206.         return (int)(t/1000 - t1/1000)/3600/24;   
  207.     }   
  208.        
  209.     /**  
  210.      * 给出一个时间,得到小时  
  211.      */  
  212.     public static int getHourByDate(String currentDate) {   
  213.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  214.         try {   
  215.             Date current = format.parse(currentDate);   
  216.             return current.getHours();   
  217.         } catch (ParseException e) {   
  218.             e.printStackTrace();   
  219.             System.out.println("得到小时数出错!");   
  220.             return 0;   
  221.         }   
  222.     }   
  223.        
  224.     /**  
  225.      * 得到当前月份  
  226.      * @return  
  227.      */  
  228.     public static int getCurMonth(){   
  229.         Calendar ca = Calendar.getInstance();   
  230.         int month = ca.get(Calendar.MONTH);   
  231.         return month+1;   
  232.     }   
  233.        
  234.     /**  
  235.      * 通过毫秒数转换为时间  
  236.      * @return  
  237.      */  
  238.     public static String getDateTileByMillos(long modifiedTime){   
  239.         Date d = new Date(modifiedTime);   
  240.         String strtile = format(d);   
  241.         return strtile;   
  242.     }   
  243.        
  244.     /**  
  245.      * 得到当前年份  
  246.      */  
  247.     public static String getCurrentYear() {   
  248.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy");   
  249.         String currYear = sdf.format(new Date());   
  250.         return currYear;   
  251.     }   
  252.   
  253.     /**  
  254.      * 得到当前月的日列表  
  255.      * @param month  
  256.      * @return  
  257.      */  
  258.     public static List<String> getMonthList(int month) {   
  259.         int year = 0;   
  260.         int day = 0;   
  261.         Calendar c = Calendar.getInstance();// 获得系统当前日期   
  262.         year = c.get(Calendar.YEAR);   
  263.         if (month < 1) {   
  264.             month = c.get(Calendar.MONTH) + 1;// 系统日期从0开始算起   
  265.         }   
  266.         day = c.get(Calendar.DAY_OF_MONTH);   
  267.         c.set(Calendar.YEAR, year);   
  268.         c.set(Calendar.MONTH, day - 1);// Java月份才0开始算   
  269.         int dateOfMonth = c.getActualMaximum(Calendar.DATE);   
  270.         System.out.println("当前年:" + year + " 当前月:" + month + " 当前天" + day);   
  271.         List<String> monthList = new ArrayList<String>();   
  272.         for (int i = 1; i <= dateOfMonth; i++) {   
  273.             System.out.println(year + "年" + month + "月" + i + "日");   
  274.             monthList.add(year + "年" + month + "月" + i + "日");   
  275.         }   
  276.         return monthList;   
  277.     }   
  278.        
  279.     /**  
  280.      * 得到当前月初日期  
  281.      * @param datae  
  282.      * @return  
  283.      */  
  284.     public static String getYueChu() {   
  285.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");   
  286.         String datae = sdf.format(new Date());   
  287.         String[] strs = datae.split("-");   
  288.         String newstr = strs[0] + "-" + (Integer.parseInt(strs[1])) + "-""01";   
  289.         return newstr;   
  290.     }   
  291.        
  292.     /**  
  293.      * 得到当前月月末  
  294.      *   
  295.      * @param datae  
  296.      * @return  
  297.      */  
  298.     public static String getYueMo(String datae) {   
  299.         String[] strs = datae.split("-");   
  300.         String newstr = strs[0] + "-" + strs[1] + "-" + "30";   
  301.         return newstr;   
  302.     }   
  303.        
  304.     /**  
  305.      * 根据生日计算年龄  
  306.      */  
  307.     public static long getAgeByBirthDay(String birthDay) {   
  308.         long year = 0;   
  309.         try {   
  310.             Date date = new Date();   
  311.             Date mydate = parse(birthDay, "yyyy-MM-dd");   
  312.             long day = (date.getTime() - mydate.getTime())/ (24 * 60 * 60 * 1000) + 1;   
  313.             year = day / 365;   
  314.         } catch (Exception e) {   
  315.             year = 0;   
  316.         }   
  317.         return year;   
  318.     }   
  319.        
  320.     /**  
  321.      * 传入一个时间判断是否与当前时间相等  
  322.      */  
  323.     public static boolean JudgeTimeEquals(String time){   
  324.         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");   
  325.         String curTime = sdf.format(new Date());   
  326.         //System.out.println("当前时间是:"+curTime+"  对比时间:"+time);   
  327.         if(curTime.equals(time)){   
  328.             return true;   
  329.         }else{   
  330.             return false;   
  331.         }   
  332.     }   
  333. }  

你可能感兴趣的:(java,的)