📄 dateformatutil.java
字号:
/*
* description: 针对日期操作的类
*
* Created on 2005-7-15
* @author WuQiaoYun
*
*/
package com.common.util;
import java.text.DateFormat;
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 com.emk.manage.Constants;
import com.emk.manage.exception.IllegalArgumentOfException;
import com.emk.manage.exception.ParseStringException;
/**
* 日期格式处理、转换工具类
* @author WuQiaoYun
*/
public final class DateFormatUtil {
/**
* 长日期型
*/
public static final int LONG = DateFormat.LONG;
/**
* 中日期型
*/
public static final int MEDIUM = DateFormat.MEDIUM;
/**
* 短日期型
*/
public static final int SHORT = DateFormat.SHORT;
/**
* 按照给定的格式style将指定的日期值转换成字符串。
*
* @param date: 待转换的日期
* @param style: 指定转化类型,style参数取静态常量LONG、MEDIUM和SHORT的值
* @param loc:字符定义对象
* @return 格式化后的日期字符串
* @throws IllegalArgumentException: style模板不符合格式时报异常
*/
public static String formatDate(Date date,int style,Locale loc){
if (style<1 || style>3) {
IllegalArgumentOfException ie = new IllegalArgumentOfException();
ie.setMessageCode("resource_DateFormatUtil_002");
ie.setExtendMessage(String.valueOf(style));
throw ie;
}
String newDate = "";
if (loc==null){
loc = Locale.getDefault();
}
if (date != null) {
DateFormat df = DateFormat.getDateInstance(style,loc);
newDate = df.format(date);
}
return newDate;
}
/**
* 按照给定的格式模板将指定的日期值转换成字符串。
*
* @param date: 待转换的日期
* @param pattern: 指定转化格式字符串,例如:yyyy-MM-dd
* @param loc: 字符定义对象
* @return 格式化后的日期字符串
* @throws IllegalArgumentException: pattern模板不符合格式时报异常
*/
public static String formatDate(Date date,String pattern,Locale loc){
if (pattern==null) {
IllegalArgumentOfException ie = new IllegalArgumentOfException();
ie.setMessageCode("resource_DateFormatUtil_002");
ie.setExtendMessage(pattern);
throw ie;
}
String newDate = "";
if (loc==null){
loc = Locale.getDefault();
}
if (date != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern,loc);
newDate = sdf.format(date);
}
return newDate;
}
/**
* 按照不同的日期格式和时间格式,将指定的日期时间值转换成字符串。
*
* @param date: 待转换的日期
* @param dateStyle: 指定的日期类型,style参数取静态常量LONG、MEDIUM和SHORT的值
* @param timeStyle:指定的时间类型,style参数取静态常量LONG、MEDIUM和SHORT的值
* @param loc:字符定义对象
* @return 格式化后的日期时间字符串
* @throws IllegalArgumentOfException: 日期时间模板违反规定时
*/
public static String formatDateTime(Date date,int dateStyle,int timeStyle,Locale loc){
if (dateStyle<1 || dateStyle>3) {
IllegalArgumentOfException ie = new IllegalArgumentOfException();
ie.setMessageCode("resource_DateFormatUtil_002");
ie.setExtendMessage(String.valueOf(dateStyle));
throw ie;
}
if (timeStyle<1 || timeStyle>3) {
IllegalArgumentOfException ie = new IllegalArgumentOfException();
ie.setMessageCode("resource_DateFormatUtil_003");
ie.setExtendMessage(String.valueOf(timeStyle));
throw ie;
}
String newDate = "";
if (loc==null){
loc = Locale.getDefault();
}
if (date != null) {
DateFormat df = DateFormat.getDateTimeInstance(dateStyle,timeStyle,loc);
newDate = df.format(date);
}
return newDate;
}
/**
* 按照不同的格式模板将指定的日期时间值转换成字符串。
*
* @param date: 待转换的日期
* @param dateStr: 指定日期转化格式字符串模板,例如:yyyy-MM-dd
* @param timeStr: 指定时间转化格式字符串模板,例如:hh:mm:ss
* @param loc:字符定义对象
* @return 格式化后的日期时间字符串
* @throws IllegalArgumentException: pattern模板不符合格式时报异常
*/
public static String formatDateTime(Date date,String dateStr,String timeStr, Locale loc){
if (dateStr==null) {
IllegalArgumentOfException ie = new IllegalArgumentOfException();
ie.setMessageCode("resource_DateFormatUtil_002");
ie.setExtendMessage(dateStr);
throw ie;
}
if (timeStr==null) {
IllegalArgumentOfException ie = new IllegalArgumentOfException();
ie.setMessageCode("resource_DateFormatUtil_003");
ie.setExtendMessage(timeStr);
throw ie;
}
String newDate = "";
if (loc==null) {
loc = Locale.getDefault();
}
if (date != null) {
String pattern = (dateStr==null?"":dateStr) +" "+ (timeStr==null?"":timeStr);
SimpleDateFormat sdf = new SimpleDateFormat(pattern,loc);
newDate = sdf.format(date);
}
return newDate;
}
/**
* 把日期时间格式化为yyyy-MM-dd HH:mm:ss格式
*
* @param dt java.util.Date
* @return 格式化后的日期时间字符串
*/
public static String formatDateTime(Date dt) {
String newDate = "";
if (dt != null) {
Locale locale = Locale.CHINESE;
SimpleDateFormat dateStyle = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);
newDate = dateStyle.format(dt);
}
return newDate;
}
/**
* 将指定的字符串转换成日期
*
* @param dateStr: 待转换的日期符串,以yyyy-MM-dd模板进行转换
* @return 返回标准的日期格式yyyy-MM-dd,与字符串dateStr对应的date对象
* @throws ParseStringException
*/
public static Date parseStrToDate(String dateStr) throws ParseStringException{
try {
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
return sf.parse(dateStr);
}catch(ParseException e){
ParseStringException pse = new ParseStringException(e.getMessage(),e.getErrorOffset());
pse.setMessageCode("resource_DateFormatUtil_001");
pse.setMessageFileName(Constants.MESSAGE_FILENAME);
pse.setExtendMessage(dateStr);
throw pse;
}
}
/**
* 按照不同的格式模板将指定的字符串转换成日期。
*
* @param date: 待转换的日期符串
* @param pattern: 字符串的格式模板,例如:yyyy-MM-dd hh:mm:ss
* @return 与字符串dateStr对应的date对象
* @throws ParseStringException
*/
public static Date parseStrToDate(String date, String pattern) throws ParseStringException{
try {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(date);
}catch(ParseException e){
ParseStringException pse = new ParseStringException(e.getMessage(),e.getErrorOffset());
pse.setMessageCode("resource_DateFormatUtil_001");
pse.setMessageFileName(Constants.MESSAGE_FILENAME);
pse.setExtendMessage(date);
throw pse;
}
}
/**
* 获得当前日期,时间位置为00:00:00
*
* @return Date实例
*/
public static Date getCurrentDate() {
return new Date(System.currentTimeMillis());
}
/**
* 获得当前系统的时间戳
* @return 从1970-1-1到现在的毫秒数
*/
public static long getCurrentTimeMillis() {
return System.currentTimeMillis();
}
/**
* 在给定的日期点上加入指定的天数
*
* @param date 给定的日期点
* @param days 天数,正数为向后;负数为向前
* @return 返回改变后的时间点
*/
public static Date addDate(Date date, int days) {
if (date == null)
return date;
Locale loc = Locale.getDefault();
GregorianCalendar cal = new GregorianCalendar(loc);
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
cal.set(year, month, day + days);
return cal.getTime();
}
/**
* 在当前的日期点上加入指定的天数
*
* @param days 天数,正数为向后;负数为向前
* @return 返回改变后的时间
*/
public static Date addDate(int days) {
Locale loc = Locale.getDefault();
GregorianCalendar cal = new GregorianCalendar(loc);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
cal.set(year, month, day + days);
return cal.getTime();
}
/**
* 获得两个时间点之间相差的天数
*
* @param date1 开始时间点
* @param date2 结束时间点
* @return 具体的天数
*/
public static int getDays(java.util.Date date1, java.util.Date date2) {
if (date1 == null || date2 == null)
return 0;
return (int) ((date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000));
}
/**
* 根据当前的时间分别得到 当天 or 3天内 or 一周内 or 一月内 or 一年内 or 一年以上的时间段
* @param flag 想要得到的时间段标志(0表示当天,1表示3天内,2表示一周内,3表示一月内,4表示一年内,5表示一年以上)
*/
public static Date getTimeRange(String flag){
int flagInt=0;
try{
flagInt=Integer.parseInt(flag);
}
catch(Exception e){
System.out.println(e.getMessage());
return null;
}
Date returnDate=null;
Date nowTime=new Date();
Calendar cal = Calendar.getInstance();
//判断是否为闰年
boolean booleanleapYear = ( (GregorianCalendar)cal ).isLeapYear(((int)cal.getTime().getYear()+1900));
try{
switch(flagInt){
case 0:
returnDate=nowTime; //当天时间
break;
case 1:
nowTime.setDate(nowTime.getDate()-2); //三天内的时间
returnDate=nowTime;
break;
case 2:
nowTime.setDate(nowTime.getDate()-6);//一周内的时间
returnDate=nowTime;
break;
case 3:
if(cal.getActualMaximum(Calendar.DAY_OF_MONTH)==30){
nowTime.setDate(nowTime.getDate()-29);//一个月有30天的时间
}
if(cal.getActualMaximum(Calendar.DAY_OF_MONTH)==31){
nowTime.setDate(nowTime.getDate()-30);//一个月有31的时间
}
if(cal.getActualMaximum(Calendar.DAY_OF_MONTH)==29){
nowTime.setDate(nowTime.getDate()-28);//一个月29的时间
}
if(cal.getActualMaximum(Calendar.DAY_OF_MONTH)==28){
nowTime.setDate(nowTime.getDate()-27);//一个月28的时间
}
returnDate=nowTime;
break;
case 4:
if(booleanleapYear){
//闰年减去365天
nowTime.setDate(nowTime.getDate()-365);
}
else{
//否则减去364天
nowTime.setDate(nowTime.getDate()-364);
}
returnDate=nowTime;
break;
case 5:
if(booleanleapYear){
//闰年减去366天
nowTime.setDate(nowTime.getDate()-366);
}
else{
//否则减去365天
nowTime.setDate(nowTime.getDate()-365);
}
returnDate=nowTime;
break;
case 10:
nowTime.setDate(nowTime.getDate()-9);//10天内的时间
returnDate=nowTime;
break;
case 20:
nowTime.setDate(nowTime.getDate()-19);//20天内的时间
returnDate=nowTime;
break;
default:
break;
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
// System.out.println("========time======"+sf.format(returnDate));
return returnDate;
}
/**
* 获得两个时间点之间相差的天数
*
* @param date1 开始时间点
* @param date2 结束时间点
* @return 具体的天数
*/
public static String getValideTimeDays(java.util.Date date1, java.util.Date date2) {
String gtime="10";
if (date1 == null || date2 == null)
return "";
int gettime = (int) ((date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000));
if(gettime == 10 )
gtime = "10";
if(gettime == 20 )
gtime = "20";
if(gettime>20 && gettime<80)
gtime = "30";
if(gettime>80)
gtime = "90";
return gtime;
}
/**
* 根据传入的时间分别得到 10天后 or 20天后 or 一月后 or 3月后 的时间
* @param flag 想要得到的时间段标志(10表示10天,20表示20天内,30表示一月内,90表示3月内)
*/
public static Date getValideTime(String sourceDay,String flag){
int flagInt=0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try{
flagInt=Integer.parseInt(flag);
}
catch(Exception e){
System.out.println(e.getMessage());
return null;
}
Date returnDate=null;
Date nowTime = null;
try {
nowTime = sdf.parse(sourceDay);
}catch(Exception e){}
try{
switch(flagInt){
case 90:
nowTime.setMonth(nowTime.getMonth()+3);
returnDate=nowTime;
break;
case 30:
nowTime.setMonth(nowTime.getMonth()+1);
returnDate=nowTime;
break;
case 10:
nowTime.setDate(nowTime.getDate()+10);//10天后的时间
returnDate=nowTime;
break;
case 20:
nowTime.setDate(nowTime.getDate()+20);//20天后的时间
returnDate=nowTime;
break;
default:
returnDate=nowTime;
break;
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
return returnDate;
}
/**
* 获得当前时间
* */
public static Date getFormatDate() {
Date now = new Date();
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = bartDateFormat.format(now);
try {
now = bartDateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return now;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -