java 日期工具_java日期工具类

packagecom.djzh.common.utils;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.Locale;importorg.apache.commons.lang.StringUtils;importorg.apache.commons.lang.time.DateFormatUtils;importorg.apache.log4j.Logger;/*** 日期实用工具类

**/

public classDateUtil {public static final Logger log = Logger.getLogger(DateUtil.class);/*** 返回当前日期

*

*@returnString*/

public staticString getDate() {return DateFormatUtils.format(new Date(), "yyyy-MM-dd");

}/*** 返回日期yyyy-MM-dd

*

*@returnString*/

public staticString getDate(Date date) {return DateFormatUtils.format(date, "yyyy-MM-dd");

}/*** 返回当前日期时间

*

*@returnString*/

public staticString getDateTime() {return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");

}/*** 返回当前日期时间

*

*@returnString*/

public staticString getDateRoundMinute() {return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm");

}/*** 获取日期200-10-5

*

*@paramstr

* 原日期类型yyyy-MM-dd HH:mm:ss.S

*@return

*/

public staticString getDateTime(String str) {if (str == null || str.equals(""))return "";

DateFormat df=DateFormat.getDateInstance();

Date d= null;try{

d=df.parse(str);

}catch(Exception e) {return null;

}return DateFormatUtils.format(d, "yyyy-MM-dd");

}/*** 返回当前时间

*

*@returnString*/

public staticString getTime() {return DateFormatUtils.format(new Date(), "HHmmssSSS");

}/*** 返回当前时间

*

*@returnString*/

public staticString getCurrentTime() {return DateFormatUtils.format(new Date(), "HH:mm:ss");

}/*** 获取月的实现

*

*@paramdate

*@returnString*/

public static String getEndTime(Date date, int month, booleanendmonth) {

String ds= null;

Calendar c=Calendar.getInstance();

c.setTime(date);

c.add(Calendar.MONTH, month);if(endmonth) {int i =c.getActualMaximum(Calendar.DAY_OF_MONTH);

c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), i,23, 59, 59);

}

ds= DateFormatUtils.format(c.getTime(), "yyyy-MM-dd HH:mm:ss");returnds;

}/*** 获取天的时限

*

*@paramdate

*@returnString*/

public static String getEndTime(Date date, intdays) {

String ds= null;

Calendar c=Calendar.getInstance();

c.setTime(date);

c.add(Calendar.DATE, days);

ds= DateFormatUtils.format(c.getTime(), "yyyy-MM-dd HH:mm:ss");returnds;

}/*** 返回中文日期

*

*@returnString*/

public staticString getGBDate(String dateStr) {

DateFormat df=DateFormat.getDateInstance();

String gbdate= null;try{

Date date=df.parse(dateStr);

gbdate= DateFormatUtils.format(date, "yyyy年MM月dd日");

}catch(ParseException pe) {

log.info(pe.getMessage());

}returngbdate;

}/*** 返回日期

*

*@returnString*/

public staticString getStringDate(String dateStr, String pattern) {

DateFormat df=DateFormat.getDateInstance();

String gbdate= null;try{

Date date=df.parse(dateStr);

gbdate=DateFormatUtils.format(date, pattern);

}catch(ParseException pe) {

log.info(pe.getMessage());

}returngbdate;

}/*** 返回中文日期时间

*

*@returnString*/

public staticString getGBDateTime(String dateStr) {

DateFormat df=DateFormat.getDateInstance();

String gbdate= null;try{

Date date=df.parse(dateStr);

gbdate= DateFormatUtils.format(date, "yyyy年MM月dd日 HH时mm分ss秒");

}catch(ParseException pe) {

log.info(pe.getMessage());

}returngbdate;

}/*** 返回日期格式

*

*@returnDate*/

