⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateutil.java

📁 WAP PUSH后台源码,WAP PUSH后台源码
💻 JAVA
字号:
package com.sxit.wap.threads;import java.util.*;import java.sql.*;public class DateUtil {  private static String sysChar;//日期分隔符  static {    sysChar="-";  }  public static Timestamp getStatBeginDate(Timestamp workDate) {    return getFirstDayInMonth(workDate);  }  public static Timestamp getStatEndDate(Timestamp workDate) {    Timestamp endDate = getLastDayInMonth(workDate);    if (endDate.after(getCurrTime())) {      return getCurrTime();    } else {      return getLastDayInMonth(workDate);    }  }  public static Timestamp getFirstDayInMonth(Timestamp currDate) {    String yyyy = getYYYY(currDate);    String mm = getMM(currDate);    return getTimestamp(Integer.parseInt(yyyy), Integer.parseInt(mm), 1);  }  public static Timestamp getLastDayInMonth(Timestamp currDate) {    String yyyy = getYYYY(currDate);    String mm = getMM(currDate);    Timestamp t = getTimestamp(Integer.parseInt(yyyy), Integer.parseInt(mm), 31);    if (Integer.parseInt(getMM(currDate)) != Integer.parseInt(getMM(t))) {      t = getTimestamp(Integer.parseInt(yyyy), Integer.parseInt(mm), 30);      if (Integer.parseInt(getMM(currDate)) != Integer.parseInt(getMM(t))) {        t = getTimestamp(Integer.parseInt(yyyy), Integer.parseInt(mm), 29);        if (Integer.parseInt(getMM(currDate)) != Integer.parseInt(getMM(t))) {          t = getTimestamp(Integer.parseInt(yyyy), Integer.parseInt(mm), 28);          if (Integer.parseInt(getMM(currDate)) != Integer.parseInt(getMM(t))) {            t = getTimestamp(Integer.parseInt(yyyy), Integer.parseInt(mm), 27);          }        }      }    }    return t;  }  /************************取特定时间函数************************/  public static Timestamp getSomeDate(int space){//根据时间间隔来取得某个时间,例如:参数为1时,得到明天的日期,参数为30时,得到一个月后的日期,参数为0时,得到今天的日期,参数为-1时,得到昨天的日期,参数为-30时,得到一个月前的日期,    return getSomeDate(getCurrTime(),space);  }  public static Timestamp getSomeDate(Timestamp t,int space){//比上面的方法多一个日期参数,上面方法默认为当天,所以没此参数    java.util.Date someDate=(java.util.Date)t;    int sign=space<0?-1:1;    space=space<0?-space:space;    int s=space/10;    int y=space%10;    for (int i=0;i<s;i++){      someDate=new java.util.Date(someDate.getTime()+3600*24*1000*10*sign);    }    someDate=new java.util.Date(someDate.getTime()+3600*24*1000*y*sign);    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat ("yyyy", Locale.getDefault());    int someDateYear = Integer.parseInt(formatter.format(someDate));//求一个月前是什么年    formatter = new java.text.SimpleDateFormat ("M", Locale.getDefault());    int someDateMonth = Integer.parseInt(formatter.format(someDate));//求一个月前是几月    formatter = new java.text.SimpleDateFormat ("dd", Locale.getDefault());    int someDateDay = Integer.parseInt(formatter.format(someDate));//求一个月前是几号    return getTimestamp(someDateYear,someDateMonth,someDateDay);  }  public static String getYYYY(Timestamp t){//取特定时间的年份,比如2003年    if (isEmptyTime(t)) return "";    String yyyy=getFM("yyyy",t);    yyyy="0000"+yyyy;    return yyyy.substring(yyyy.length()-4);  }  public static String getYYYY(String date) {//取特定日期字符串(如2003-12-11)的年份    StringTokenizer st = new StringTokenizer(date, "-/");    return st.nextToken();  }  public static String getMM(Timestamp t){//取特定时间的月份,比如4月    if (isEmptyTime(t)) return "";    String mm=getFM("M",t);    mm="00"+mm;    return mm.substring(mm.length()-2);  }  public static String getMM(String date) {//取特定日期字符串(如2003-12-11)的月份    StringTokenizer st = new StringTokenizer(date, "-/");    st.nextToken();    String mm = st.nextToken();    if (mm.length() == 1) mm = "0" + mm;    return mm;  }  public static String getDD(Timestamp t){//取特定时间的日,比如3号    if (isEmptyTime(t)) return "";    String dd=getFM("dd",t);    dd="00"+dd;    return dd.substring(dd.length()-2);  }  public static String getDD(String date) {//取特定日期字符串(如2003-12-11)的号数    StringTokenizer st = new StringTokenizer(date, "-/");    st.nextToken();    st.nextToken();    String dd = st.nextToken();    if (dd.length() == 1) dd = "0" + dd;    return dd;  }  public static String getYYYYMM(Timestamp t){//取特定时间的年月份,比如2003-04    if (isEmptyTime(t)) return "";    return getYYYY(t)+sysChar+getMM(t);  }  public static String getYYYYMMDD(Timestamp t){//取特定时间的年月日,比如2003-04-03    if (isEmptyTime(t)) return "";    return getYYYYMM(t)+sysChar+getDD(t);  }  public static String getHH(Timestamp t){    if (isEmptyTime(t)) return "";    String hh=getFM("H",t);    hh="00"+hh;    return hh.substring(hh.length()-2);  }  public static String getMI(Timestamp t){    if (isEmptyTime(t)) return "";    String mi=getFM("m",t);    mi="00"+mi;    return mi.substring(mi.length()-2);  }  public static String getSS(Timestamp t){    if (isEmptyTime(t)) return "";    String ss=getFM("s",t);    ss="00"+ss;    return ss.substring(ss.length()-2);  }  public static String getHHMISS(Timestamp t){//取特定时间的时分秒,比如12:45:30    if (isEmptyTime(t)) return "";    return getHH(t)+":"+getMI(t)+":"+getSS(t);  }  public static String getYYYYMMDDHHMISS(Timestamp t){//取特定时间的年月日时分秒,比如2003-04-03 12:45:30    if (isEmptyTime(t)) return "";    return getYYYYMMDD(t)+" "+getHHMISS(t);  }  public static Timestamp getTimestamp(int year,int month,int day,int hour,int minute,int second){//通过代入年月日时分秒构造Timestamp时间对象    Calendar cal=Calendar.getInstance(Locale.getDefault());    cal.set(year,month-1,day,hour,minute,second);    return new Timestamp(cal.getTime().getTime());  }  public static Timestamp getTimestamp(int year,int month,int day){//通过代入年月日构造Timestamp时间对象    return getTimestamp(year,month,day,0,0,0);  }  public static Timestamp getTimestamp(String date) {    return getTimestamp(Integer.parseInt(getYYYY(date)),Integer.parseInt(getMM(date)),Integer.parseInt(getDD(date)),0,0,0);  }  public static String DateToString(Timestamp t){    if (t==null) {      return "1970"+sysChar+"01"+sysChar+"01"+" "+"00"+":"+"00"+":"+"00";    }else{      return getYYYYMMDDHHMISS(t);    }  }  /************************取当前时间函数************************/  public static Timestamp getCurrTime(){    return new Timestamp(System.currentTimeMillis());  }  public static String getYYYY(){//取当前年份,比如2003年    return getYYYY(getCurrTime());  }  public static String getMM(){//取当前月份,比如4月    return getMM(getCurrTime());  }  public static String getDD(){//取当前日,比如03号    return getDD(getCurrTime());  }  public static String getYYYYMM(){//取当前年月份,比如2003-04    return getYYYYMM(getCurrTime());  }  public static String getYYYYMMDD(){//取当前年月日,比如2003-04-03    return getYYYYMMDD(getCurrTime());  }  public static String getHH(){    return getHH(getCurrTime());  }  public static String getMI(){    return getMI(getCurrTime());  }  public static String getSS(){    return getSS(getCurrTime());  }  public static String getHHMISS(){//取当前时分秒,比如12:45:30    return getHHMISS(getCurrTime());  }  public static String getYYYYMMDDHHMISS(){//取当前年月日时分秒,比如2003-04-03 12:45:30    return getYYYYMMDDHHMISS(getCurrTime());  }  public static boolean isEmptyTime(Timestamp t) {    return getTimestamp(1970, 01, 01, 00, 00, 00).getTime() == t.getTime();  }  /************************其它方法************************/  public static boolean isDate(String s) {//检查日期格式是否正确    StringTokenizer st=new StringTokenizer(s,"-");    try{      if (st.hasMoreElements()){        String strYear=(String)st.nextElement();        if (strYear.length()==4){          int year=Integer.parseInt(strYear);          if (year>=1970 && year<=2470) {            if (st.hasMoreElements()){              String strMonth=(String)st.nextElement();              if (strMonth.length()==1 || strMonth.length()==2){                int month =Integer.parseInt(strMonth);                if (month >0 && month <=12) {                  if (st.hasMoreElements()){                    String strDay=(String)st.nextElement();                    if (strDay.length()==1 || strDay.length()==2){                      int day =Integer.parseInt(strDay);                      if (day >0 && day <=31) {                        if (!st.hasMoreElements()){                          return true;                        }                      }                    }                  }                }              }            }          }        }      }    }catch(Exception e){    }    return false;  }  public static boolean isTime(String s) {//检查时间格式是否正确    StringTokenizer st=new StringTokenizer(s,":");    try{      if (st.hasMoreElements()){        String strHour=(String)st.nextElement();        if (strHour.length()==2){          int hour=Integer.parseInt(strHour);          if (hour>=0 && hour<24) {            if (st.hasMoreElements()){              String strMinute=(String)st.nextElement();              if (strMinute.length()==2){                int minute =Integer.parseInt(strMinute);                if (minute >=0 && minute <60) {                  if (st.hasMoreElements()){                    String strSecond=(String)st.nextElement();                    if (strSecond.length()==2){                      int second =Integer.parseInt(strSecond);                      if (second >=0 && second <60) {                        if (!st.hasMoreElements()){                          return true;                        }                      }                    }                  }                }              }            }          }        }      }    }catch(Exception e){    }    return false;  }  public static String getDateSql(int dbType, Timestamp t) {    String sql = "";    if (dbType == Database.ORACLE) {      sql = "TO_DATE('" + DateUtil.getYYYYMMDDHHMISS(t) + "', 'YYYY-MM-DD HH24:MI:SS')";    } else if (dbType == Database.SQLSERVER) {      sql = "'" + DateUtil.getYYYYMMDDHHMISS(t) + "'";    } else if (dbType == Database.MYSQL) {      sql = "'" + DateUtil.getYYYYMMDDHHMISS(t) + "'";    }    return sql;  }  public static String getDateCompareSql(int dbType, String dateColumnName, Timestamp bt, Timestamp et) {    String strBt = getDateSql(dbType, bt);    String strEt = getDateSql(dbType, et);    return " " + dateColumnName + " >= " + strBt + " AND " + dateColumnName + " < " + strEt + " ";  }  public static String getDateCompareSql(int dbType, String dateColumnName, String bt, String et) {    return getDateCompareSql(dbType, dateColumnName, getTimestamp(bt), getTimestamp(et));  }  public static String getDateCompareSql(int dbType, String dateColumnName, Timestamp t) {    Timestamp bt = getTimestamp(Integer.parseInt(getYYYY(t)), Integer.parseInt(getMM(t)), Integer.parseInt(getDD(t)));    Timestamp et = getSomeDate(t, 1);    et = getTimestamp(Integer.parseInt(getYYYY(et)), Integer.parseInt(getMM(et)), Integer.parseInt(getDD(et)));    return getDateCompareSql(dbType, dateColumnName, t, et);  }  public static String getDateCompareSql(int dbType, String dateColumnName, String t) {    return getDateCompareSql(dbType, dateColumnName, getTimestamp(t));  }  public static String getDateBeforeSql(int dbType, String dateColumnName, Timestamp et) {    String strEt = getDateSql(dbType, et);    return " " + dateColumnName + " < " + strEt + " ";  }  public static String getDateBeforeSql(int dbType, String dateColumnName, String t) {    return getDateBeforeSql(dbType, dateColumnName, getTimestamp(t));  }  public static Timestamp getLastMonthFirstDay() {    java.util.Calendar calendar = java.util.Calendar.getInstance();    calendar.set(Integer.parseInt(DateUtil.getYYYY()),Integer.parseInt(DateUtil.getMM()),1,0,0,0);    calendar.add(java.util.Calendar.MONTH,-2);    return new Timestamp(calendar.getTime().getTime());  }  public static Timestamp getCurrMonthFirstDay() {    java.util.Calendar calendar = java.util.Calendar.getInstance();    calendar.set(Integer.parseInt(DateUtil.getYYYY()),Integer.parseInt(DateUtil.getMM()),1,0,0,0);    calendar.add(java.util.Calendar.MONTH,-1);    return new Timestamp(calendar.getTime().getTime());  }  public static Timestamp getNextMonthFirstDay() {    java.util.Calendar calendar = java.util.Calendar.getInstance();    calendar.set(Integer.parseInt(DateUtil.getYYYY()),Integer.parseInt(DateUtil.getMM()),1,0,0,0);    calendar.add(java.util.Calendar.MONTH,0);    return new Timestamp(calendar.getTime().getTime());  }  /************************私有方法************************/  private static String getFM(String flag,java.util.Date date){    java.util.Date currentDate = date;    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat (flag,Locale.getDefault());    String result = formatter.format(currentDate);    return result;  }  public static void main(String[] args) {    System.out.println(getYYYYMMDD(getNextMonthFirstDay()));  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -