1. 程式人生 > >時間工具類方法-Java實現

時間工具類方法-Java實現

實際工作中經常遇到需要處理如下時間相關的場景需要,所以寫個工具類方法,方便使用.

1>獲取指定時間的前一天,後一天

2>獲取指定時間的前一週

3>獲取指定時間的前一月

4>獲取指定時間的一天的起止時間

這個工具類方法可以寫個主方法進行測試,當然也可以進一步完善修改.程式碼如下:

=========================================================================

package com.tgy.util;


import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * DateUtil
 * @author Jerry Tong
 * @date 2017-10-24
 *
 */
public class DateUtil {
private static final String TIME_PATTERN0="yyyy-MM-dd";
private static final String TIME_PATTERN1="yyyy-MM-dd HH:mm:ss";
private static SimpleDateFormat sdf;
/**
* @param cal A specified instance of the Calendar.
* @return Get the A day's startTime adn endTime.<br>
* <ul>
* <li>eg:Current time is 2017-10-24 15:08:00</li>
* <li>return list will be:list.get(0)=2017-10-24 00:00:00,list.get(1)=2017-10-24 23:59:59</li>
* </ul>
* @throws Exception
*/
public static List<Date> getDate(Calendar cal) throws Exception{
if(cal==null)
throw new Exception("Null exception:cal is null!");
Date curDate=cal.getTime();
sdf=new SimpleDateFormat(TIME_PATTERN0);
String strCurDate=sdf.format(curDate);
sdf=new SimpleDateFormat(TIME_PATTERN1);
List<Date>list=new ArrayList<Date>();
list.add(sdf.parse(strCurDate+" 00:00:00"));
list.add(sdf.parse(strCurDate+" 23:59:59"));
return list;
}
/**

* @param cal A specified instance of the Calendar
* @return Get the specified cal's previous Date and next Date.<br>
* <ul>
*  <li>eg:</li>
* <li>Current time is 2017-10-24 15:08:00</li>
* <li>return list will be:list.get(0)="2017-10-23",list.get(1)="2017-10-25"</li>
* </ul>
* @throws Exception
*/
public static List<String> getPreNextDate(Calendar cal) throws Exception{
if(cal==null)
throw new Exception("Null exception:cal is null!");
List<String>list=new ArrayList<String>();
cal.add(Calendar.DATE, -1);
list.add(sdf.format(cal.getTime()));
cal.add(Calendar.DATE, 2);
list.add(sdf.format(cal.getTime()));
return list;
}
/**

* @param cal A specified instance of the Calendar
* @return Get a list of last week(from Monday to Sunday)<br>
* <ul>
*   <li>eg:</li>
*             <li>Current time is 2017-10-24 15:08:00</li>
*             <li>Return list will be:list.get(0)="2017-10-16",list.get(1)="2017-10-22"</li>
* </ul>
* @throws Exception
*/
public static List<String> getPreWeekDate(Calendar cal) throws Exception{
if(cal==null)
throw new Exception("Null exception:cal is null!");
List<String>list=new ArrayList<String>();
sdf=new SimpleDateFormat(TIME_PATTERN0);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
cal.add(Calendar.DATE, -1*7);
list.add(sdf.format(cal.getTime()));
cal.add(Calendar.DATE, 6);
list.add(sdf.format(cal.getTime()));
return list;
}
/**

* @param cal A specified instance of the Calendar
* @return Get a List of last month(from 1st day to the last day)<br>
* eg:Current time is 2017-10-24 15:08:00,call this method<br>
* &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;you will get the list(list.get(0)="2017-09-01",list.get(1)="2017-09-30")
* @throws Exception
*/
public static List<String> getPreMonthDate(Calendar cal) throws Exception{
if(cal==null)
throw new Exception("Null exception:cal is null!");
sdf=new SimpleDateFormat(TIME_PATTERN0);
List<String>list=new ArrayList<String>();
sdf=new SimpleDateFormat(TIME_PATTERN0);
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 1);
list.add(sdf.format(cal.getTime()));
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DATE, -1);
list.add(sdf.format(cal.getTime()));
return list;
}
}