public staticDate getDate(String dateStr) {

DateFormat df=DateFormat.getDateInstance();try{

Date date=df.parse(dateStr);returndate;

}catch(Exception e) {

}return null;

}public staticString getShortDate(String dateStr) {if (dateStr == null || "".equals(dateStr))return null;

DateFormat df=DateFormat.getDateInstance();try{

Date date=df.parse(dateStr);return DateFormatUtils.format(date, "yyyy-MM-dd");

}catch(Exception e) {

log.error("日期格式有误" +e.getMessage());

}return null;

}public staticDate getDate(String dateStr, String pattern) {

SimpleDateFormat sdf= newSimpleDateFormat(pattern);try{

Date date=sdf.parse(dateStr);returndate;

}catch(Exception e) {

}return null;

}/*** 返回日期格式

*

*@returnString*/

public staticString getDateStr(Date date) {

String dateStr= null;try{

dateStr= DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss.S");returndateStr;

}catch(Exception e) {

}return null;

}/*** 返回日期格式

*

*@returnString*/

public staticString getDateStr(Date date, String pattern) {

String dateStr= null;try{

dateStr=DateFormatUtils.format(date, pattern);returndateStr;

}catch(Exception e) {

}return "";

}/*** 返回时间类型

*

*@paramaValue

*@return

*/

public staticjava.sql.Timestamp parseTimestampFromFormats(String aValue) {if(StringUtils.isEmpty(aValue))return null;

DateFormat df=DateFormat.getDateTimeInstance();if (aValue.indexOf(":") == -1) {

aValue= " 00:00:00";

}else if (aValue.indexOf(":") != -1

&& aValue.indexOf(":") == aValue.lastIndexOf(":")) {

aValue= aValue + ":00";

}try{

Date date=df.parse(aValue);return newjava.sql.Timestamp(date.getTime());

}catch(Exception e) {

}return null;

}public staticjava.sql.Timestamp now() {return new java.sql.Timestamp(newjava.util.Date().getTime());

}/*** 获取服务器的系统时间

*

*@returnDate 日期 格式:yyyy-MM-dd HH:mm:ss*/

public staticString getCurrentServerTime() {returngetDateTime();

}/*** 把时间格式转换成到分钟

*

*@paramString

* 日期 格式:yyyy-MM-dd HH:mm:ss

*@returnString 日期 格式:yyyy-MM-dd HH:mm*/

public staticString getDateTimeStringToMinute(String date) {if (date == null || date.equals(""))return "";try{

String dateStr=DateFormatUtils.format(DateFormat

.getDateTimeInstance().parse(date),"yyyy-MM-dd HH:mm");returndateStr;

}catch(Exception e) {return "";

}

}/*** 获取时间到秒

*

*@paramdate

* 日期

*@return

*/

public staticString getDateStrToSecond(Date date) {if (date == null)return "";

String dateStr= null;try{

dateStr= DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");returndateStr;

}catch(Exception e) {

log.info(e.getMessage());return null;

}

}/*** 获取时间到分

*

*@paramdate

* 日期

*@return

*/

public staticString getDateStrToMinute(Date date) {if (date == null)return "";

String dateStr= null;try{

dateStr= DateFormatUtils.format(date, "yyyy-MM-dd HH:mm");returndateStr;

}catch(Exception e) {return "";

}

}/*** 获取时间到日

*

*@paramdate

* 日期

*@return

*/

public staticString getDateStrToDay(Date date) {if (date == null)return "";

String dateStr= null;try{

dateStr= DateFormatUtils.format(date, "yyyy-MM-dd");returndateStr;

}catch(Exception e) {

log.info(e.getMessage());return null;

}

}public static Date getDateFromString(String dateStr) throwsException {

DateFormat df=DateFormat.getDateInstance();

Date date=df.parse(dateStr);returndate;

}public static Date getDateTimeFromString(String dateStr) throwsException {

DateFormat df=DateFormat.getDateTimeInstance();

Date date=df.parse(dateStr);returndate;

}///**//* 检查文本框中的日期时间格式//*//* @param source//* Text//*//* @return//*///public static boolean checkDateTime(Text text) {//

//if (text.getText() != null && text.getText().trim().length() > 0) {//String str = text.getText().trim();//Date date = null;//try {//date = changeDate(str);//String sdate = DateUtil.getDateStrToMinute(date);//text.setText(sdate);//} catch (Exception e1) {//MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06 6:6");//

//text.setText("");//

