Java date工具类

package com.zgxcw.operation.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

public static final String DATETIME = "yyyy-MM-dd HH:mm:ss";
public static final String DATE = "yyyy-MM-dd";
public static final String DAY = "yyyyMMdd";
public static final String MONTH = "yyyy-MM";
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";

public static String getCurrentDatetime() {
return new SimpleDateFormat(DATETIME).format(new Date());
}

public static String getCurrentDate() {
return new SimpleDateFormat(DATE).format(new Date());
}

/**
* 将时间字串转化成date
*
* @param dateStr
* @param format
* @return
*/
public static final Date strToDate(String dateStr, String format) {
Date date = null;
try {
date = new SimpleDateFormat(format).parse(dateStr);
} catch (ParseException e) {
}
return date;
}

/**
* 将时间戳转化为字符日期
*
* @param timestamp
* @param patten
* @return
*/
public static String getTime(String timestamp, String patten) {
SimpleDateFormat sdf = new SimpleDateFormat(patten);
String time = null;
try {
Long mydate = new Long(timestamp);
time = sdf.format(mydate);
} catch (Exception e) {
// e.printStackTrace();
}
return time;
}

/**
* 验证时间是否过期
*
* @param timeMillis 待验证时间(毫秒)
* @param expireTimeMillis 过期时间(毫秒)
* @return true 过期, false 没过期
*/
public static boolean validateExpireDate(long timeMillis, long expireTimeMillis) {
return (System.currentTimeMillis() - timeMillis) > expireTimeMillis;
}

/**
* 根据格式由日期型转换成字符串类型
*
* @param date 日期
* @param formatStr 日期格式
* @return 字符串类型日期
*/
public static String formatDateToString(Date date, String formatStr) {
if (date == null)
return "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
return simpleDateFormat.format(date);
}

/**
* 获取指定日期所属天的开始时间
*
* @param date 日期
* @return date 指定日期的开始时间
*/
public static Date getDayBeginTime(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();
}

/**
* 获取指定日期所属天的开始时间
*
* @param date 日期
* @return date 指定日期的结束时间
*/
public static Date getDayEndTime(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();
}

/**
* 获取指定日期所属周第一天
*
* @param date 日期
* @param formatStr 日期格式
* @return 字符串类型日期
*/
public static Date getWeekBeginTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 得到今天是星期几,星期日为1
int datInWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (datInWeek == 1)
datInWeek = 8;

calendar.add(Calendar.DATE, -(datInWeek - 2));

calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DATE), 0, 0, 0);

return calendar.getTime();
}

/**
* 获取指定日期所属周最后一天
*
* @param date 日期
* @param formatStr 日期格式
* @return 字符串类型日期
*/
public static Date getWeekEndTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 得到今天是星期几,星期日为1
int datInWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (datInWeek == 1) {
datInWeek = 8;
}

calendar.add(Calendar.DATE, -(datInWeek - 2));
calendar.add(Calendar.DATE, 6);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DATE), 23, 59, 59);
return calendar.getTime();
}

/**
* 获取指定日期所属月的开始时间
*
* @return date 日期
*/
public static Date getMonthBeginTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTime(date);
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
return cal.getTime();
}

/**
* 获取指定日期所属月的结束时间
*
* @return date 日期
*/
public static Date getMonthEndTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTime(date);
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 23, 59, 59);
cal.roll(Calendar.DATE, -1);
return cal.getTime();
}

/**
* 获取指定日期所属季度起始时间
*
* @param date
* @return 季度起始时间
*/
public static Date getQuarterBeginTime(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 0);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 3);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH, 6);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 9);
}
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0);
return c.getTime();
}

/**
* 获取指定日期所属季度结束时间
*
* @param date
* @return 季度结束时间
*/
public static Date getQuarterEndTime(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 2);
c.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH, 8);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 23, 59, 59);
c.roll(Calendar.DATE, -1);
return c.getTime();
}

/**
* 确定指定日期是不是周六日
*
* @param date
* @return
*/
public static Boolean isWeekend(Date date) {
Boolean isWeekend = false;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
isWeekend = true;
}
return isWeekend;
}

/**
* 获取周末起始时间
*
* @param date
* @return
*/
public static Date getWeekendBeginTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 得到今天是星期几,星期日为1
int datInWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (datInWeek == 1) {
datInWeek = 8;
}

calendar.add(Calendar.DATE, -(datInWeek - 2));
calendar.add(Calendar.DATE, 5);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DATE), 0, 0, 0);
return calendar.getTime();
}

/**
* 获取周末结束时间
*
* @param date
* @return
*/
public static Date getWeekendEndTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 得到今天是星期几,星期日为1
int datInWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (datInWeek == 1) {
datInWeek = 8;
}

calendar.add(Calendar.DATE, -(datInWeek - 2));
calendar.add(Calendar.DATE, 6);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DATE), 23, 59, 59);
return calendar.getTime();
}

/**
* 获得指定日期的后一天
*
* @param specifiedDay
* @return
*/
public static Date getDayAfter(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE, day + 1);
return calendar.getTime();
}

/**
* 获取当年的第一天
*
* @param year
* @return
*/
public static Date getYearStartTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int currentYear = calendar.get(Calendar.YEAR);
return getYearFirst(currentYear);
}

/**
* 获取当年的最后一天
*
* @param year
* @return
*/
public static Date getYearEndTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int currentYear = calendar.get(Calendar.YEAR);
return getYearLast(currentYear);
}

/**
* 获取某年第一天日期
*
* @param year 年份
* @return Date
*/
public static Date getYearFirst(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
Date currYearFirst = calendar.getTime();
return currYearFirst;
}

/**
* 获取某年最后一天日期
*
* @param year 年份
* @return Date
*/
public static Date getYearLast(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
Date currYearLast = calendar.getTime();
return currYearLast;
}

/**
* 获取days天前的日期
* @param days
* @return 日期字符串:格式yyyy-MM-dd
*/
public static String getDateAddDay(int days){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, days);
calendar.getTime();
return new SimpleDateFormat(DATE).format(calendar.getTime());

}


/**
* 将字符串日期转化为Long型
* @param timeStr 时间字符串
* @param sdf 时间字符串格式
* @return
*/
public static Long stringToLong(String timeStr,SimpleDateFormat sdf){
Date date = null;
try {
date = sdf.parse(timeStr);
} catch (ParseException e) {
e.printStackTrace();
}
//继续转换得到秒数的long型
long lTime = date.getTime();
return lTime;
}

public static void main(String[] args) {
DateUtils.stringToLong("2016-02-24 00:00:00",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
DateUtils.stringToLong("2016-3-24 00:00:00",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// System.out.println(System.currentTimeMillis());
// System.out.println(DateUtils.getDateAddDay(-5));
// System.out.println(DateUtils.formatDateToString(new Date(),YYYYMMDDHHMM));
}

}

原文地址:https://www.cnblogs.com/maz9666/p/5378218.html