Chinaunix首页 | 论坛 | 博客
  • 博客访问: 84939
  • 博文数量: 23
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-06 09:13
文章分类
文章存档

2011年(1)

2008年(22)

我的朋友
最近访客

分类: Java

2008-06-06 14:03:17

java 代码
  1. import java.sql.Time;   
  2. import java.sql.Timestamp;   
  3. import java.text.ParseException;   
  4. import java.text.SimpleDateFormat;   
  5. import java.util.Calendar;   
  6. import java.util.Date;   
  7. import java.util.GregorianCalendar;       
  8.   
  9. public class DateUtil {   
  10.     /**  
  11.      * getDateStr get a string with format YYYY-MM-DD from a Date object  
  12.      *   
  13.      * @param date  
  14.      *            date  
  15.      * @return String  
  16.      */  
  17.     static public String getDateStr(Date date) {   
  18.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  19.         return format.format(date);   
  20.     }   
  21.       
  22.     static public String getYear(Date date) {   
  23.         SimpleDateFormat format = new SimpleDateFormat("yyyy");   
  24.         return format.format(date);   
  25.     }   
  26.     static public String getDateStrC(Date date) {   
  27.         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");   
  28.         return format.format(date);   
  29.     }   
  30.   
  31.     static public String getDateStrCompact(Date date) {   
  32.         if (date == null)   
  33.             return "";   
  34.         SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");   
  35.         String str = format.format(date);   
  36.         return str;   
  37.     }   
  38.   
  39.     /**  
  40.      * getDateStr get a string with format YYYY-MM-DD HH:mm:ss from a Date  
  41.      * object  
  42.      *   
  43.      * @param date  
  44.      *            date  
  45.      * @return String  
  46.      */  
  47.     static public String getDateTimeStr(Date date) {   
  48.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  49.         return format.format(date);   
  50.     }   
  51.   
  52.     static public String getDateTimeStrC(Date date) {   
  53.         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");   
  54.         return format.format(date);   
  55.     }   
  56.   
  57.     public static String getCurDateStr(String pattern) {   
  58.         SimpleDateFormat format = new SimpleDateFormat(pattern);   
  59.         return format.format(new Date());   
  60.     }   
  61.   
  62.     /**  
  63.      * Parses text in 'YYYY-MM-DD' format to produce a date.  
  64.      *   
  65.      * @param s  
  66.      *            the text  
  67.      * @return Date  
  68.      * @throws ParseException  
  69.      */  
  70.     static public Date parseDate(String s) throws ParseException {   
  71.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  72.         return format.parse(s);   
  73.     }   
  74.   
  75.     static public Date parseDateC(String s) throws ParseException {   
  76.         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");   
  77.         return format.parse(s);   
  78.     }   
  79.   
  80.     /**  
  81.      * Parses text in 'YYYY-MM-DD' format to produce a date.  
  82.      *   
  83.      * @param s  
  84.      *            the text  
  85.      * @return Date  
  86.      * @throws ParseException  
  87.      */  
  88.     static public Date parseDateTime(String s) throws ParseException {   
  89.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  90.         return format.parse(s);   
  91.     }   
  92.        
  93.     static public Date parseDateTimeC(String s) throws ParseException {   
  94.         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");   
  95.         return format.parse(s);   
  96.     }   
  97.   
  98.     /**  
  99.      * Parses text in 'HH:mm:ss' format to produce a time.  
  100.      *   
  101.      * @param s  
  102.      *            the text  
  103.      * @return Date  
  104.      * @throws ParseException  
  105.      */  
  106.     static public Date parseTime(String s) throws ParseException {   
  107.         SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");   
  108.         return format.parse(s);   
  109.     }   
  110.   
  111.     static public Date parseTimeC(String s) throws ParseException {   
  112.         SimpleDateFormat format = new SimpleDateFormat("HH时mm分ss秒");   
  113.         return format.parse(s);   
  114.     }   
  115.   
  116.     static public int yearOfDate(Date s) throws ParseException {   
  117.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  118.         String d = format.format(s);   
  119.         return Integer.parseInt(d.substring(04));   
  120.     }   
  121.   
  122.     static public int monthOfDate(Date s) throws ParseException {   
  123.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  124.         String d = format.format(s);   
  125.         return Integer.parseInt(d.substring(57));   
  126.     }   
  127.   
  128.     static public int dayOfDate(Date s) throws ParseException {   
  129.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  130.         String d = format.format(s);   
  131.         return Integer.parseInt(d.substring(810));   
  132.     }   
  133.   
  134.     static public String getDateTimeStr(java.sql.Date date, double time) {   
  135.         int year = date.getYear() + 1900;   
  136.         int month = date.getMonth() + 1;   
  137.         int day = date.getDate();   
  138.         String dateStr = year + "-" + month + "-" + day;   
  139.         Double d = new Double(time);   
  140.         String timeStr = String.valueOf(d.intValue()) + ":00:00";   
  141.   
  142.         return dateStr + " " + timeStr;   
  143.     }   
  144.   
  145.     /**  
  146.      * Get the total month from two date.  
  147.      *   
  148.      * @param sd  
  149.      *            the start date  
  150.      * @param ed  
  151.      *            the end date  
  152.      * @return int month form the start to end date  
  153.      * @throws ParseException  
  154.      */  
  155.     static public int diffDateM(Date sd, Date ed) throws ParseException {   
  156.         return (ed.getYear() - sd.getYear()) * 12 + ed.getMonth()   
  157.                 - sd.getMonth() + 1;   
  158.     }   
  159.   
  160.     static public int diffDateD(Date sd, Date ed) throws ParseException {   
  161.         return Math.round((ed.getTime() - sd.getTime()) / 86400000) + 1;   
  162.     }   
  163.   
  164.     static public int diffDateM(int sym, int eym) throws ParseException {   
  165.         return (Math.round(eym / 100) - Math.round(sym / 100)) * 12  
  166.                 + (eym % 100 - sym % 100) + 1;   
  167.     }   
  168.   
  169.     static public java.sql.Date getNextMonthFirstDate(java.sql.Date date)   
  170.             throws ParseException {   
  171.         Calendar scalendar = new GregorianCalendar();   
  172.         scalendar.setTime(date);   
  173.         scalendar.add(Calendar.MONTH, 1);   
  174.         scalendar.set(Calendar.DATE, 1);   
  175.         return new java.sql.Date(scalendar.getTime().getTime());   
  176.     }   
  177.   
  178.     static public java.sql.Date getFrontDateByDayCount(java.sql.Date date,   
  179.             int dayCount) throws ParseException {   
  180.         Calendar scalendar = new GregorianCalendar();   
  181.         scalendar.setTime(date);   
  182.         scalendar.add(Calendar.DATE, -dayCount);   
  183.         return new java.sql.Date(scalendar.getTime().getTime());   
  184.     }   
  185.   
  186.     /**  
  187.      * Get first day of the month.  
  188.      *   
  189.      * @param year  
  190.      *            the year  
  191.      * @param month  
  192.      *            the month  
  193.      * @return Date first day of the month.  
  194.      * @throws ParseException  
  195.      */  
  196.     static public Date getFirstDay(String year, String month)   
  197.             throws ParseException {   
  198.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  199.         return format.parse(year + "-" + month + "-1");   
  200.     }   
  201.   
  202.     static public Date getFirstDay(int year, int month) throws ParseException {   
  203.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  204.         return format.parse(year + "-" + month + "-1");   
  205.     }   
  206.   
  207.     static public Date getLastDay(String year, String month)   
  208.             throws ParseException {   
  209.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  210.         Date date = format.parse(year + "-" + month + "-1");   
  211.   
  212.         Calendar scalendar = new GregorianCalendar();   
  213.         scalendar.setTime(date);   
  214.         scalendar.add(Calendar.MONTH, 1);   
  215.         scalendar.add(Calendar.DATE, -1);   
  216.         date = scalendar.getTime();   
  217.         return date;   
  218.     }   
  219.   
  220.     static public Date getLastDay(int year, int month) throws ParseException {   
  221.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  222.         Date date = format.parse(year + "-" + month + "-1");   
  223.   
  224.         Calendar scalendar = new GregorianCalendar();   
  225.         scalendar.setTime(date);   
  226.         scalendar.add(Calendar.MONTH, 1);   
  227.         scalendar.add(Calendar.DATE, -1);   
  228.         date = scalendar.getTime();   
  229.         return date;   
  230.     }   
  231.   
  232.     /**  
  233.      * getToday get todat string with format YYYY-MM-DD from a Date object  
  234.      *   
  235.      * @param date  
  236.      *            date  
  237.      * @return String  
  238.      */  
  239.   
  240.     static public String getTodayStr() throws ParseException {   
  241.         Calendar calendar = Calendar.getInstance();   
  242.         return getDateStr(calendar.getTime());   
  243.     }   
  244.   
  245.     static public Date getToday() throws ParseException {   
  246.         return new Date(System.currentTimeMillis());   
  247.     }   
  248.   
  249.     static public String getTodayAndTime() {   
  250.         return new Timestamp(System.currentTimeMillis()).toString();   
  251.     }   
  252.   
  253.     static public String getTodayC() throws ParseException {   
  254.         Calendar calendar = Calendar.getInstance();   
  255.         return getDateStrC(calendar.getTime());   
  256.     }   
  257.   
  258.     static public int getThisYearMonth() throws ParseException {   
  259.         Date today = Calendar.getInstance().getTime();   
  260.         return (today.getYear() + 1900) * 100 + today.getMonth() + 1;   
  261.     }   
  262.   
  263.     static public int getYearMonth(Date date) throws ParseException {   
  264.         return (date.getYear() + 1900) * 100 + date.getMonth() + 1;   
  265.     }   
  266.   
  267.     // 获取相隔月数   
  268.     static public long getDistinceMonth(String beforedate, String afterdate)   
  269.             throws ParseException {   
  270.         SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");   
  271.         long monthCount = 0;   
  272.         try {   
  273.             java.util.Date d1 = d.parse(beforedate);   
  274.             java.util.Date d2 = d.parse(afterdate);   
  275.   
  276.             monthCount = (d2.getYear() - d1.getYear()) * 12 + d2.getMonth()   
  277.                     - d1.getMonth();   
  278.             // dayCount = (d2.getTime()-d1.getTime())/(30*24*60*60*1000);   
  279.   
  280.         } catch (ParseException e) {   
  281.             System.out.println("Date parse error!");   
  282.             // throw e;   
  283.         }   
  284.         return monthCount;   
  285.     }   
  286.   
  287.     // 获取相隔天数   
  288.     static public long getDistinceDay(String beforedate, String afterdate)   
  289.             throws ParseException {   
  290.         SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");   
  291.         long dayCount = 0;   
  292.         try {   
  293.             java.util.Date d1 = d.parse(beforedate);   
  294.             java.util.Date d2 = d.parse(afterdate);   
  295.   
  296.             dayCount = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);   
  297.   
  298.         } catch (ParseException e) {   
  299.             System.out.println("Date parse error!");   
  300.             // throw e;   
  301.         }   
  302.         return dayCount;   
  303.     }   
  304.   
  305.     // 获取相隔天数   
  306.     static public long getDistinceDay(Date beforedate, Date afterdate)   
  307.             throws ParseException {   
  308.         long dayCount = 0;   
  309.   
  310.         try {   
  311.             dayCount = (afterdate.getTime() - beforedate.getTime())   
  312.                     / (24 * 60 * 60 * 1000);   
  313.   
  314.         } catch (Exception e) {   
  315.             // System.out.println("Date parse error!");   
  316.             // // throw e;   
  317.         }   
  318.         return dayCount;   
  319.     }   
  320.   
  321.     static public long getDistinceDay(java.sql.Date beforedate,   
  322.             java.sql.Date afterdate) throws ParseException {   
  323.         long dayCount = 0;   
  324.   
  325.         try {   
  326.             dayCount = (afterdate.getTime() - beforedate.getTime())   
  327.                     / (24 * 60 * 60 * 1000);   
  328.   
  329.         } catch (Exception e) {   
  330.             // System.out.println("Date parse error!");   
  331.             // // throw e;   
  332.         }   
  333.         return dayCount;   
  334.     }   
  335.   
  336.     // 获取相隔天数   
  337.     static public long getDistinceDay(String beforedate) throws ParseException {   
  338.         return getDistinceDay(beforedate, getTodayStr());   
  339.     }   
  340.   
  341.     // 获取相隔时间数   
  342.     static public long getDistinceTime(String beforeDateTime,   
  343.             String afterDateTime) throws ParseException {   
  344.         SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
  345.         long timeCount = 0;   
  346.         try {   
  347.             java.util.Date d1 = d.parse(beforeDateTime);   
  348.             java.util.Date d2 = d.parse(afterDateTime);   
  349.   
  350.             timeCount = (d2.getTime() - d1.getTime()) / (60 * 60 * 1000);   
  351.   
  352.         } catch (ParseException e) {   
  353.             System.out.println("Date parse error!");   
  354.             throw e;   
  355.         }   
  356.         return timeCount;   
  357.     }   
  358.   
  359.     // 获取相隔时间数   
  360.     static public long getDistinceTime(String beforeDateTime)   
  361.             throws ParseException {   
  362.         return getDistinceTime(beforeDateTime, new Timestamp(System   
  363.                 .currentTimeMillis()).toLocaleString());   
  364.     }   
  365.   
  366.     // 获取相隔分钟数   
  367.     static public long getDistinceMinute(String beforeDateTime,   
  368.             String afterDateTime) throws ParseException {   
  369.         SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  370.         long timeCount = 0;   
  371.         try {   
  372.             java.util.Date d1 = d.parse(beforeDateTime);   
  373.             java.util.Date d2 = d.parse(afterDateTime);  
阅读(1002) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~