日期工具类

时间的工具类方法:



package com.ceair.dc.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.lang.StringUtils;

import cn.sh.cares.core.utils.DateUtils;

/**
* <p>
* Title: FlyDateUtils.java
*</p>
* <p>
* Description:日期工具类
* </p>
*
* @author: 李建波
* @version 1.0
* @Date:2011-1-17,下午05:06:58
*/

public class CommonDateUtils extends DateUtils {

private static Map<String, String> weekDays = new HashMap<String, String>();
static {
weekDays.put("0", "星期日");
weekDays.put("1", "星期一");
weekDays.put("2", "星期二");
weekDays.put("3", "星期三");
weekDays.put("4", "星期四");
weekDays.put("5", "星期五");
weekDays.put("6", "星期六");
}

/**
* 功能:获取日期对应的星期中文名
* @param date
* @return
*/
public static String getWeekDayCnOfDate(Date date) {
return weekDays.get(getWeekDayOfDate(date));
}

/**
* 取得给定时间对应的星期
*
* @return
*/
public static String getWeekDayOfDate(Date date) {
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
return "" + (c.get(Calendar.DAY_OF_WEEK) - 1);
}

/**
* 功能:获取两个日期之间的所有日期
*
* @param startDate
* 开始日期
* @param endDate
* 结束日期
* @return
*/
public static List<Date> getIntervalDates(Date startDate, Date endDate) {
List<Date> result = new ArrayList<Date>();
Long intervalDay = getDateInterval(truncateTimeOfDate(startDate), truncateTimeOfDate(endDate));

if (intervalDay < 0)
return result;

for (Long i = 0L; i <= intervalDay; i++) {
Date currentDate = addDateField(startDate, Calendar.DATE, i
.intValue());
result.add(currentDate);
}
return result;
}

/**
* 功能:获取两个日期之间的所有日期
*
* @param strStartDate
* 开始日期
* @param strEndDate
* 结束日期
* @return
*/
public static List<Date> getIntervalDates(String strStartDate,
String strEndDate) {
Date startDate = parseString(strStartDate);
Date endDate = parseString(strEndDate);
return getIntervalDates(startDate, endDate);
}

/**
*求两个日期差
*
* @param beginDate
* 开始日期
*@param endDate
* 结束日期
*@return 两个日期相差天数
*/
public static Long getDateInterval(Date startDate, Date endDate) {
if (startDate.after(endDate)) {
return -1L;
}
long interval = endDate.getTime() - startDate.getTime();
return interval / (1000 * 60 * 60 * 24);
}
/**
* 功能:求两个日期相差小时数
* @param startDate
* @param endDate
* @return
*/
public static Long getHourInterval(Date startDate, Date endDate){
if(startDate.after(endDate)) {
return -1L;
}
long interval = endDate.getTime() - startDate.getTime();
return interval/(1000*60*60);
}

/**
*求两个日期差(分钟)
*
* @param beginDate
* 开始日期
*@param endDate
* 结束日期
*@return 两个日期相差分钟
*/
public static Long getMinuteInterval(Date startDate, Date endDate) {
long interval = endDate.getTime() - startDate.getTime();
return interval / (1000 * 60);
}

/**
* 功能:获取不带时分秒的时间
* @param sourceDate
* @return
*/
public static Date getShortDate(Date sourceDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = dateFormat.format(sourceDate);
try {
return dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}

/**
* 功能:返回当前月份第一天
* @return
*/
public static Date getCurrentMonthFristDay(){
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH,1 );
return c.getTime();
}

/**
* 功能:返回当前月份最后一天
* @return
*/
public static Date getCurrentMonthLastDay(){
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH,1);
c.add(Calendar.DATE, -1);
return c.getTime();
}

/**
* 功能:返回参数所在星期的7天,
* 如,传入日期'2011-06-02'(星期四),
* 则返回20011-06-02所在星期的周日至周一的日期Map:
* {(SUN:'2011-05-29'),(MON:'2011-05-30'),(TUE:'2011-05-31'),(WED:'2011-06-01'),(THU:'2011-06-02'),(FRI:'2011-06-03',(SAT:'2011-06-04')}
* @param date
* @return
*/
public static HashMap<String,Date> getWeekDates(Date date){
HashMap<String,Date> dates = new HashMap<String,Date>();
Calendar c = Calendar.getInstance();
c.setTime(date);
int index = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DAY_OF_WEEK, (-1)*(index-1));
dates.put("SUN", c.getTime());
c.add(Calendar.DAY_OF_WEEK, 1);
dates.put("MON", c.getTime());
c.add(Calendar.DAY_OF_WEEK, 1);
dates.put("TUE", c.getTime());
c.add(Calendar.DAY_OF_WEEK, 1);
dates.put("WED", c.getTime());
c.add(Calendar.DAY_OF_WEEK, 1);
dates.put("THU",c.getTime());
c.add(Calendar.DAY_OF_WEEK, 1);
dates.put("FRI", c.getTime());
c.add(Calendar.DAY_OF_WEEK, 1);
dates.put("SAT", c.getTime());
return dates;
}