//return false;//}//}//return true;//}///**//* 检查文本框中的日期时间格式//*//* @param source//* Text//*//* @return//*///public static boolean checkDateTime(WText text) {//

//if (text.getText() != null && text.getText().trim().length() > 0) {//String str = text.getText().trim();//Date date = null;//try {//date = changeDate(str);//String sdate = DateUtil.getDateStrToMinute(date);//text.setText(sdate);//} catch (Exception e1) {//MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06 6:6");//

//text.setText("");//

//return false;//}//}//return true;//}///**//* 检查文本框中的日期时间格式//*//* @param source//* StyledText//*//* @return//*///public static boolean checkDateTime(StyledText text) {//

//if (text.getText() != null && text.getText().trim().length() > 0) {//String str = text.getText().trim();//Date date = null;//try {//date = changeDate(str);//String sdate = DateUtil.getDateStrToMinute(date);//text.setText(sdate);//} catch (Exception e1) {//MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06 6:6");//

//text.setText("");//return false;//}//}//return true;//

//}//

///**//* 检查文本框中的日期格式//*//* @param source//* Text//*//* @return//*///public static boolean checkDate(Text text) {//

//if (text.getText() != null && text.getText().trim().length() > 0) {//String str = text.getText().trim();//Date date = null;//try {//date = changeDate(str);//String sdate = DateUtil.getDateStrToDay(date);//text.setText(sdate);//} catch (Exception e1) {//MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06");//

//text.setText("");//return false;//}//}//return true;//}///**//* 检查文本框中的日期格式//*//* @param source//* Text//*//* @return//*///public static boolean checkDate(WText text) {//

//if (text.getText() != null && text.getText().trim().length() > 0) {//String str = text.getText().trim();//Date date = null;//try {//date = changeDate(str);//String sdate = DateUtil.getDateStrToDay(date);//text.setText(sdate);//} catch (Exception e1) {//MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06");//

//text.setText("");//return false;//}//}//return true;//}///**//* 检查日期输入//* @param text//* @return//*///public static boolean checkDateInput(Text text) {//

//if (text.getText() != null && text.getText().trim().length() > 0) {//String str = text.getText().trim();//Date date = null;//try {//date = changeDate(str);//String sdate = DateUtil.getDateStrToDay(date);//text.setText(sdate);//} catch (Exception e1) {//text.setText("");//text.setFocus();//MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06");//return false;//}//}//return true;//}//

///**//* 检查文本框中的日期格式//*//* @param source//* StyledText//*//* @return//*///public static void checkDate(StyledText source) {//

//source.addFocusListener(new FocusAdapter() {//

//public void focusLost(FocusEvent e) {//StyledText text = (StyledText) e.getSource();//if (text.getText() != null//&& text.getText().trim().length() > 0) {//String str = text.getText().trim();//Date date = null;//try {//date = changeDate(str);//String sdate = DateUtil.getDateStrToDay(date);//text.setText(sdate);//} catch (Exception e1) {//MessageDialog//.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06");//

//text.setText("");//}//}//}//});//}

private static Date changeDate(String str) throwsException {

Date date= null;if (str.indexOf(" ") >= 0) {

String[] strArray= str.split(":");if (strArray != null) {if (strArray.length == 2) {

str= str + ":00";

}

}

date=DateUtil.getDateTimeFromString(str);

}else{

date=DateUtil.getDateFromString(str);

}returndate;

}/*** 获取限时急件时间

*

*@throwsParseException*/

public staticString getLimitDispatchTime() {try{

String dateTime=DateUtil.getCurrentServerTime();

DateFormat datef=DateFormat.getDateTimeInstance();

SimpleDateFormat m_DateFormatter= newSimpleDateFormat("yyyy-MM-dd HH:mm");

Date sDate=datef.parse(dateTime);

Calendar cal=Calendar.getInstance();

cal.setTime(sDate);

cal.add(Calendar.HOUR,2);

Date eDate=cal.getTime();

String str=m_DateFormatter.format(eDate);returnstr;

}catch(Exception e) {

log.info(e.getMessage());return null;

}

}/*** 获取特急件时间

*

*@throwsParseException*/

