JAVA各种时间类型的取得
程序员文章站
2022-05-18 17:28:09
...
package com.hello.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
public class DateUtil {
private static Logger log = Logger.getLogger(DateUtil.class);
private static String defaultDatePattern = null;
private static String timePattern = "HH:mm";
public static Date getSystemTime() {
Calendar now = Calendar.getInstance();
return now.getTime();
}
public static synchronized String getDatePattern() {
Locale locale = Locale.CHINA; // LocaleContextHolder.getLocale();
try {
defaultDatePattern = ResourceBundle.getBundle("yyyy-MM-dd", locale).getString("date.format");
} catch (MissingResourceException mse) {
defaultDatePattern = "yyyy-MM-dd";
}
return defaultDatePattern;
}
public static final String getDate(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(getDatePattern());
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final Date convertStringToDate(String aMask, String strDate) {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
}
try {
date = df.parse(strDate);
} catch (ParseException pe) {
log.error("ParseException: " + pe);
}
return (date);
}
public static String getTimeNow(Date theTime) {
return getDateTime(timePattern, theTime);
}
public static Calendar getToday() throws ParseException {
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
// This seems like quite a hack (date -> string -> date),
// but it works ;-)
String todayAsString = df.format(today);
Calendar cal = new GregorianCalendar();
cal.setTime(convertStringToDate(todayAsString));
return cal;
}
public static Date getMonday(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
date = DateUtil.addDays(date, -7);
c.setTime(date);
}
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String mondayString = DateUtil.format(c.getTime(), "yyyy-MM-dd");
mondayString = mondayString + " 00:00:00";
return convertStringToDate("yyyy-MM-dd HH:mm:ss", mondayString);
}
public static Date getFriday(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
String fridayString = format(c.getTime(), "yyyy-MM-dd");
fridayString = fridayString + " 23:59:59";
return convertStringToDate("yyyy-MM-dd HH:mm:ss", fridayString);
}
public static final String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
log.error("aDate is null!");
} else {
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final String convertDateToString(Date aDate) {
return getDateTime(getDatePattern(), aDate);
}
public static Date convertStringToDate(String strDate) {
Date aDate = null;
try {
if (log.isDebugEnabled()) {
log.debug("converting date with pattern: " + getDatePattern());
}
aDate = convertStringToDate(getDatePattern(), strDate);
} catch (Exception pe) {
log.error("Could not convert '" + strDate + "' to a date, throwing exception");
}
return aDate;
}
public static int dayDiff(Date first, Date second) {
long msPerDay = 1000 * 60 * 60 * 24;
long diff = (first.getTime() / msPerDay) - (second.getTime() / msPerDay);
Long convertLong = new Long(diff);
return convertLong.intValue();
}
public static long dayDiffMilSecond(Date first, Date second) {
long diff = first.getTime() - second.getTime();
Long convertLong = new Long(diff);
return convertLong;
}
public static String format(Date date, String pattern) {
if (date == null)
return "";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}
static public final Date addDays(Date target, int days) {
long msPerDay = 1000 * 60 * 60 * 24; // 一天的毫秒数
long msTarget = target.getTime(); // 返回从一个特定的日期(1970。。)到现在经过的毫秒数。
long msSum = msTarget + (msPerDay * days);
Date result = new Date();
result.setTime(msSum); // 根据毫秒设置日期
return result;
}
public static Date beforeDays(Date target, int days) {
long msPerDay = 1000 * 60 * 60 * 24; // 一天的毫秒数
long msTarget = target.getTime(); // 返回从一个特定的日期(1970。。)到现在经过的毫秒数。
long msSum = msTarget - (msPerDay * days);
Date result = new Date();
result.setTime(msSum); // 根据毫秒设置日期
return result;
}
public static Date formatString(String time, String format) {
try {
SimpleDateFormat formater = new SimpleDateFormat(format);
return formater.parse(time);
} catch (Exception e) {
return null;
}
}
public static Date getFirstDateOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
public static Date getLastlyDateOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month + 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
public static Date getFirstDateOfNextMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month + 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
public static String getCurrentDayPath() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date now = new Date();
String filePath = sdf.format(now) + "/";
sdf = new SimpleDateFormat("MM");
filePath += sdf.format(now) + "/";
sdf = new SimpleDateFormat("dd");
filePath += sdf.format(now) + "/";
return filePath;
}
}
下一篇: vue-router路由传参的三种方法