// 计算当月有多少天
public static int days(int year, int month) {
int days = 0;
if (month != 2) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
}
} else {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
days = 29;
else
days = 28;
}
return days;
}

/**
* 功能: 取传入参数日期的当月第一天
* @param date
* @return
*/
public static Date getMonthFirstDay(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}

/**
* 功能: 取传入参数日期的当月最后一天
* @param date
* @return
*/
public static Date getMonthLastDay(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.add(Calendar.DAY_OF_YEAR, -1);
return cal.getTime();
}

/**
* 功能: 获取星期几,如:一、二
* @param year
* @param month
* @param day
* @return
*/
public static String getChinaWeek(int year, int month, int day){
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day);
return formatDate(cal.getTime(), "E");
}

/**
* 功能: 平移月份
* @param date
* @param monthNum
* @return
*/
public static Date moveDate(Date date, int monthNum){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, monthNum);
return cal.getTime();
}

/**
* 获取当前日期,不带时分秒
* @return
*/
public static Date getCurrentDate() {
return parseString(formatDate(new Date()));
}

/**
* 分钟转小时
* @param min
* @return
*/
public static String minToHour(int min){
if(min==0)
return "0";
int hour = min/60;
int leftMin = min%60;
String minStr = leftMin<10?"0"+leftMin:leftMin+"";
return hour+":"+minStr;
}

/**
* 功能: 判断两个日期的间隔天数是否超过参数值
* 超过返回true 未超过返回false
* @param beforeDate 前一日期
* @param lastDate 后一日期
* @param days 间隔天数
* @return
*/
public static Boolean checkDistanceDay(Date beforeDate, Date lastDate, int days) {
long interval = 0l;
if(beforeDate.before(lastDate)){
interval = lastDate.getTime() - beforeDate.getTime();
} else {
interval = beforeDate.getTime() - lastDate.getTime();
}

int betweenDays = (int) (interval / (1000 * 60 * 60 * 24));
if(betweenDays > days){
return true;
}else{
return false;
}
}

/**
* 功能: 根据days参数移动日期,days可为正 可为负
* 日期去除时分秒
* @param date
* @param days
* @return
*/
public static Date moveDay(Date date, int days) {
date = truncateTimeOfDate(date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, days);
return cal.getTime();
}

/**
* 功能:平移小时
* @param date
* @param hour
* @return
*/
public static Date moveHour(Date date,int hour){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, hour);
return cal.getTime();
}

/**
* 功能:获取Date中某个属性的值
* @param date
* @param field java.util.Calendar类中定义
* @return
*/
public static int getDateField(Date date, int field){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(field);
}

/**
* 功能: 根据参数years平移年份
* @param date
* @param years
* @return
*/
public static Date moveYear(Date date, int years) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, years);
return cal.getTime();
}
/**
* 功能:根据年份获得这年的最后一天
* @param year
* @return
*/
public static Date getLastDayOfYear(int year){
Calendar cal = Calendar.getInstance();
cal.set(year, 11, 31);
return cal.getTime();
}
/**
* 功能:根据年份获得这年的第一天
* @param year
* @return
*/
public static Date getFirstDayOfYear(int year){
Calendar cal = Calendar.getInstance();
cal.set(year, 0, 1);
return cal.getTime();
}
/**
* 把分钟转换为小时显示,格式如:2:50表示2小时50分钟
* @param mins
* @return
*/
public static String formatMinutes2Hours(Long mins){
if(mins == null || mins.longValue() == 0)
return "0";
return "" + mins/60 + ":" + StringUtils.leftPad("" + mins%60, 2, "0");
}

/**
* 功能:返回指定年度指定季度的第一天
* @param quarter,{1,2,3,4}
* @param year
* @return
*/
public static Date qetQuarterFirstDay(int quarter,int year){
String firstDateStr = null;
Date firstDate = null;
if(year > 0){
if(quarter>=1&&quarter<=4){
switch(quarter){
case 1:{
firstDateStr = year+"-01-01";
break;
}
case 2:{
firstDateStr = year+"-04-01";
break;
}
case 3:{
firstDateStr = year+"-07-01";
break;
}
case 4:{
firstDateStr = year+"-10-01";
break;
}
default:;
}
}
}
if(firstDateStr != null){
firstDate = parseString(firstDateStr, "yyyy-MM-dd");
}
return firstDate;
}