public staticString getEspecialDispatchTime() {try{

String dateTime=DateUtil.getCurrentServerTime();

DateFormat datef=DateFormat.getDateTimeInstance();

SimpleDateFormat m_DateFormatter= newSimpleDateFormat("yyyy-MM-dd HH:mm");

Date sDate=datef.parse(dateTime);

Calendar cal=Calendar.getInstance();

cal.setTime(sDate);

cal.add(Calendar.HOUR,8);

Date eDate=cal.getTime();

String str=m_DateFormatter.format(eDate);returnstr;

}catch(Exception e) {

log.info(e.getMessage());return null;

}

}//获取时间到分

public staticString getTimeMinute() {try{

String dateTime=DateUtil.getCurrentServerTime();

DateFormat datef=DateFormat.getDateTimeInstance();

SimpleDateFormat m_DateFormatter= newSimpleDateFormat("yyyy-MM-dd HH:mm");

Date sDate=datef.parse(dateTime);

Calendar cal=Calendar.getInstance();

cal.setTime(sDate);

Date eDate=cal.getTime();

String str=m_DateFormatter.format(eDate);returnstr;

}catch(Exception e) {

log.info(e.getMessage());return null;

}

}public static booleanisYesterdayORtoday(String newsCreatTime)

{try{

String today=DateUtil.getDate();

newsCreatTime=DateUtil.getDateTime(newsCreatTime);

String yesterday=getYesterday(newsCreatTime);if(today.equals(newsCreatTime) ||today.equals(yesterday)){return true;

}

}catch(Exception e)

{

e.printStackTrace();

}return false;

}/*** 获取上一天的信息

*@paramnowDate format:2006-06-17

*@return

*/

public staticString getYesterday(String nowDate)

{

String yesterday= "";int year = 0;int month = 0;int day = 0;try{

year= Integer.parseInt(nowDate.substring(0,nowDate.indexOf("-")));

month= Integer.parseInt(nowDate.substring(nowDate.indexOf("-")+1,nowDate.lastIndexOf("-")));

day= Integer.parseInt(nowDate.substring(nowDate.lastIndexOf("-")+1));

day= day +1;if(day == 0)

{

month= month - 1;if(month == 0)

{//January

month = 12;

day= 31;

year= year - 1;

}else{//not Jan.

switch(month)

{//1|3|5|7|8|10|12) day=31;;

case 1:

day= 31;break;case 3:

day= 31;break;case 5:

day= 31;break;case 7:

day= 31;break;case 8:

day= 31;break;case 10:

day= 31;break;case 12:

day= 31;break;//4|6|9|11) day=30;;

case 4:

day= 30;break;case 6:

day= 30;break;case 9:

day= 30;break;case 11:

day= 30;break;case 2:if(year % 4 ==0 && year % 100 !=0 || year % 400 ==0)

{//leap year

day = 29;

}else day = 28;

}

}

}

String monthStr= "";

String dayStr= "";if(month < 10)

{

monthStr= "0" +String.valueOf(month);

}else{

monthStr=String.valueOf(month);

}if(day < 10)

{

dayStr= "0" +String.valueOf(day);

}else{

dayStr=String.valueOf(day);

}

yesterday= String.valueOf(year) + "-" + monthStr + "-" +dayStr;

}catch(Exception e)

{

e.printStackTrace();

}returnyesterday;

}public static intgetLastDateOfMonth(Date date){int lastDate = 0;

Calendar cal=Calendar.getInstance();

cal.set(date.getYear(), date.getMonth()+1,-1);

lastDate=cal.get(cal.DATE);return lastDate+1;

}public staticDate parseStringToDate(String usedate,String hour,String minute){

Date date=null;

SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm");try{

date= sd.parse(usedate+" "+hour+":"+minute);

}catch(Exception e) {

e.printStackTrace();

}returndate;

}public static voidmain(String[] args) {

System.out.println(DateUtil.getLastDateOfMonth(newDate()));

}public staticString parseStringToDate(String dateStr) {

Date date= null;

SimpleDateFormat sf2= null;try{

SimpleDateFormat sf1= new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.ENGLISH);

date=sf1.parse(dateStr);

sf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

}catch(ParseException e) {

e.printStackTrace();

}returnsf2.format(date);//System.out.println(sf2.format(date));

}public staticString parseStringToDate2(String s){

Date date= null;

SimpleDateFormat sf2= null;try{

SimpleDateFormat sf1= new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);

date=sf1.parse(s);

sf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

}catch(ParseException e) {

e.printStackTrace();

}returnsf2.format(date);

}public staticString parseChinaDateToDate(String s) {

String[] strArr=s.split("月");if (strArr[0].equals("一")) {

strArr[0]="01";

}else if (strArr[0].equals("二")) {

strArr[0]="02";

}else if (strArr[0].equals("三")) {

strArr[0]="03";

}else if (strArr[0].equals("四")) {

strArr[0]="04";

}else if (strArr[0].equals("五")) {

strArr[0]="05";

}else if (strArr[0].equals("六")) {

strArr[0]="06";

}else if (strArr[0].equals("七")) {

strArr[0]="07";

}else if (strArr[0].equals("八")) {

strArr[0]="08";

}else if (strArr[0].equals("九")) {

strArr[0]="09";

}else if (strArr[0].equals("十")) {

strArr[0]="10";

}else if (strArr[0].equals("十一")) {

strArr[0]="11";

}else if (strArr[0].equals("十二")) {

strArr[0]="12";

}

s=strArr[0]+strArr[1];

Date date= null;

SimpleDateFormat sf2= null;try{

SimpleDateFormat sf1= new SimpleDateFormat("MM dd, yyyy", Locale.ENGLISH);

date=sf1.parse(s);

sf2= new SimpleDateFormat("yyyy-MM-dd");

}catch(ParseException e) {

e.printStackTrace();

}returnsf2.format(date);

}

}

