import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
public class DateUtils {
public static final String YEAR = "year";//年份
public static final String WEEKNUM = "weekNum";//周数
public static final String TYPE_DATE_BEFORE = "date_before";
public static final String TYPE_DATE_AFTER = "date_after";
/**
* 获取现在时间
*
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(0);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
/**
* 方法描述:获取自然周数和所在年份
* @date: 日期:2015-11-17 时间:下午07:17:03
* @param date
* @return
*/
public static Map<String, Integer> getWeekNumAndYearNum(Date date) {
Date monday = getMonday(date);
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(7);
cal.setTime(monday);
int week = cal.get(Calendar.WEEK_OF_YEAR);
int year = cal.get(Calendar.YEAR);
Map<String, Integer> map = new HashMap<String, Integer>();
map.put(WEEKNUM, week);
map.put(YEAR, year);
return map;
}
/**
*
*方法描述:返回当前时间
*
*@date: 日期:2015-5-14 时间:下午03:55:20
*@param i
*@return
*/
public static String getCurrTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(new Date());
return date;
}
/**
*
*方法描述:获取当前时间
*
*@return Timestamp
*/
public static Timestamp getNowTime() {
return new Timestamp(new Date().getTime());
}
/**
* 把形如GMT+0.8 GMT+8.0 GMT+8:00 GMT+08:00中国的标准时间改为yyyy-MM-dd HH:mm:ss的格式
*
* @param date
* @return
*/
public static String getDateStr(String date) {
if (!"".equals(date)) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dest = format.format(Date.parse(date));
return dest;
} else {
return "";
}
}
/**
* 获取时间格式: HH:mm
*
* @param date
* @return
*/
public static String getDateStrHM(String date) {
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
String dest = format.format(DateUtils.strToDateLong(date));
return dest;
}
public static String strFormatDateShort(String date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String dest = formatter.format(DateUtils.strToDateLong(date));
return dest;
}
/**
*
*方法描述:获取年月
*
*@param date
*@return
*/
public static String getDateStrYM(Date d) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
String dest = format.format(d);
return dest;
}
/**
* 获取现在时间
*
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
*/
public static Date getDateLong(Date currentTime) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return DateUtils.strToDateLong(dateString);
}
/**
* 获取现在时间
*
* @throws ParseException
*
* @return返回短时间格式 yyyy-MM-dd
*/
public static Date getNowDateShort() {
try {
Date currentTime = new Date();
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
Date currentTime_2 = formatter.parse(dateString);
return currentTime_2;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将长格式字符串改为yyyy-MM-dd格式
*
* @return返回短时间格式 yyyy-MM-dd
*/
public static String getDateShort(Date currentTime) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
if (null != currentTime) {
String dateString = formatter.format(currentTime);
return dateString;
} else {
return "";
}
}
public static String getDateFormatShort(Date currentTime) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
if (null != currentTime) {
String dateString = formatter.format(currentTime);
return dateString;
} else {
return "";
}
}
/**
* 将长格式字符串改为yyyy-MM-dd hh:mm:ss格式
*
* @return返回短时间格式yyyy-MM-dd hh:mm:ss
*/
public static String getDateLongL(Date currentTime) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 获取现在时间
*
* @return返回字符串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 获取现在时间
*
* @return 返回短时间字符串格式yyyy-MM-dd
*/
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 获取时间 小时:分;秒 HH:mm:ss
*
* @return
*/
public static String getTimeShort() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date currentTime = new Date();
String dateString = formatter.format(currentTime);
return dateString;
}
public static String getTimeShort(Date currentTime) {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
public static String getTimeShorts() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
Date currentTime = new Date();
String dateString = formatter.format(currentTime);
return dateString;
}
public static String getTimeShorts(Date str) {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
String dateString = formatter.format(str);
return dateString;
}
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
if (strDate != null) {
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
return null;
}
public static Date strToDateShort(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
if (strDate != null) {
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
return null;
}
/**
* 将时间mm/dd/yyyy格式转换为yyyy-mm-dd
*
* @param date
* 时间字符串
* @return
*/
public static String dateToStr(String date) {
String[] strDate = date.split("/");
String dateString = strDate[2] + "-" + strDate[1] + "-" + strDate[0];
return dateString;
}
/**
* 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
*
* @param dateDate
* @return
*/
public static String dateToStrLong(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (dateDate != null) {
String dateString = formatter.format(dateDate);
return dateString;
} else {
return "";
}
}
/**
* 将短时间格式时间转换为字符串 yyyy-MM-dd
*
* @param dateDate
* @param k
* @return
*/
public static String dateToStr(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
if (dateDate != null) {
String dateString = formatter.format(dateDate);
return dateString;
} else {
return "";
}
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
if (strDate == null) {
return null;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 将短时间格式字符串转换为时间 yyyy
*
* @param strDate
* @return
*/
public static Date strToDateYYYY(String strDate) {
if (strDate == null) {
return null;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 得到现在时间
*
* @return
*/
public static Date getNow() {
Date currentTime = new Date();
return currentTime;
}
/**
* 提取一个月中的最后一天
*
* @param day
* @return
*/
public static Date getLastDate(long day) {
Date date = new Date();
long date_3_hm = date.getTime() - 3600000 * 34 * day;
Date date_3_hm_date = new Date(date_3_hm);
return date_3_hm_date;
}
/**
* 得到现在时间
*
* @return 字符串 yyyyMMdd HHmmss
*/
public static String getStringToday() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 得到现在时间
*
* @return 学符串yyyyMMdd
*/
public static String getStringShortToday() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Str
java 时间工具类 DateUtils.java DateFormat/Calendar/Timestamp
最新推荐文章于 2022-05-23 14:29:32 发布
本文详细介绍了Java中用于处理日期时间的DateUtils工具类,包括获取当前时间、计算周数和年份、格式化日期等方法。通过示例展示了如何使用SimpleDateFormat、Calendar和Timestamp进行日期操作。
摘要由CSDN通过智能技术生成