/**
* 功能:返回指定年度指定季度的最后一天
* @param quarter
* @param year
* @return
*/
public static Date getQuarterLastDay(int quarter,int year){
Date quarterFirstDay = qetQuarterFirstDay(quarter,year);
if(quarterFirstDay != null){
return getQuarterLastDay(quarterFirstDay);
}else
return null;
}

/**
* 功能:返回参数日期所在季度第一天
* @param date
* @return
*/
public static Date getQuarterFirstDay(Date date){
int year = getDateField(date,Calendar.YEAR);
int quarter = getQuarter(date);
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
switch(quarter){
case 1:
{
c.set(Calendar.MONTH, 0);
break;
}
case 2:
{
c.set(Calendar.MONTH, 3);
break;
}
case 3:
{
c.set(Calendar.MONTH, 6);
break;
}
case 4:
{
c.set(Calendar.MONTH, 9);
break;
}
default: ;
}
c.set(Calendar.DAY_OF_MONTH, 1);
return c.getTime();
}

/**
* 功能:返回参数日期所在季度最后一天
* @param date
* @return
*/
public static Date getQuarterLastDay(Date date){
int year = getDateField(date,Calendar.YEAR);
int quarter = getQuarter(date);
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
switch(quarter){
case 1 :
{
c.set(Calendar.MONTH, 3);
break;
}
case 2 :
{
c.set(Calendar.MONTH, 6);
break;
}
case 3 :
{
c.set(Calendar.MONTH, 9);
break;
}
case 4 :
{
c.set(Calendar.YEAR, year+1);
c.set(Calendar.MONTH, 0);
break;
}
default:;
}
c.set(Calendar.DAY_OF_MONTH ,1);
c.add(Calendar.DAY_OF_YEAR, -1);
return c.getTime();
}

/**
* 功能:返回参数日期所在季度,
* 1:第一季度
* 2:第二季度
* 3:第三季度
* 4:第四季度
* @param date
* @return
*/
public static int getQuarter(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
switch(month){
case 0 : return 1;
case 1 : return 1;
case 2 : return 1;
case 3 : return 2;
case 4 : return 2;
case 5 : return 2;
case 6 : return 3;
case 7 : return 3;
case 8 : return 3;
case 9 : return 4;
case 10 : return 4;
case 11 : return 4;
default:;
}
return -1;
}

