Created
June 10, 2015 06:25
-
-
Save imjoey/285c0c036e18cd64b2b3 to your computer and use it in GitHub Desktop.
Java Date utility functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package com.newcoresys.utils; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
/** | |
* 日期工具类 | |
* | |
* @author joeymac | |
* | |
*/ | |
public class DateEx { | |
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); | |
private static SimpleDateFormat simpltsdf = new SimpleDateFormat("yyyyMMdd"); | |
/** | |
* 获得两个日期相差的天数 | |
* @param date1 | |
* @param date2 | |
* @return | |
*/ | |
public static int getAlterDays(Date date1, Date date2) { | |
int days = (int) ((date1.getTime() - date2.getTime()) / (24*60*60*1000)); | |
return Math.abs(days); | |
} | |
/** | |
* 解析 整数 20150505 为对应的date | |
* @param yyyyMMdd | |
* @return | |
*/ | |
public static Date parseInt(int yyyyMMdd) { | |
try { | |
return simpltsdf.parse(String.valueOf(yyyyMMdd)); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
/** | |
* 获得两个日期相差的交易日天数 | |
* @param date1 | |
* @param date2 | |
* @return | |
*/ | |
public static int getAlterDealDays(Date date1, Date date2) { | |
int days = (int) ((date1.getTime() - date2.getTime()) / (24*60*60*1000)); | |
return Math.abs(days); | |
} | |
/** | |
* 返回对应当前时间的date对象 | |
* @return | |
*/ | |
public static Date now() { | |
return new Date(); | |
} | |
/** | |
* 几秒后的工具函数, | |
* @param count 允许为负值,表示几秒之前 | |
* @return | |
*/ | |
public static Date secondsLater(int count) { | |
return new Date(now().getTime() + count * 1000); | |
} | |
/** | |
* date的几天后 | |
* @param date | |
* @param days 允许为负值,表示几天之前 | |
* @return | |
*/ | |
public static Date daysLater(Date date, int days) { | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
cal.add(Calendar.DAY_OF_MONTH, days); | |
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), | |
cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY), | |
cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)); | |
return cal.getTime(); | |
} | |
/** | |
* 判断source是否在dest之后 | |
* @param source | |
* @param dest | |
* @return 如果相等,返回false | |
*/ | |
public static boolean isAfter(Date source, Date dest) { | |
return source.getTime() > dest.getTime(); | |
} | |
/** | |
* 判断source是否在dest之前 | |
* @param source | |
* @param dest | |
* @return 如果相等,返回false | |
*/ | |
public static boolean isBefore(Date source, Date desc) { | |
return desc.getTime() > source.getTime(); | |
} | |
public static String toChineseFormat(Date date) { | |
return sdf.format(date); | |
} | |
public static String toYYYYMMDDFormat(Date date) { | |
return simpltsdf.format(date); | |
} | |
public static int getDayOfYear(Date date) { | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
return cal.get(Calendar.DAY_OF_YEAR); | |
} | |
/** | |
* 获取某年(自从2015年)的第多少天 | |
* 比如:20150101,(2015-2015)*366【防止出现366天】 | |
* @param date | |
* @return | |
*/ | |
public static int getDayAfter20150101(Date date) { | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
int dayofyear = cal.get(Calendar.DAY_OF_YEAR); | |
return (cal.get(Calendar.YEAR) - 2015) * 366 + dayofyear; | |
} | |
/** | |
* 获取指定date明天00:00:00 | |
* @param date | |
* @return | |
*/ | |
public static Date tomorrowStart(Date date) { | |
return dayStart(daysLater(date, 1)); | |
} | |
/** | |
* 获取指定date的00:00:00 | |
* @param date | |
* @return | |
*/ | |
public static Date dayStart(Date date) { | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), | |
cal.get(Calendar.DATE), 0, 0, 0); | |
return cal.getTime(); | |
} | |
/** | |
* 获取指定date的23:59:59 | |
* @param date | |
* @return | |
*/ | |
public static Date dayEnd(Date date) { | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), | |
cal.get(Calendar.DATE), 23, 59, 59); | |
return cal.getTime(); | |
} | |
public static void main(String[] args) { | |
Calendar cal = Calendar.getInstance(); | |
cal.set(2015, 0, 1); | |
System.out.println(DateEx.getDayAfter20150101(cal.getTime())); | |
Date date = DateEx.parseInt(20150331); | |
System.out.println(DateEx.dayEnd(date)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment