package com.hx.util;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.Locale;
|
|
public class DateUtil {
|
|
private static SimpleDateFormat Format_1 = new SimpleDateFormat("yyyyMMdd");
|
private static SimpleDateFormat Format_2 = new SimpleDateFormat("yyyyMMddHHmmss");
|
private static SimpleDateFormat Format_3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
private static SimpleDateFormat Format_4 = new SimpleDateFormat("yyyy-MM-dd");
|
private static SimpleDateFormat Format_5 = new SimpleDateFormat("HH:mm:ss");
|
private static SimpleDateFormat Format_6 = new SimpleDateFormat("yyyyMM");
|
private static SimpleDateFormat Format_7 = new SimpleDateFormat("MM月dd日 HH:mm");
|
private static SimpleDateFormat Format_8 = new SimpleDateFormat("yyyy年MM月");
|
private static SimpleDateFormat Format_9 = new SimpleDateFormat("MM-dd");
|
private static SimpleDateFormat Format_10 = new SimpleDateFormat("yyyy年MM月dd日");
|
private static SimpleDateFormat Format_11 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
|
private static SimpleDateFormat Format_12 = new SimpleDateFormat("yyyyMMddHHmm");
|
private static SimpleDateFormat Format_13 = new SimpleDateFormat("yyyy/MM/dd");
|
private static SimpleDateFormat Format_14 = new SimpleDateFormat("yyyy-MM");
|
private static SimpleDateFormat Format_15 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
private static SimpleDateFormat Format_16 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
|
private static SimpleDateFormat Format_17 = new SimpleDateFormat("HH:mm");
|
|
/**时间格式转化
|
* @param date 时间
|
* @param format 时间格式
|
* @return 返回的时间格式字符串
|
*/
|
public static String dateFormat(Date date, String format) {
|
if(!SimpleTool.checkNotNull(date)){
|
return "";
|
}
|
SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
|
return df.format(date);
|
}
|
|
/**
|
* 转换成yyyyMMdd格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate(Date date) {
|
return Format_1.format(date);
|
}
|
|
/**
|
* 转换成yyyyMMddHHmmss格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_1(Date date) {
|
return Format_2.format(date);
|
}
|
|
/**
|
* 转换成yyyy-MM-dd HH:mm:ss格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_2(Date date) {
|
return date == null ? null : Format_3.format(date);
|
}
|
|
/**
|
* 转换成yyyy-MM-dd格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_3(Date date) {
|
return date != null ? Format_4.format(date) : null;
|
}
|
|
/**
|
* 转换成HH:mm:ss格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_4(Date date) {
|
return Format_5.format(date);
|
}
|
|
/**
|
* 转换成yyyyMM格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_5(Date date) {
|
return Format_6.format(date);
|
}
|
|
/**
|
* 转换成mm月dd日 HH:mm格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_6(Date date) {
|
return Format_7.format(date);
|
}
|
|
/**
|
* 转换成yyyy年mm月格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_7(Date date) {
|
return Format_8.format(date);
|
}
|
|
/**
|
* 转换成mm-dd格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_8(Date date) {
|
return Format_9.format(date);
|
}
|
|
/**
|
* 转换成yyyy年MM月dd日格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_9(Date date) {
|
return Format_10.format(date);
|
}
|
|
/**
|
* 转换成yyyyMMddHHmm格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_11(Date date) {
|
return Format_12.format(date);
|
}
|
|
/**
|
* 转换成yyyy年MM月dd日 HH:mm:ss格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_10(Date date) {
|
return Format_11.format(date);
|
}
|
|
/**
|
* 转换成yyyy/MM/dd 格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_13(Date date) {
|
return Format_13.format(date);
|
}
|
|
/**
|
* 转换成yyyy-MM 格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_14(Date date) {
|
return Format_14.format(date);
|
}
|
|
/**
|
* 转换成yyyyMMddHHmmssSSS格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_15(Date date) {
|
return Format_15.format(date);
|
}
|
|
/**
|
* 转换成yyyy/MM/dd HH:mm格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_16(Date date) {
|
return Format_16.format(date);
|
}
|
|
/**
|
* 转换成HH:mm格式的日期字符串
|
*
|
* @param date
|
* @return
|
*/
|
public static String formatDate_17(Date date) {
|
return Format_17.format(date);
|
}
|
|
/**
|
* 转换成字符串yyyyMMddHHmmss成日�?
|
*
|
* @param str
|
* @return
|
* @throws Exception
|
*/
|
public static Date parseString(String str) throws Exception {
|
return Format_2.parse(str);
|
}
|
|
/**
|
* 转换成字符串到指定的日期
|
*
|
* @param str
|
* @param format
|
* @return
|
* @throws Exception
|
*/
|
public static Date parseString(String str, String format) throws Exception {
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
return sdf.parse(str);
|
}
|
|
/**字符串转成时间yyyy-MM-dd HH:mm:ss*/
|
public static Date parseString_1(String str)
|
{
|
try{
|
return Format_3.parse(str);
|
}catch (Exception e)
|
{
|
return null;
|
}
|
}
|
|
/**字符串转成时间yyyy-MM-dd*/
|
public static Date parseString_2(String str)
|
{
|
try{
|
return Format_4.parse(str);
|
}catch (Exception e)
|
{
|
return null;
|
}
|
}
|
|
|
/**时间上分钟叠加
|
* @Author: ChenJiaHe
|
* @param dateTime 时间
|
* @param min 分钟
|
* @return
|
*/
|
public static Date addMin(Date dateTime,int min){
|
Calendar c = Calendar.getInstance();
|
c.setTime(dateTime);
|
c.add(Calendar.MINUTE, min);
|
return c.getTime();
|
}
|
|
/**时间上小时叠加
|
* @Author: ChenJiaHe
|
* @param dateTime 时间
|
* @param hour 小时
|
* @return
|
*/
|
public static Date addhour(Date dateTime,int hour){
|
Calendar c = Calendar.getInstance();
|
c.setTime(dateTime);
|
c.add(Calendar.HOUR, hour);
|
return c.getTime();
|
}
|
|
/**时间上天数叠加
|
* @Author: ChenJiaHe
|
* @param dateTime 时间
|
* @param dayNum 天数
|
* @return
|
*/
|
public static Date addDay(Date dateTime,int dayNum){
|
Calendar c = Calendar.getInstance();
|
c.setTime(dateTime);
|
c.add(Calendar.DATE, dayNum);
|
return c.getTime();
|
}
|
|
/**时间上年数叠加
|
* @Author: ChenJiaHe
|
* @param dateTime 时间
|
* @param dayNum 天数
|
* @return
|
*/
|
public static Date addYear(Date dateTime,int dayNum){
|
Calendar c = Calendar.getInstance();
|
c.setTime(dateTime);
|
c.add(Calendar.YEAR, dayNum);
|
return c.getTime();
|
}
|
|
/**
|
* 转换成字符串到指定的日期
|
*
|
* @param date
|
* @param format
|
* @return
|
* @throws Exception
|
*/
|
public static String formatDate(Date date, String format) {
|
if (date == null) {
|
return null;
|
}
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
|
return sdf.format(date);
|
}
|
|
/**日期转星期
|
* @param dateTime 字符串时间
|
* @param format 字符串时间格式
|
* @param returnType 返回类型 0星期几1周几
|
* @return 数据
|
*/
|
public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException {
|
SimpleDateFormat f = new SimpleDateFormat(format);
|
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
|
if(SimpleTool.checkNotNull(returnType)&&returnType==1){
|
weekDays = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
|
}
|
Calendar cal = Calendar.getInstance(); // 获得一个日历
|
Date datet = null;
|
datet = f.parse(dateTime);
|
cal.setTime(datet);
|
|
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
|
if (w < 0)
|
w = 0;
|
return weekDays[w];
|
}
|
|
/**日期转星期
|
* @param dateTime 时间
|
* @param returnType 返回类型 0星期几1周几
|
* @return 数据
|
*/
|
public static String dateToWeek(Date dateTime,Integer returnType) {
|
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
|
if(SimpleTool.checkNotNull(returnType)&&returnType==1){
|
weekDays = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
|
}
|
Calendar cal = Calendar.getInstance(); // 获得一个日历
|
cal.setTime(dateTime);
|
|
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
|
if (w < 0)
|
w = 0;
|
return weekDays[w];
|
}
|
|
/**
|
* endTime比startTime多的天数
|
* @param startTime 最小时间
|
* @param endTime 最大时间
|
* @return
|
*/
|
public static Integer differDay(Date startTime,Date endTime){
|
Calendar cal1 = Calendar.getInstance();
|
cal1.setTime(startTime);
|
|
Calendar cal2 = Calendar.getInstance();
|
cal2.setTime(endTime);
|
int day1= cal1.get(Calendar.DAY_OF_YEAR);
|
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
|
|
int year1 = cal1.get(Calendar.YEAR);
|
int year2 = cal2.get(Calendar.YEAR);
|
if(year1 != year2) { //同一年
|
int timeDistance = 0 ;
|
for(int i = year1 ; i < year2 ; i ++) {
|
if(i%4==0 && i%100!=0 || i%400==0) {//闰年
|
timeDistance += 366;
|
}
|
else {//不是闰年
|
timeDistance += 365;
|
}
|
}
|
|
return timeDistance + (day2-day1) ;
|
}
|
else { //不同年
|
System.out.println("判断day2 - day1 : " + (day2-day1));
|
return day2-day1;
|
}
|
}
|
|
/**判断两个时间是不是同一天*/
|
public static boolean timeEqual(Date startTime,Date endTime){
|
if(startTime == null || endTime==null){
|
return false;
|
}
|
boolean status = false;
|
if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){
|
status = false;
|
}
|
return status;
|
}
|
|
/**把秒转换成X天X时X分X秒*/
|
public static String getChineseStr(Integer second)
|
{
|
int day = 24 * 60 * 60;
|
int hour = 60 * 60;
|
int min = 60;
|
|
int dayNum = second / day;
|
int hourNum = second % day / hour;
|
int minNum = second % day % hour / min;
|
second = second % day % hour % min;
|
|
String str = dayNum > 0 ? dayNum + "天" : "";
|
str += hourNum > 0 ? hourNum + "时" : "";
|
str += minNum > 0 ? minNum + "分" : "";
|
str += second + "秒";
|
|
return str;
|
}
|
|
}
|