/**
* @description:String转换为Date yyyy-MM-dd
* @param stringDate
* @return
* @throws ParseException
*/
public static Date stringToDate(String stringDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
* @description:Date转换为String yyyy-MM-dd
* @param stringDate
* @return
* @throws ParseException
*/
public static String dateToString(Date formatDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if(null == formatDate) return null;

String date = sdf.format(formatDate);
return date;
}

/**
* @description:Date转换为String MM月dd日
* @param formatDate
* @return
*/
public static String dateToString2(Date formatDate) {
SimpleDateFormat sdf=new SimpleDateFormat("MM月dd日");
if(null == formatDate) return null;

String date = sdf.format(formatDate);
return date;
}

/**
* @description:Date转换为String HH:mm
* @param formatDate
* @return
*/
public static String dateToString3(Date formatDate) {
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
if(null == formatDate) return null;

String date = sdf.format(formatDate);
return date;
}

/**
* @description:Date转换为String CST时间 ddMMM HH:mm
* @param formatDate
* @return
*/
public static String dateToString4(Date formatDate) {
SimpleDateFormat sdf=new SimpleDateFormat("ddMMM HHmm",Locale.US);
if(null == formatDate) return null;

String date = sdf.format(formatDate);
return date;
}

/**
* @description:Date转换为String CST时间 ddMMM
* @param formatDate
* @return
*/
public static String dateToString5(Date formatDate) {
SimpleDateFormat sdf=new SimpleDateFormat("ddMMM",Locale.US);
if(null == formatDate) return null;

String date = sdf.format(formatDate);
return date;
}

/**
* @description:Date转换为String CST时间 HHmm
* @param formatDate
* @return
*/
public static String dateToString6(Date formatDate) {
SimpleDateFormat sdf=new SimpleDateFormat("HHmm",Locale.US);
if(null == formatDate) return null;

String date = sdf.format(formatDate);
return date;
}

/**
* @description:Date转换为String 如:March 1st
* @param formatDate
* @return
*/
public static String dateToString7(Date formatDate) {
if (null == formatDate)
return null;
SimpleDateFormat sdf = new SimpleDateFormat("MMMM ", Locale.US);
String month = sdf.format(formatDate);
SimpleDateFormat sdf1 = new SimpleDateFormat("d", Locale.US);
String day = changeNumber(Integer.parseInt(sdf1.format(formatDate)));
return month + day;
}

/**
* @description:数字转换,如:1为1st,2为2nd,3为3rd,4为4th
* @param num
* @return
*/
public static String changeNumber(int num) {
switch (num) {
case 1: return "1st";
case 2: return "2nd";
case 3: return "3rd";
case 4: return "4th";
case 5: return "5th";
case 6: return "6th";
case 7: return "7th";
case 8: return "8th";
case 9: return "9th";
case 10: return "10th";
case 11: return "11th";
case 12: return "12th";
case 13: return "13th";
case 14: return "14th";
case 15: return "15th";
case 16: return "16th";
case 17: return "17th";
case 18: return "18th";
case 19: return "19th";
case 20: return "20th";
case 21: return "21st";
case 22: return "22nd";
case 23: return "23rd";
case 24: return "24th";
case 25: return "25th";
case 26: return "26th";
case 27: return "27th";
case 28: return "28th";
case 29: return "29th";
case 30: return "30th";
case 31: return "31st";
default: return "";
}
}

/**
* @description:String转换为Date yyyy-MM-dd HH:mm
* @param stringDate
* @return
* @throws ParseException
*/
public static Date stringToDate2(String stringDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = null;
try {
date = sdf.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
* @description:String转换为Date yyyy-MM-dd HH:mm:ss
* @param stringDate
* @return
* @throws ParseException
*/
public static Date stringToDate3(String stringDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = sdf.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
* 加amount天
* @param now
* @param amount
* @return
*/
public static Date addDays(Date now, int amount) {

return addDateField(now, Calendar.DATE, amount);
}

/**
* 获取一天的起始时间
* @param date
*/
public static Date getDayFirstTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}

/**
* 获取一天的结束时间
* @param date
* @return
*/
public static Date getDayLastTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}

public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) {

GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
XMLGregorianCalendar gc = null;
try {
gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
} catch (Exception e) {

e.printStackTrace();
}
return gc;
}

public static Date convertToDate(XMLGregorianCalendar cal) {
if(cal == null) {
return null;
}
GregorianCalendar ca = cal.toGregorianCalendar();
return ca.getTime();
}

/**
* 判断一个时间是否在两个时间段内,如:0000-0400,0点-4点,精确到分
* @param date
* @param strDateBegin
* @param strDateEnd
* @return
*/
public static boolean isInTime(Date date, String strDateBegin, String strDateEnd) {
int beginRegionTime = 0;
int endRegionTime = 0;
int currentTime = Integer.parseInt(dateToString6(date));
if (StringUtils.isNotBlank(strDateBegin)) {
beginRegionTime = Integer.parseInt(strDateBegin);
}
if (StringUtils.isNotBlank(strDateEnd)) {
endRegionTime = Integer.parseInt(strDateEnd);
}
// 如果当前时间在广告播放时间段里面
if (currentTime >= beginRegionTime && currentTime <= endRegionTime) {
return true;
} else {
return false;
}
}

/**
* GMT时间,时区转换(格林威治平时),北京时间与当地时间转换
* @param sourceDate 传入时间
* @param timeZone 时区
* @return
*/
public static Date getGMTLocalTime(Date sourceDate,String timeZone){
if(timeZone == null || "".equals(timeZone.trim())){
return sourceDate;
}else{
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);
formatter.setTimeZone(destTimeZone);
return stringToDate3(formatter.format(sourceDate));
}
}

/**
*求两个时间差(XX天XX分钟XX秒)
*
* @param beginDate
* 开始日期
*@param endDate
* 结束日期
*@return 两个日期相差XX天XX分钟XX秒
*/
public static String getTimeInterval(Date startDate, Date endDate) {
long interval = endDate.getTime() - startDate.getTime();
long days = interval / (1000 * 60 * 60 * 24);
long hours = (interval - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (interval - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
long seconds = (interval - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
String result = "" + days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
return result;
}


public static void main(String[] args) throws Exception{
// System.out.println(getGMTLocalTime(new Date(), "GMT+10:00"));
// DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date startDate = df.parse("2016-01-05 14:05:30");
// Date endDate = df.parse("2016-01-05 17:07:25");
System.out.println(dateToString5(new Date()));
System.out.println(dateToString7(new Date()));
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值