二、packagecom.djzh.common.utils;importjava.io.PrintStream;importjava.text.ParseException;importjava.text.ParsePosition;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.Locale;importorg.apache.commons.lang.StringUtils;/*** 日期工具类

*@authorhanlin

**/

public classDateTimeUtils

{public staticDate getNowDate()

{

Date currentTime= newDate();

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);returncurrentTime_2;

}public staticDate getNowDateShort() {

Date currentTime= newDate();

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");

String dateString=formatter.format(currentTime);

ParsePosition pos= new ParsePosition(8);

Date currentTimeFormat=formatter.parse(dateString, pos);returncurrentTimeFormat;

}public static String getYyyyMMddHhmmStrDate(Date date) throwsException{

SimpleDateFormat df= new SimpleDateFormat( "yyyy-MM-dd HH:mm");returndf.format(date);

}public staticString getStrDate(Date date) {

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");returnsdf.format(date);

}public staticString getYyyyMMStrDate(Date date) {

SimpleDateFormat sdf= new SimpleDateFormat("yyyyMM");returnsdf.format(date);

}public staticString getYyyyStrDate(Date date) {

SimpleDateFormat sdf= new SimpleDateFormat("yyyy");returnsdf.format(date);

}public staticInteger getStrIntegerDate(Date date)

{

SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");return newInteger(sdf.format(date));

}public staticString getStrYyyyMMdd(Date date) {

SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");returnsdf.format(date);

}public staticString getStrYyyyMMddHhmmss(Date date) {

SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");returnsdf.format(date);

}public static int getIntervalYear(intinterval)

{

Date currentTime= newDate();return currentTime.getYear() +interval;

}public staticDate strToDate(String strDate) {

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");

ParsePosition pos= new ParsePosition(0);

Date strtodate=formatter.parse(strDate, pos);returnstrtodate;

}public staticString getStringDateShort()

{

Date currentTime= newDate();

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");

String dateString=formatter.format(currentTime);returndateString;

}public staticString dateToStr(Date dateDate)

{

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");

String dateString=formatter.format(dateDate);returndateString;

}public staticString dateToLocalStr(Date dateDate) {

SimpleDateFormat formatter= new SimpleDateFormat("yyyy年MM月dd日");

String dateString=formatter.format(dateDate);returndateString;

}public staticString dateToLocalWeekStr(Date dateDate) {

SimpleDateFormat formatter= new SimpleDateFormat("yyyy年MM月dd日 E", Locale.CHINA);

String dateString=formatter.format(dateDate);returndateString;

}public staticString dateToLocalStrYearAndMonth(Date dateDate)

