时间工具类

package com.zq.utils;

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

import org.apache.log4j.Logger;


import com.zq.utils.string.StringUtils;

/**
* 时间工具类
*
* Created by MyEclipse. Author: ChenBin E-mail: chenb@8000056.com Date:
* 2016-2-25 Time: 下午6:14:14 Company: HuNan BwWan information technology co.,LTD
* Web sites: http://www.8000056.com/
*/
public class DateUtils {

private static Logger log = Logger.getLogger(DateUtils.class);

/** 14-12-10 */
public static final String YY_MM_DD = "yy-MM-dd";
/** 2014-12-10 */
public static final String YYYY_MM_DD = "yyyy-MM-dd";
/** 2014-12-11 14:18:28 */
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/** 14-12-11 14:18:28 */
public static final String YY_MM_DD_HH_MM_SS = "yy-MM-dd HH:mm:ss";
/** 14:18:28 */
public static final String HH_MM_SS = "HH:mm:ss";

public static final String HH_MM_SS_STR = " 00:00:00";

/** 获得4位年字符串 */
public static String getFullYear() {
return getFullYear(date());
}

public static String getFullYear(Date date) {
return new SimpleDateFormat("yyyy").format(date);
}

/** 获得2位年字符串 */
public static String getYear() {
return getYear(date());
}

public static String getYear(Date date) {
return new SimpleDateFormat("yy").format(date);
}

/** 获得月字符串 */
public static String getMonth() {
return getMonth(date());
}

public static String getMonth(Date date) {
return new SimpleDateFormat("MM").format(date);
}

/** 获得日字符串 */
public static String getDay() {
return getDay(date());
}

public static String getDay(Date date) {
return new SimpleDateFormat("dd").format(date);
}

/** 获得12小时制格式小时字符串 */
public static String getHour12() {
return getHour12(date());
}

public static String getHour12(Date date) {
return new SimpleDateFormat("hh").format(date);
}

/** 获得24小时制格式小时字符串 */
public static String getHour24() {
return getHour24(date());
}

public static String getHour24(Date date) {
return new SimpleDateFormat("HH").format(date);
}

/**
* Description : 获取当前系统的分钟数
*
* @author : ChenBin
* @date : 2016年12月8日 下午9:15:44
*/
public static String getMinute() {
return getMinute(date());
}

public static String getMinute(Date date) {
return new SimpleDateFormat("mm").format(date);
}

/**
* Description :获取当前系统的秒数
*
* @author : ChenBin(E-Mail:chenbin_2008@126.com)
* @date : 2017年9月9日 下午8:44:31
*/
public static String getSecond() {
return getSecond(date());
}

public static String getSecond(Date date) {
return new SimpleDateFormat("ss").format(date);
}

/**
* Description : 获取默认格式的指定时间字符串(2016-12-08 18:54:06)
*
* @author : ChenBin
* @date : 2016年12月8日 下午6:56:09
*/
public static String getDateStr(Date date) {
return getDateStr(date, YYYY_MM_DD_HH_MM_SS);
}

/**
* Description : 获取默认格式的当前时间字符串(2016-12-08 18:54:06)
*
* @author : ChenBin
* @date : 2016年12月8日 下午6:54:41
*/
public static String getDateStr() {
return getDateStr(YYYY_MM_DD_HH_MM_SS);
}

/** 获得时间字符串 */
public static String getDateStr(String format) {
return getDateStr(date(), format);
}

/** 把Date类型转换为String类型 */
public static String getDateStr(Date date, String format) {
if (StringUtils.compareTrim(format))
if (date != null)
return new SimpleDateFormat(format).format(date);
return null;
}

/**
* 将String类型的日期字符串转换为Date类型
**/
public static Date parse(String format, String dateStr) {
if (dateStr != null) {
try {
return new SimpleDateFormat(format).parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
return null;
}

/**
* @author : ChenBin
* @date : 2015-3-3 下午7:07:54
* @Description :获得由"时间戳"+"_"+"4位随机数"组成的文件名
* @param suffix
* 后缀
* @param extension
* 扩展名
*/
public static String getFileName(String suffix, String extension) {
StringBuffer sb = new StringBuffer();
long timestamp = System.currentTimeMillis();// 时间戳
sb.append(timestamp);
long rmNum = Math.round(Math.random() * 8999 + 1000);// 1000-9999之间的4位随机数
sb.append("_");
sb.append(rmNum);
sb.append(suffix);
sb.append(".");
sb.append(extension);
return sb.toString();
}

/**
*
* Description 获取某种格式下的时间字符串
* @author wangzhiyuan
* @date 2017-11-4 下午2:12:02
* @param format 时间格式
* @return String
*/
public static String getDateStrByFormat(String format){
if(StringUtils.isEmpty(format)){
return null;
}
SimpleDateFormat sf = new SimpleDateFormat(format);
return sf.format(new Date());
}
/**
* @author : ChenBin
* @date : 2015-5-6 下午4:13:48
* @Description :获得字符创格式时间戳
*/
public static String timestamp() {
return System.currentTimeMillis() + "";
}

// public static void main(String[] args) {
// // System.out.println(DateUtils.getDateStr("yyyy-MM-dd_HH:mm:ss"));
// // System.out.println(System.currentTimeMillis());
// // System.out.println(getFileName(null,"jpg"));
//
// // "2016-06-25 12:03:36" yyyy-MM-dd
// // System.out.println(DateUtils.getYear());//14
// // System.out.println(DateUtils.getMonth());//12
// // System.out.println(DateUtils.getDay());//11
// // System.out.println(DateUtils.getHour12());//11
// // System.out.println(DateUtils.getHour24());//11
// // System.out.println(DateUtils.getDateStr(DateUtils.YY_MM_DD));//14-12-11
// // System.out.println(DateUtils.getDateStr(DateUtils.YYYY_MM_DD_HH_MM_SS));//14-12-11
// // 14:18:48
// // long ms = 3001;
// // System.out.println(toSecond(ms));
// }

/**
* Description : 获得一个当前时间的Date类型对象
*
* @author : ChenBin
* @date : 2016-2-25 下午6:16:05
*/
public static Date date() {
return new Date();
}

/**
* Description : 获得当前系统时间Timestamp类型的对象
*
* @author : ChenBin
* @date : 2016-2-25 下午6:05:29
*/
public static synchronized Timestamp currentTimestamp() {
return new Timestamp(System.currentTimeMillis());
}

/**
* Description :获得指定时间Timestamp类型的对象
*
* @author : ChenBin
* @date : 2016-2-25 下午6:08:39
*/
public static Timestamp appointedTimestamp(long milliseconds) {
return new Timestamp(milliseconds);
}

/**
* Description : 获得当前系统的毫秒数
*
* @author : ChenBin
* @date : 2016-3-1 上午9:06:31
*/
public static synchronized long getTimeMillis() {
return System.currentTimeMillis();
}

/**
* Description : 毫秒数转换成Date对象
*
* @author : ChenBin
* @date : 2016-3-1 上午9:48:35
*/
public static Date milliSeconds2Date(long milliseconds) {
return new Date(milliseconds);
}

/**
* Description : 求指定时间戳与当前时间戳的时间差
*
* @author : ChenBin (E-Mail:chenbin_2008@126.com)
* @date : 2017年3月9日 下午5:40:50
*/
public static long timeDifference(long milliseconds) {
return DateUtils.getTimeMillis() - milliseconds;
}

/**
* Description : 将单位毫秒转换成秒
*
* @author : ChenBin (E-Mail:chenbin_2008@126.com)
* @date : 2017年3月9日 下午5:49:59
*/
public static long toSecond(long milliseconds) {
return milliseconds / 1000;
}

/**
* Description :将单位毫秒转换成分
*
* @author : ChenBin (E-Mail:chenbin_2008@126.com)
* @date : 2017年3月9日 下午5:51:12
*/
public static long toMinute(long milliseconds) {
return toSecond(milliseconds) / 60;

}

/**
* Description : 将单位毫秒转换成小时
*
* @author : ChenBin (E-Mail:chenbin_2008@126.com)
* @date : 2017年3月9日 下午5:51:24
*/
public static long toHour(long milliseconds) {
return toMinute(milliseconds) / 60;
}

/**
* 获取当天 11点59分59秒的那个时间点
* @return
* @throws ParseException 时间转换异常
*/
public static Date newDate(){
try{
SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
String result = format.format(new Date());
result+=" 23:59:59";
format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
Date d = format.parse(result);
return d;
}catch (Exception e) {
e.printStackTrace();
log.info("获取当天11点59分59秒这个时间点异常");
return new Date();
}

}



/**
* 在date的基础上往过去反推past天 (00:00:00)
* @param past
* @param date
* @return
* @throws ParseException
*/
public static Date getPastDate(int past) {
try{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
String result = format.format(today)+" 00:00:00";
return format.parse(result);
}catch (Exception e) {
e.printStackTrace();
log.info("时间推算异常");
return new Date();
}
}

public static void main(String[] args) throws ParseException {
// Calendar calendar = Calendar.getInstance();
// calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) -3);
// Date today = calendar.getTime();
// SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
// String result = format.format(today);
// System.out.println(today);
//
// //
// String d1 = "2018-1-13 12:33:59";
// Date date1 = format.parse(d1);
//
// if(date1.after(today)){
// System.out.println("OK");
// }
//


Date temp = getPastDate(3);
SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
String d1 = "2018-1-13 12:33:59";
Date date1 = format.parse(d1);
}



}

原文地址:https://www.cnblogs.com/rey888/p/8315935.html