DateUtil-时间工具类
package com.jeeplus.modules.utils; /** * <p>Title:ϵͳʹ��ʱ��ת�������� </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: ���¿Ƽ�</p> * * @author unascribed * @version 1.0 */ import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateUtil { /** * ȡ��ϵͳ��ǰʱ��,����ΪTimestamp * * @return Timestamp */ public static Timestamp getNowTimestamp() { Date d = new Date(); Timestamp numTime = new Timestamp(d.getTime()); return numTime; } /** * ȡ��ϵͳ�ĵ�ǰʱ��,����Ϊjava.sql.Date * * @return java.sql.Date */ public static java.sql.Date getNowDate() { Date d = new Date(); return new java.sql.Date(d.getTime()); } /** * ��Timestamp����ת��Ϊyyyy/mm/dd���͵��ַ��� * * @param date * @param strDefault * @return */ public static String TimestampToString(Timestamp date, String strDefault) { String strTemp = strDefault; if (date != null) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); strTemp = formatter.format(date); } return strTemp; } /** * ��Timestamp����ת��Ϊyyyy/mm/dd���͵��ַ���,���Ϊnull,��Ż�"" * * @param date * @return */ public static String TimestampToString(Timestamp date) { return TimestampToString(date, null); } /** * date��ת��ΪString ��ʽΪyyyy/MM/dd * * @param date * @param strDefault * @return */ public static String DateToString(java.sql.Date date, String strDefault) { String strTemp = strDefault; if (date != null) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); strTemp = formatter.format(date); } return strTemp; } public static String DateToString(java.sql.Date date) { return DateToString(date, null); } /** * Stringת��ΪTimestamp���� * * @param strDefault * @param date * @return */ public static Timestamp StringToTimestamp(String strDate) { if (strDate != null && !strDate.equals("")) { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); Date d = formatter.parse(strDate); Timestamp numTime = new Timestamp(d.getTime()); return numTime; } catch (Exception e) { return null; } } else { return null; } } /** * ϵͳʱ��ת����Timestamp���� * @param strDate * @return */ public static Date timeToTimestamp(String strDate) { if (strDate != null && !strDate.equals("")) { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = formatter.parse(strDate); //Timestamp numTime = new Timestamp(d.getTime()); return date; } catch (Exception e) { return null; } } else { return null; } } /** * ϵͳ����ת����Timestamp���� * @param strDate * @return */ public static Timestamp dateToTimestamp(String strDate) { if (strDate != null && !strDate.equals("")) { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date d = formatter.parse(strDate); Timestamp numTime = new Timestamp(d.getTime()); return numTime; } catch (Exception e) { return null; } } else { return null; } } /** * Stringת��Ϊjava.sql.date���ͣ� * * @param strDate * @return */ public static java.sql.Date StringToDate(String strDate) { if (strDate != null && !strDate.equals("")) { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); Date d = formatter.parse(strDate); java.sql.Date numTime = new java.sql.Date(d.getTime()); return numTime; } catch (Exception e) { return null; } } else { return null; } } /** * ����ǰʱ��ת�����ַ��� * * @return */ public static String currDateToStr() { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); return formatter.format(new Date()); } catch (Exception e) { // TODO: handle exception return null; } } public static String currDateToStr4() { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); return formatter.format(new Date()); } catch (Exception e) { // TODO: handle exception return null; } } public static String currDateToStr1() { try { SimpleDateFormat formatter = new SimpleDateFormat("HHmmss"); return formatter.format(new Date()); } catch (Exception e) { // TODO: handle exception return null; } } public static String currDateToStr5() { try { SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); return formatter.format(new Date()); } catch (Exception e) { // TODO: handle exception return null; } } public static String currDateToStr6(Date date ) { try { SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); return formatter.format(date); } catch (Exception e) { // TODO: handle exception return null; } } public static String currDateToStr2() { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); return formatter.format(new Date()); } catch (Exception e) { // TODO: handle exception return null; } } public static String currDateToStr3() { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); return formatter.format(new Date()); } catch (Exception e) { // TODO: handle exception return null; } } public static Date getNowDate1() throws ParseException { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(currentTime); Date currentTime_2 =formatter.parse(dateString); return currentTime_2; } public static String DateToStr(Date date) { try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); return formatter.format(date); } catch (Exception e) { // TODO: handle exception return null; } } public static String dateNext(){ SimpleDateFormat df = new SimpleDateFormat("HHmmss"); Calendar c = new GregorianCalendar(); Date date = new Date(); System.out.println("系统当前时间 :"+df.format(date)); c.setTime(date);//设置参数时间 c.add(Calendar.SECOND,60);//把日期往后增加SECOND 秒.整数往后推,负数往前移动 date=c.getTime(); //这个时间就是日期往后推一天的结果 String str = df.format(date); return str; } /** * ��ʽ���ַ��� * @param str * @return */ public static String StringCoventor(String str) { String s[] = str.split("/"); return s[2] + "-" + s[1] + "-" + s[0]; } /** * �жϵ�ǰʱ���Ƿ���ʱ��� * @return */ public static String[] ifTimesLice() { // java.util.Date []times=new java.util.Date[2]; String[] mydate = new String[2]; Date date = new Date(); // String fristtime; // String endtime; if (date.after(DateUtil.timeToTimestamp("2009-08-20 00:00:00")) && date.before(DateUtil.timeToTimestamp("2009-09-20 23:59:59"))) { mydate[0] = "2009-08-20 00:00:00"; mydate[1] = "2009-09-20 23:59:59"; } else if (date.after(DateUtil.timeToTimestamp("2009-09-21 00:00:00")) && date.before(DateUtil.timeToTimestamp("2009-10-20 23:59:59"))) { mydate[0] = "2009-09-21 00:00:00"; mydate[1] = "2009-10-20 23:59:59"; } // java.util.Date time= new java.util.Date(); // java.util.Date fristtime1=new java.util.Date(20090820); // java.util.Date endtime1=new java.util.Date(20090920); // java.util.Date fristtime2=new java.util.Date(20090921); // java.util.Date endtime2=new java.util.Date(20091020); // if(time.after(fristtime1)&&time.before(endtime1)){ // times[0]=fristtime1; // times[1]=endtime1; // }else if(time.after(fristtime1)&&time.before(endtime1)){ // times[0]=fristtime2; // times[1]=endtime2; // } return mydate; } /*************************************************************************** * �������ƣ�getDate ������ܣ����ص�ǰ���� ****-**-**��ʽ ���� ���� ���� ��2004/10/30 * ����-------����--------��Ҫ-------------------------- **************************************************************************/ public static String getDate() { String year, month, date; Calendar now = Calendar.getInstance(); year = (now.get(Calendar.YEAR) < 1900 ? "" + (1900 + now.get(Calendar.YEAR)) : "" + now.get(Calendar.YEAR)); month = (now.get(Calendar.MONTH) < 9 ? "0" + (now.get(Calendar.MONTH) + 1) : "" + (now.get(Calendar.MONTH) + 1)); date = (now.get(Calendar.DATE) < 10 ? "0" + now.get(Calendar.DATE) : "" + now.get(Calendar.DATE)); return "" + year + "-" + month + "-" + date; } /*************************************************************************** * �������ƣ�getDateOff ������ܣ����ص�ǰ����ǰ���ʱ�� ****-**-**��ʽ ���� ��type * 0-year,1-month,2-day; off ƫ����ֵ ���� ��2004/10/30 * ����-------����--------��Ҫ-------------------------- **************************************************************************/ public static String getDateOff(int type, int off) { String year, month, date; Calendar now = Calendar.getInstance(); if (type == 0) now.add(Calendar.YEAR, off); else if (type == 1) now.add(Calendar.MONTH, off); else if (type == 3) now.add(Calendar.DATE, off); year = (now.get(Calendar.YEAR) < 1900 ? "" + (1900 + now.get(Calendar.YEAR)) : "" + now.get(Calendar.YEAR)); month = (now.get(Calendar.MONTH) < 9 ? "0" + (now.get(Calendar.MONTH) + 1) : "" + (now.get(Calendar.MONTH) + 1)); date = (now.get(Calendar.DATE) < 10 ? "0" + now.get(Calendar.DATE) : "" + now.get(Calendar.DATE)); return "" + year + "-" + month + "-" + date; } /*************************************************************************** * �������ƣ�getTime ������ܣ����ص�ǰ���� ****-**-** **:**��ʽ ���� ���� ���� ��2004/10/30 * ����-------����--------��Ҫ-------------------------- **************************************************************************/ public static String getTime() { String result, year, month, date, hour, minute; Calendar now = Calendar.getInstance(); year = (now.get(Calendar.YEAR) < 1900 ? "" + (1900 + now.get(Calendar.YEAR)) : "" + now.get(Calendar.YEAR)); month = (now.get(Calendar.MONTH) < 9 ? "0" + (now.get(Calendar.MONTH) + 1) : "" + (now.get(Calendar.MONTH) + 1)); date = (now.get(Calendar.DATE) < 9 ? "0" + now.get(Calendar.DATE) : "" + now.get(Calendar.DATE)); hour = (now.get(Calendar.HOUR_OF_DAY) < 10 ? "0" + now.get(Calendar.HOUR_OF_DAY) : "" + now.get(Calendar.HOUR_OF_DAY)); minute = (now.get(Calendar.MINUTE) < 10 ? "0" + now.get(Calendar.MINUTE) : "" + now.get(Calendar.MINUTE)); result = "" + year + "-" + month + "-" + date + " " + hour + ":" + minute; return result; } /** * 字符串转unix时间戳 * @param datetime * @return */ public static Timestamp stringToTimestamp(String datetime) { DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //1361325960 Date d = null; try { d = df.parse(datetime); } catch (ParseException e) { e.printStackTrace(); } Timestamp numTime = new Timestamp(d.getTime()); return numTime; } /* * 将时间转换为时间戳 */ public static long dateToStamp(String s) throws ParseException { String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = simpleDateFormat.parse(s); long ts = date.getTime()/1000; return ts; } /* * 将时间戳转换为时间 */ public static String stampToDate(String s) { String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(s); Date date = new Date(lt); res = simpleDateFormat.format(date); return res; } /** * 返回当前时间UNIX时间戳 * @return */ public static long currentToStamp(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = new Date(); String t = df.format(d); long epoch = 0; try { epoch = df.parse(t).getTime() / 1000; } catch (ParseException e) { e.printStackTrace(); } return epoch; } public static void main(String[] args) throws ParseException { System.out.println(dateNext()); } }