package main;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils
{
/**
* Number of milliseconds in a standard second.
*/
public static final long MILLIS_PER_SECOND = 1000;// 一秒钟的毫秒数
/**
* Number of milliseconds in a standard minute.
*/
public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;// 一分钟的毫秒数
/**
* Number of milliseconds in a standard hour.
*/
public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;// 一小时的毫秒数
/**
* Number of milliseconds in a standard day.
*/
public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;// 一天的毫秒数
/**
* ISO8601 date format: yyyy-MM-dd
*/
public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
/**
* ISO8601 date-time format: yyyy-MM-dd HH:mm:ss
*/
public static String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
private DateUtils()
{
}
public static String fillZero(String str, int fillSize)
{
if (null != str)
{
int beforeSize = str.length();
if(str.length() < fillSize)
{
int afterSize = fillSize-beforeSize;
for(int i=0; i<afterSize; i++)
{
str = "0"+str;
}
}
}
return str;
}
/**
* 获得系统时间yyyy-MM-dd HH:mm:ss格式
*
* @return
*/
public static String getSystime() {
Calendar todaysDate = new GregorianCalendar();
int year = todaysDate.get(Calendar.YEAR);
int month = todaysDate.get(Calendar.MONTH) + 1;
int day = todaysDate.get(Calendar.DAY_OF_MONTH);
int hourOfDay = todaysDate.get(Calendar.HOUR_OF_DAY);//24小时制
// int hour = todaysDate.get(Calendar.HOUR); //12小时制
int minute = todaysDate.get(Calendar.MINUTE);
int second = todaysDate.get(Calendar.SECOND);
return year + "/" + month + "/" + day + " " + hourOfDay + ":" + fillZero(minute+"", 2)
+ ":" + fillZero(second+"", 2);
}
/**
* 获得当前时间yyyy-MM-dd HH:mm:ss格式
* @return
*/
public static String getCurrentDateAsString()
{
SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT_PATTERN);
return formatter.format(new Date());
}
/**
* 获得当前日期yyyy-MM-dd HH:mm:ss格式
* @return
*/
public static Date getCurrentDate()
{
return parseToDate(getCurrentDateAsString(), DATETIME_FORMAT_PATTERN);
}
/**
* 将日期字符串转换为指定的日期格式
*
* @param str 日期字符串
* @param pattern 日期格式
* @return
*/
public static Date parseToDate(String str, String pattern)
{
DateFormat parser = new SimpleDateFormat(pattern);
try
{
return parser.parse(str);
}
catch (ParseException e)
{
throw new IllegalArgumentException("Can't parse " + str + " using " + pattern);
}
}
/**
* 将yyyy-MM-dd HH:mm:ss格式字符串转成Date
* @param str
* @return
*/
public static Date parseToDate(String str)
{
return parseToDate(str, DATETIME_FORMAT_PATTERN);
}
/**
* 比较两个时间数相差多少分钟
* @param begin
* @param end
* @return
*/
public static long getDiff(Date begin, Date end)
{
long diff = end.getTime() - begin.getTime();
diff = Math.abs(diff);
long day = diff / MILLIS_PER_DAY;// 计算差多少天
long hour = diff % MILLIS_PER_DAY / MILLIS_PER_HOUR;// 计算差多少小时
long min = diff % MILLIS_PER_DAY % MILLIS_PER_HOUR / MILLIS_PER_MINUTE;// 计算差多少分钟
long sec = diff % MILLIS_PER_DAY % MILLIS_PER_HOUR % MILLIS_PER_MINUTE / MILLIS_PER_SECOND;// 计算差多少秒
System.out.println("时间相差:" + day + "天" + hour + "小时" + min + "分钟" + sec + "秒。");
return min;
}
/**
* 获得指定日期的前一天
*
* @param specifiedDay yy-MM-dd
* @return
* @throws Exception
*/
public static String getBeforeDay(String specifiedDay)
{
Calendar c = Calendar.getInstance();
Date date = null;
try
{
date = new SimpleDateFormat(DATE_FORMAT_PATTERN).parse(specifiedDay);
}
catch (ParseException e)
{
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - 1);
String dayBefore = new SimpleDateFormat(DATE_FORMAT_PATTERN).format(c.getTime());
return dayBefore;
}
/**
* 获得指定日期的后一天
*
* @param specifiedDay yy-MM-dd
* @return
*/
public static String getAfterDay(String specifiedDay)
{
Calendar c = Calendar.getInstance();
Date date = null;
try
{
date = new SimpleDateFormat(DATE_FORMAT_PATTERN).parse(specifiedDay);
}
catch (ParseException e)
{
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + 1);
String dayAfter = new SimpleDateFormat(DATE_FORMAT_PATTERN).format(c.getTime());
return dayAfter;
}
/**
* 获得一个月的最后一天
* @param date null|当天
* @return Date
*/
public static Date getLastDayOfMonth(Date date) {
// 获取Calendar
Calendar calendar = Calendar.getInstance();
// 设置时间,当前时间不用设置(NULL当天)
if(date != null)
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(calendar.getTime()));
return calendar.getTime();
}
/**
* 计算两个日期之间相差的天数
* @param d1 较小的时间
* @param d2 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date d1, Date d2) throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
d1 = sdf.parse(sdf.format(d1));
d2 = sdf.parse(sdf.format(d2));
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
long time1 = cal.getTimeInMillis();
cal.setTime(d2);
long time2 = cal.getTimeInMillis();
long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(betweenDays));
}
/**
* 获得两个时间之间隔的天数
*
* @return
*/
public static int daysBetween(String d1, String d2) throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(d1));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(d2));
long time2 = cal.getTimeInMillis();
long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(betweenDays));
}
/**
* 计算两个日期之间相差的月数
*
* @param date1
* @param date2
* @return
*/
public static int getMonth(Date date1, Date date2) {
int iMonth = 0;
int flag = 0;
try {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
if (cal2.equals(cal1)) return 0;
if (cal1.after(cal2)) {
Calendar temp = cal1;
cal1 = cal2;
cal2 = temp;
}
if (cal2.get(Calendar.DAY_OF_MONTH) < cal1.get(Calendar.DAY_OF_MONTH)) flag = 1;
if (cal2.get(Calendar.YEAR) > cal1.get(Calendar.YEAR))
iMonth = ((cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR)) * 12 + cal2.get(Calendar.MONTH) - flag)
- cal1.get(Calendar.MONTH);
else iMonth = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH) - flag;
} catch (Exception e) {
e.printStackTrace();
}
return iMonth;
}
}