{

SimpleDateFormat formatter= new SimpleDateFormat("yyyy年MM月");

String dateString=formatter.format(dateDate);returndateString;

}public static longgetDays(String date1, String date2)

{if ((date1 == null) || (date1.equals("")))return 0L;if ((date2 == null) || (date2.equals(""))) {return 0L;

}

SimpleDateFormat myFormatter= new SimpleDateFormat("yyyy-MM-dd");

Date date= null;

Date mydate= null;try{

date=myFormatter.parse(date1);

mydate=myFormatter.parse(date2);

}catch(Exception localException) {

}long day = (date.getTime() - mydate.getTime()) / 86400000L;returnday;

}public staticString getNextDay(String nowdate, String delay)

{try{

SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");

String mdate= "";

Date d=strToDate(nowdate);long myTime = d.getTime() / 1000L + Integer.parseInt(delay) * 24 * 60 * 60;

d.setTime(myTime* 1000L);returnformat.format(d);

}catch(Exception e) {

}return "";

}public staticString getCurrentWeekOfMonth(Calendar calendar)

{

String strWeek= "";int dw = calendar.get(7);if (dw == 1)

strWeek= "星期天";else if (dw == 2)

strWeek= "星期一";else if (dw == 3)

strWeek= "星期二";else if (dw == 4)

strWeek= "星期三";else if (dw == 5)

strWeek= "星期四";else if (dw == 6)

strWeek= "星期五";else if (dw == 7) {

strWeek= "星期六";

}returnstrWeek;

}public static intgetQuarterByStrDate(String date)

{if (date.split("-").length < 3) {return 0;

}int month = Integer.parseInt(date.split("-")[1]);if ((month > 0) && (month <= 3))return 1;if ((month > 3) && (month <= 6))return 2;if ((month > 6) && (month <= 9))return 3;if ((month > 9) && (month <= 12)) {return 4;

}return 0;

}public staticDate getDateByYyyyMMddHHmmss(String yMdhms)

{try{

SimpleDateFormat ymdHmsFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");if (yMdhms.length() > 19) {

yMdhms= yMdhms.substring(0, 19);

}return ymdHmsFormat.parse(yMdhms); } catch(ParseException e) {

}return newDate();

}public staticDate getDateByYyyyMmDdHhMmSs(String ymdhms)

{try{

SimpleDateFormat ymdHmsFormat= new SimpleDateFormat("yyyyMMddHHmmss");return ymdHmsFormat.parse(ymdhms); } catch(ParseException e) {

}return newDate();

}public staticString getYyyyMMddHHmmssStr(Date date)

{

SimpleDateFormat ymdHmsFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");returnymdHmsFormat.format(date);

}public staticDate getYyyyMMddHHmmssDate(String date)

{try{

SimpleDateFormat ymdHmsFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return ymdHmsFormat.parse(date); } catch(ParseException e) {

}return newDate();

}public staticString getYyyyMMddHHmmssHan(Date date)

{

SimpleDateFormat ymdHmsFormat= new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");returnymdHmsFormat.format(date);

}public staticBoolean dateCompare(String myDate)throwsParseException

{

Date nowdate= newDate();

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

Date d=sdf.parse(myDate);returnBoolean.valueOf(d.before(nowdate));

}public staticString parseFormatDateByString(String time)

{

String now= null;try{if (StringUtils.trimToNull(time) == null) {return null;

}

Date date= new SimpleDateFormat("yyyyMMddHHmmss").parse(time);

now= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

}catch(ParseException e) {

e.printStackTrace();

}returnnow;

}public staticString getFormatDate(Date date) {

SimpleDateFormat format= new SimpleDateFormat("yyyy.MM.dd");returnformat.format(date);

}public static void main(String[] args) throwsParseException

{

System.out.println(getStrYyyyMMddHhmmss(newDate()));

System.out.println(getStrYyyyMMdd(newDate()));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值