1、日期处理工具
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期常用格式转换
*/
public class DateTimeUtil {
static {
ymdhmsFormat = new SimpleDateFormat("yyyyMMddHHmmss");
ymdhmsFormat2 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
ymdFormat = new SimpleDateFormat("yyyyMMdd");
ymdFormat2 = new SimpleDateFormat("yyyy-MM-dd");
hmsFormat = new SimpleDateFormat("HHmmss");
ymFormat = new SimpleDateFormat("yyyyMM");
c = Calendar.getInstance();
}
private static SimpleDateFormat ymdhmsFormat;
private static SimpleDateFormat ymdhmsFormat2;
private static SimpleDateFormat ymdFormat;
private static SimpleDateFormat ymdFormat2;
private static SimpleDateFormat hmsFormat;
private static SimpleDateFormat ymFormat;//年月
private static Calendar c;
public static Date dateOnly(Date date) {
return yyyyMMddToDate(parseToyyyyMMdd(date));
}
/**
* 转换为 yyyyMMddHHmmss格式
*/
public static String parseToyyyyMMddHHmmss(Date date) {
if (date == null) {
return null;
}
return ymdhmsFormat.format(date);
}
/**
* 转换为 yyyyMMdd HH:mm:ss格式
*/
public static String parseToyyyyMMddHHmmss2(Date date) {
if (date == null) {
return null;
}
return ymdhmsFormat2.format(date);
}
/**
* 转换为HHmmss格式
*/
public static String parseToHHmmss(Date date) {
if (date == null) {
return null;
}
return hmsFormat.format(date);
}
/**
* 转换为yyyyMMdd格式
*/
public static String parseToyyyyMMdd(Date date) {
if (date == null) {
return null;
}
return ymdFormat.format(date);
}
/**
* 转换为yyyyMM格式
*/
public static int parseToyyyyMM(Date date) {
if (date == null) {
return 0;
}
return Integer.valueOf(ymFormat.format(date));
}
public static Date yyyyMMddHHmmssToDate(String yyyyMMddHHmmss) {
try {
return ymdhmsFormat.parse(yyyyMMddHHmmss);
}
catch (Exception e) {
return null;
}
}
public static Date yyyyMMddToDate(String yyyyMMdd) {
try {
return ymdFormat.parse(yyyyMMdd);
}
catch (Exception e) {
return null;
}
}
public static Date yyyyMMToDate(String yyyyMM) {
try {
return ymFormat.parse(yyyyMM);
}
catch (Exception e) {
return null;
}
}
/**
* yyyy-MM-dd转换成date
* @author linbingwen
* @since 2016年4月14日
* @param yyyyMMdd2
* @return
*/
public static Date yyyyMMddToDate2(String yyyyMMdd2) {
try {
return ymdFormat2.parse(yyyyMMdd2);
}
catch (Exception e) {
return null;
}
}
public static Date HHmmssToDate(String HHmmss) {
try {
return hmsFormat.parse(HHmmss);
}
catch (Exception e) {
return null;
}
}
public static Date getDate(Date srcDate, Integer daysToAdd) {
c.setTime(srcDate);
c.add(Calendar.DATE, daysToAdd); // number of days to add
return c.getTime();
}
public static Date yyyyMMddHHmmssToDate2(String yyyyMMddHHmmss) {
try {
return ymdhmsFormat2.parse(yyyyMMddHHmmss);
}
catch (Exception e) {
return null;
}
}
public static final int daysBetween(Date early, Date late) {
java.util.Calendar calst = java.util.Calendar.getInstance();
java.util.Calendar caled = java.util.Calendar.getInstance();
calst.setTime(early);
caled.setTime(late);
// 设置时间为0时
calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
calst.set(java.util.Calendar.MINUTE, 0);
calst.set(java.util.Calendar.SECOND, 0);
caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
caled.set(java.util.Calendar.MINUTE, 0);
caled.set(java.util.Calendar.SECOND, 0);
// 得到两个日期相差的天数
int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
return days;
}
public static Date getNextDayOfWeek(Date date, int dayOfWeek) {
if (dayOfWeek == 0) {
dayOfWeek = 7;
}
if (dayOfWeek > 7 || dayOfWeek < 1) {
throw new RuntimeException("星期:" + dayOfWeek + "不存在");
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
while (true) {
int day = cal.get(Calendar.DAY_OF_WEEK);
if (preWeekDay(day) == dayOfWeek) {
return cal.getTime();
}
cal.add(Calendar.DATE, 1);
}
}
public static Date getNextMonthDate(Date date, int nextMonthDate) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DATE);
if (day <= nextMonthDate) {
cal.set(Calendar.DATE, nextMonthDate);
}
else {
cal.set(Calendar.DATE, 1);
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DATE, nextMonthDate);
}
return cal.getTime();
}
public static int nextWeekDay(int day) {
if (day == 7) {
return 1;
}
else {
return day++;
}
}
public static int preWeekDay(int day) {
if (day == 1) {
return 7;
}
else {
return day - 1;
}
}
/**
* 计算两个日期相差的天数
* @param beginDate 【YYYYMMDD】
* @param endDate 【YYYYMMDD】
* @return Integer
* @author linbingwen
* @since 2015年7月21日
*/
public static long diffDate(Date beginDate,Date endDate){
Calendar theCa1= Calendar.getInstance();
Calendar theCa2= Calendar.getInstance();
theCa1.setTime(beginDate);
theCa2.setTime(endDate);
long between_days=(theCa2.getTimeInMillis()-theCa1.getTimeInMillis())/(1000*3600*24);
return between_days;
}
/**
* 分钟差
* @Title: diffMinute
* @Description: TODO
* @author : liuqiuyun
* @param @param beginDate
* @param @param endDate
* @param @return 设定文件
* @return long 返回类型
* @throws
*/
public static long diffMinute(Date beginDate,Date endDate){
Calendar theCa1= Calendar.getInstance();
Calendar theCa2= Calendar.getInstance();
theCa1.setTime(beginDate);
theCa2.setTime(endDate);
long between_minutes=(theCa2.getTimeInMillis()-theCa1.getTimeInMillis())/(1000*60);
return between_minutes;
}
/**
* 获取月份差第一天
* @Title: getMonthFirstDate
* @Description: TODO
* @author : liuqiuyun
* @param @param date
* @param @param monthToAdd
* @param @param minOrMax 月初还是月末
* @param @return 设定文件
* @return Date 返回类型
* @throws
*/
public static Date getMonthFirstDate(Date date,int monthToAdd, String minOrMax) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, monthToAdd);
if(minOrMax.equals("min")){
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
}else if(minOrMax.equals("max")){
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
}
return calendar.getTime();
}
public static long getLastMonth(Date date) {
Date lastDate = getMonthFirstDate(date,-1,"min");
long lastMonth = parseToyyyyMM(lastDate);
return lastMonth;
}
public static void main(String[] args) throws InterruptedException {
// Calendar cal = Calendar.getInstance();
// System.out.println(" cal.get(Calendar.DAY_OF_WEEK);:" + cal.get(Calendar.DAY_OF_WEEK));
// System.out.println(" cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);:" + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
//
// System.out.println(getNextDayOfWeek(cal.getTime(), 0));
// System.out.println(getNextDayOfWeek(cal.getTime(), 7));
// System.out.println(getNextDayOfWeek(cal.getTime(), 1));
// System.out.println(getNextDayOfWeek(cal.getTime(), 2));
//
// System.out.println(getNextMonthDate(cal.getTime(), 0));
// System.out.println(parseToyyyyMMdd(getNextMonthDate(cal.getTime(), 15)));
System.out.println(parseToyyyyMMdd(getMonthFirstDate(yyyyMMddToDate("20160618"),-1,"max")));
// System.out.println(yyyyMMddToDate2("2012-09-01"));
//
// Date start = new Date();
// System.out.println(start);
// Thread.sleep(60*1000*5+1000);
// Date end = new Date();
// System.out.println(end);
// System.out.println(diffMinute(start,end));
}
}
import java.text.SimpleDateFormat