Java自用日期时间工具类

项目中可能用到的一些精华如下:

 

Java代码   收藏代码
  1. package com.neusoft.framework.util;  
  2.   
  3. import java.util.Date;  
  4. import java.text.SimpleDateFormat;  
  5. import java.text.ParseException;  
  6. import java.util.Calendar;  
  7.   
  8. /** 
  9.  * 日期时间工具类<br> 
  10.  * 提供一些常用的日期时间操作方法,所有方法都为静态,不用实例化该类即可使用。<br><br> 
  11.  * 下为日期格式的简单描述详情请参看java API中java.text.SimpleDateFormat<br> 
  12.  * The following pattern letters are defined (all other characters from 
  13.  * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to 
  14.  * <code>'z'</code> are reserved): 
  15.  * <blockquote> 
  16.  * <table border=0 cellspacing=3 cellpadding=0> 
  17.  *     <tr bgcolor="#ccccff"> 
  18.  *         <th align=left>Letter 
  19.  *         <th align=left>Date or Time Component 
  20.  *         <th align=left>Presentation 
  21.  *         <th align=left>Examples 
  22.  *     <tr> 
  23.  *         <td><code>G</code> 
  24.  *         <td>Era designator 
  25.  
  26.  *         <td><a href="#text">Text</a> 
  27.  *         <td><code>AD</code> 
  28.  *     <tr bgcolor="#eeeeff"> 
  29.  *         <td><code>y</code> 
  30.  *         <td>Year 
  31.  *         <td><a href="#year">Year</a> 
  32.  *         <td><code>1996</code>; <code>96</code> 
  33.  *     <tr> 
  34.  *         <td><code>M</code> 
  35.  *         <td>Month in year 
  36.  *         <td><a href="#month">Month</a> 
  37.  *         <td><code>July</code>; <code>Jul</code>; <code>07</code> 
  38.  *     <tr bgcolor="#eeeeff"> 
  39.  *         <td><code>w</code> 
  40.  *         <td>Week in year 
  41.  *         <td><a href="#number">Number</a> 
  42.  *         <td><code>27</code> 
  43.  *     <tr> 
  44.  *         <td><code>W</code> 
  45.  *         <td>Week in month 
  46.  *         <td><a href="#number">Number</a> 
  47.  *         <td><code>2</code> 
  48.  *     <tr bgcolor="#eeeeff"> 
  49.  *         <td><code>D</code> 
  50.  *         <td>Day in year 
  51.  *         <td><a href="#number">Number</a> 
  52.  *         <td><code>189</code> 
  53.  *     <tr> 
  54.  *         <td><code>d</code> 
  55.  *         <td>Day in month 
  56.  *         <td><a href="#number">Number</a> 
  57.  *         <td><code>10</code> 
  58.  *     <tr bgcolor="#eeeeff"> 
  59.  *         <td><code>F</code> 
  60.  *         <td>Day of week in month 
  61.  *         <td><a href="#number">Number</a> 
  62.  *         <td><code>2</code> 
  63.  *     <tr> 
  64.  *         <td><code>E</code> 
  65.  *         <td>Day in week 
  66.  *         <td><a href="#text">Text</a> 
  67.  *         <td><code>Tuesday</code>; <code>Tue</code> 
  68.  *     <tr bgcolor="#eeeeff"> 
  69.  *         <td><code>a</code> 
  70.  *         <td>Am/pm marker 
  71.  *         <td><a href="#text">Text</a> 
  72.  *         <td><code>PM</code> 
  73.  *     <tr> 
  74.  *         <td><code>H</code> 
  75.  *         <td>Hour in day (0-23) 
  76.  *         <td><a href="#number">Number</a> 
  77.  *         <td><code>0</code> 
  78.  *     <tr bgcolor="#eeeeff"> 
  79.  *         <td><code>k</code> 
  80.  *         <td>Hour in day (1-24) 
  81.  *         <td><a href="#number">Number</a> 
  82.  *         <td><code>24</code> 
  83.  *     <tr> 
  84.  *         <td><code>K</code> 
  85.  *         <td>Hour in am/pm (0-11) 
  86.  *         <td><a href="#number">Number</a> 
  87.  *         <td><code>0</code> 
  88.  *     <tr bgcolor="#eeeeff"> 
  89.  *         <td><code>h</code> 
  90.  *         <td>Hour in am/pm (1-12) 
  91.  *         <td><a href="#number">Number</a> 
  92.  *         <td><code>12</code> 
  93.  *     <tr> 
  94.  *         <td><code>m</code> 
  95.  *         <td>Minute in hour 
  96.  *         <td><a href="#number">Number</a> 
  97.  *         <td><code>30</code> 
  98.  *     <tr bgcolor="#eeeeff"> 
  99.  *         <td><code>s</code> 
  100.  *         <td>Second in minute 
  101.  *         <td><a href="#number">Number</a> 
  102.  *         <td><code>55</code> 
  103.  *     <tr> 
  104.  *         <td><code>S</code> 
  105.  *         <td>Millisecond 
  106.  *         <td><a href="#number">Number</a> 
  107.  *         <td><code>978</code> 
  108.  *     <tr bgcolor="#eeeeff"> 
  109.  *         <td><code>z</code> 
  110.  *         <td>Time zone 
  111.  *         <td><a href="#timezone">General time zone</a> 
  112.  *         <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code> 
  113.  *     <tr> 
  114.  *         <td><code>Z</code> 
  115.  *         <td>Time zone 
  116.  *         <td><a href="#rfc822timezone">RFC 822 time zone</a> 
  117.  *         <td><code>-0800</code> 
  118.  * </table> 
  119.  * </blockquote> 
  120.  *<h4>Examples</h4> 
  121.  * 
  122.  * The following examples show how date and time patterns are interpreted in 
  123.  * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time 
  124.  * in the U.S. Pacific Time time zone. 
  125.  * <blockquote> 
  126.  * <table border=0 cellspacing=3 cellpadding=0> 
  127.  *     <tr bgcolor="#ccccff"> 
  128.  *         <th align=left>Date and Time Pattern 
  129.  *         <th align=left>Result 
  130.  *     <tr> 
  131.  *         <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code> 
  132.  *         <td><code>2001.07.04 AD at 12:08:56 PDT</code> 
  133.  *     <tr bgcolor="#eeeeff"> 
  134.  *         <td><code>"EEE, MMM d, ''yy"</code> 
  135.  *         <td><code>Wed, Jul 4, '01</code> 
  136.  *     <tr> 
  137.  *         <td><code>"h:mm a"</code> 
  138.  *         <td><code>12:08 PM</code> 
  139.  *     <tr bgcolor="#eeeeff"> 
  140.  *         <td><code>"hh 'o''clock' a, zzzz"</code> 
  141.  *         <td><code>12 o'clock PM, Pacific Daylight Time</code> 
  142.  *     <tr> 
  143.  *         <td><code>"K:mm a, z"</code> 
  144.  *         <td><code>0:08 PM, PDT</code> 
  145.  *     <tr bgcolor="#eeeeff"> 
  146.  *         <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code> 
  147.  *         <td><code>02001.July.04 AD 12:08 PM</code> 
  148.  *     <tr> 
  149.  *         <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code> 
  150.  *         <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code> 
  151.  *     <tr bgcolor="#eeeeff"> 
  152.  *         <td><code>"yyMMddHHmmssZ"</code> 
  153.  *         <td><code>010704120856-0700</code> 
  154.  * </table> 
  155.  * </blockquote> 
  156.  * 
  157.  */  
  158.   
  159. public class DateTimeUtil {  
  160.   
  161.   /** 
  162.    * 缺省的日期显示格式: yyyy-MM-dd 
  163.    */  
  164.   public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";  
  165.   
  166.   /** 
  167.    * 缺省的日期时间显示格式:yyyy-MM-dd HH:mm:ss 
  168.    */  
  169.   public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";  
  170.   
  171.   /** 
  172.    * 私有构造方法,禁止对该类进行实例化 
  173.    */  
  174.   private DateTimeUtil() {}  
  175.   
  176.   /** 
  177.    * 得到系统当前日期时间 
  178.    * @return 当前日期时间 
  179.    */  
  180.   public static Date getNow() {  
  181.     return Calendar.getInstance().getTime();  
  182.   }  
  183.   
  184.   /** 
  185.    * 得到用缺省方式格式化的当前日期 
  186.    * @return 当前日期 
  187.    */  
  188.   public static String getDate() {  
  189.     return getDateTime(DEFAULT_DATE_FORMAT);  
  190.   }  
  191.   
  192.   /** 
  193.    * 得到用缺省方式格式化的当前日期及时间 
  194.    * @return 当前日期及时间 
  195.    */  
  196.   public static String getDateTime() {  
  197.     return getDateTime(DEFAULT_DATETIME_FORMAT);  
  198.   }  
  199.   
  200.   /** 
  201.    * 得到系统当前日期及时间,并用指定的方式格式化 
  202.    * @param pattern 显示格式 
  203.    * @return 当前日期及时间 
  204.    */  
  205.   public static String getDateTime(String pattern) {  
  206.     Date datetime = Calendar.getInstance().getTime();  
  207.     return getDateTime(datetime, pattern);  
  208.   }  
  209.   
  210.   /** 
  211.    * 得到用指定方式格式化的日期 
  212.    * @param date 需要进行格式化的日期 
  213.    * @param pattern 显示格式 
  214.    * @return 日期时间字符串 
  215.    */  
  216.   public static String getDateTime(Date date, String pattern) {  
  217.     if (null == pattern || "".equals(pattern)) {  
  218.       pattern = DEFAULT_DATETIME_FORMAT;  
  219.     }  
  220.     SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);  
  221.     return dateFormat.format(date);  
  222.   }  
  223.   
  224.   /** 
  225.    * 得到当前年份 
  226.    * @return 当前年份 
  227.    */  
  228.   public static int getCurrentYear() {  
  229.     return Calendar.getInstance().get(Calendar.YEAR);  
  230.   }  
  231.   
  232.   /** 
  233.    * 得到当前月份 
  234.    * @return 当前月份 
  235.    */  
  236.   public static int getCurrentMonth() {  
  237.     //用get得到的月份数比实际的小1,需要加上  
  238.     return Calendar.getInstance().get(Calendar.MONTH) + 1;  
  239.   }  
  240.   
  241.   /** 
  242.    * 得到当前日 
  243.    * @return 当前日 
  244.    */  
  245.   public static int getCurrentDay() {  
  246.     return Calendar.getInstance().get(Calendar.DATE);  
  247.   }  
  248.   
  249.   /** 
  250.    * 取得当前日期以后若干天的日期。如果要得到以前的日期,参数用负数。 
  251.    * 例如要得到上星期同一天的日期,参数则为-7 
  252.    * @param days 增加的日期数 
  253.    * @return 增加以后的日期 
  254.    */  
  255.   public static Date addDays(int days) {  
  256.     return add(getNow(), days, Calendar.DATE);  
  257.   }  
  258.   
  259.   /** 
  260.    * 取得指定日期以后若干天的日期。如果要得到以前的日期,参数用负数。 
  261.    * @param date 基准日期 
  262.    * @param days 增加的日期数 
  263.    * @return 增加以后的日期 
  264.    */  
  265.   public static Date addDays(Date date, int days) {  
  266.     return add(date, days, Calendar.DATE);  
  267.   }  
  268.   
  269.   /** 
  270.    * 取得当前日期以后某月的日期。如果要得到以前月份的日期,参数用负数。 
  271.    * @param months 增加的月份数 
  272.    * @return 增加以后的日期 
  273.    */  
  274.   public static Date addMonths(int months) {  
  275.     return add(getNow(), months, Calendar.MONTH);  
  276.   }  
  277.   
  278.   /** 
  279.    * 取得指定日期以后某月的日期。如果要得到以前月份的日期,参数用负数。 
  280.    * 注意,可能不是同一日子,例如2003-1-31加上一个月是2003-2-28 
  281.    * @param date 基准日期 
  282.    * @param months 增加的月份数 
  283.  
  284.    * @return 增加以后的日期 
  285.    */  
  286.   public static Date addMonths(Date date, int months) {  
  287.     return add(date, months, Calendar.MONTH);  
  288.   }  
  289.   
  290.   /** 
  291.    * 内部方法。为指定日期增加相应的天数或月数 
  292.    * @param date 基准日期 
  293.    * @param amount 增加的数量 
  294.    * @param field 增加的单位,年,月或者日 
  295.    * @return 增加以后的日期 
  296.    */  
  297.   private static Date add(Date date, int amount, int field) {  
  298.     Calendar calendar = Calendar.getInstance();  
  299.     calendar.setTime(date);  
  300.     calendar.add(field, amount);  
  301.     return calendar.getTime();  
  302.   }  
  303.   
  304.   /** 
  305.    * 计算两个日期相差天数。 
  306.    * 用第一个日期减去第二个。如果前一个日期小于后一个日期,则返回负数 
  307.    * @param one 第一个日期数,作为基准 
  308.    * @param two 第二个日期数,作为比较 
  309.    * @return 两个日期相差天数 
  310.    */  
  311.   public static long diffDays(Date one, Date two) {  
  312.     return (one.getTime() - two.getTime()) / (24 * 60 * 60 * 1000);  
  313.   }  
  314.   
  315.   /** 
  316.    * 计算两个日期相差月份数 
  317.    * 如果前一个日期小于后一个日期,则返回负数 
  318.    * @param one 第一个日期数,作为基准 
  319.    * @param two 第二个日期数,作为比较 
  320.    * @return 两个日期相差月份数 
  321.    */  
  322.   public static int diffMonths(Date one, Date two) {  
  323.     Calendar calendar = Calendar.getInstance();  
  324.     //得到第一个日期的年分和月份数  
  325.     calendar.setTime(one);  
  326.     int yearOne = calendar.get(Calendar.YEAR);  
  327.     int monthOne = calendar.get(Calendar.MONDAY);  
  328.     //得到第二个日期的年份和月份  
  329.     calendar.setTime(two);  
  330.     int yearTwo = calendar.get(Calendar.YEAR);  
  331.     int monthTwo = calendar.get(Calendar.MONDAY);  
  332.     return (yearOne - yearTwo) * 12 + (monthOne - monthTwo);  
  333.   }  
  334.   
  335.   /** 
  336.    * 将一个字符串用给定的格式转换为日期类型。<br> 
  337.    * 注意:如果返回null,则表示解析失败 
  338.    * @param datestr 需要解析的日期字符串 
  339.    * @param pattern 日期字符串的格式,默认为“yyyy-MM-dd”的形式 
  340.    * @return 解析后的日期 
  341.    */  
  342.   public static Date parse(String datestr, String pattern) {  
  343.     Date date = null;  
  344.     if (null == pattern || "".equals(pattern)) {  
  345.       pattern = DEFAULT_DATE_FORMAT;  
  346.     }  
  347.     try {  
  348.       SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);  
  349.       date = dateFormat.parse(datestr);  
  350.     }  
  351.     catch (ParseException e) {  
  352.     }  
  353.     return date;  
  354.   }  
  355.   
  356.   /** 
  357.    * 返回本月的最后一天 
  358.    * @return 本月最后一天的日期 
  359.    */  
  360.   public static Date getMonthLastDay() {  
  361.     return getMonthLastDay(getNow());  
  362.   }  
  363.   
  364.   /** 
  365.    * 返回给定日期中的月份中的最后一天 
  366.    * @param date 基准日期 
  367.    * @return 该月最后一天的日期 
  368.    */  
  369.   public static Date getMonthLastDay(Date date) {  
  370.     Calendar calendar = Calendar.getInstance();  
  371.     calendar.setTime(date);  
  372.     //将日期设置为下一月第一天  
  373.     calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 11);  
  374.     //减去1天,得到的即本月的最后一天  
  375.     calendar.add(Calendar.DATE, -1);  
  376.     return calendar.getTime();  
  377.   }  
  378.   
  379.   public static void main(String[] args) {  
  380.   
  381.     String sss =getDateTime("yyyy年MM月dd日") ;  
  382.     String test = "2003-1-31";  
  383.     Date date;  
  384.     try {  
  385.       date = parse(test, "");  
  386.   
  387.       System.out.println("得到当前日期 - getDate():" + DateTimeUtil.getDate());  
  388.       System.out.println("得到当前日期时间 - getDateTime():" + DateTimeUtil.getDateTime());  
  389.   
  390.       System.out.println("得到当前年份 - getCurrentYear():" + DateTimeUtil.getCurrentYear());  
  391.       System.out.println("得到当前月份 - getCurrentMonth():" + DateTimeUtil.getCurrentMonth());  
  392.       System.out.println("得到当前日子 - getCurrentDay():" + DateTimeUtil.getCurrentDay());  
  393.   
  394.       System.out.println("解析 - parse(" + test + "):" + getDateTime(date, "yyyy-MM-dd"));  
  395.   
  396.       System.out.println("自增月份 - addMonths(3):" + getDateTime(addMonths(3), "yyyy-MM-dd"));  
  397.       System.out.println("增加月份 - addMonths(" + test + ",3):" + getDateTime(addMonths(date, 3), "yyyy-MM-dd"));  
  398.       System.out.println("增加日期 - addDays(" + test + ",3):" + getDateTime(addDays(date, 3), "yyyy-MM-dd"));  
  399.       System.out.println("自增日期 - addDays(3):" + getDateTime(addDays(3), "yyyy-MM-dd"));  
  400.   
  401.       System.out.println("比较日期 - diffDays():" + DateTimeUtil.diffDays(DateTimeUtil.getNow(), date));  
  402.       System.out.println("比较月份 - diffMonths():" + DateTimeUtil.diffMonths(DateTimeUtil.getNow(), date));  
  403.   
  404.       System.out.println("得到" + test + "所在月份的最后一天:" + getDateTime(getMonthLastDay(date), "yyyy-MM-dd"));  
  405.   
  406.     }catch (Exception e) {  
  407.       System.out.println(e.getStackTrace());  
  408.     }  
  409.   }  
  410. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值