java日期转化工具类

时间:2021-08-30 03:53:32
package com.rest.ful.utils;

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

/**
* 日期工具类
* @author zlzhaoe
* @version [版本号, 2017-5-10下午3:02:08 ]
* @see [相关类/方法]
* @since [产品/模块版本]
*/

public class DateUtils
{


private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

private static final String DATETIME_PATTERN_NO_MSE = "yyyy-MM-dd HH:mm";

private static final String DATETIME_PAT = "yyyy-MM-dd";

/**
* 日期转为字符串
*
* @param value
* @return yyyy-MM-dd HH:mm:ss
*/

public static String doConvertToString(Object value)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PATTERN);
String result = null;
if (value instanceof Date)
{
result = simpleDateFormat.format(value);
}
return result;
}
/**
* 日期转为字符串
*
* @param value
* @return yyyy-MM-dd
*/

public static String convertToString(Object value)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PAT);
String result = null;
if (value instanceof Date)
{
result = simpleDateFormat.format(value);
}
return result;
}
/**
* 比较是否滞后于系统时间
*
* @param value
* @return
*/

public static boolean afterToSystemDate(Object value)
{
boolean result = false;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PATTERN_NO_MSE);
Date date = null;
try
{
date = simpleDateFormat.parse((String)value);
}
catch (ParseException e)
{
e.printStackTrace();
}
result = date.after(new Date());
return result;
}

public static boolean afterToSysDateInterval(String value, int minutes)
{
java.util.Date date = null;
date = new Date(Long.valueOf(value));
java.util.Date sysDate = new Date();
long between = (sysDate.getTime() - date.getTime()) / 1000;// 除以1000是为了转换成秒
return between > minutes;
}

/**
* 获取当前时间的毫秒数
*
* @return
*/

public static String getCurrentTimeMillis()
{
return System.currentTimeMillis() + "";
}

/**
* 获得指定日期的前一天
*
* @param specifiedDay
* @return
* @throws Exception
*/

public static Date getSpecifiedDayBefore(Date date)
{// 可以用new Date().toLocalString()传递参数
Calendar c = Calendar.getInstance();
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - 1);
return c.getTime();
}


/**
* 两个时间之间相差距离多少天
*
* @param one 时间参数 1:
* @param two 时间参数 2:
* @return 相差天数
*/

public static long getDistanceDays(String str1, String str2)
throws Exception
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date one;
Date two;
long days = 0;
try
{
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2)
{
diff = time2 - time1;
}
else
{
diff = time1 - time2;
}
days = diff / (1000 * 60 * 60 * 24);
}
catch (ParseException e)
{
e.printStackTrace();
}
return days;
}

/**
* 两个时间相差距离多少天多少小时多少分多少秒
*
* @param str1 时间参数 1 格式:1990-01-01 12:00:00
* @param str2 时间参数 2 格式:2009-01-01 12:00:00
* @return long[] 返回值为:{天, 时, 分, 秒}
*/

public static long[] getDistanceTimes(String str1, String str2)
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date one;
Date two;
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
try
{
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2)
{
diff = time2 - time1;
}
else
{
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
}
catch (ParseException e)
{
e.printStackTrace();
}
long[] times = {day, hour, min, sec};
return times;
}

/**
* 两个时间相差距离多少天多少小时多少分多少秒
*
* @param str1 时间参数 1 格式:1990-01-01 12:00:00
* @param str2 时间参数 2 格式:2009-01-01 12:00:00
* @return String 返回值为:xx天xx小时xx分xx秒
*/

public static String getDistanceTime(Date str1, Date str2)
{
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
long time1 = str1.getTime();
long time2 = str2.getTime();
long diff;
if (time1 < time2)
{
diff = time2 - time1;
}
else
{
diff = time1 - time2;
}
// day = diff / (24 * 60 * 60 * 1000);
// hour = (diff / (60 * 60 * 1000) - day * 24);
// min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
return sec + "";
}

}