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

📄 dateclass.java

📁 一个关于日期处理的基础类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package hp.base.util;

import java.util.*;
import java.text.*;

public class DateClass {

  /**
   * 将日期字符串转换为日期类型
   * @param date 日期字符串
   * @return 相应的日期类型
   */
  public static Date convertStr2Date(String date) {
    Date retD = null;
    //Calendar tmp ;
    //tmp.getActualMaximum()
    int yyyy, mm, dd = 0;
    // Date
    String year = date.substring(0, 4);
    String month = date.substring(5, 7);
    String day = date.substring(8, 10);
    try {
      yyyy = Integer.parseInt(year);
      mm = Integer.parseInt(month);
      dd = Integer.parseInt(day);
    }
    catch (Exception e) {
      System.out.print("can't convert string to number in convertStr2Date()");
      e.printStackTrace();
      return null;
    }
    try {
      GregorianCalendar today = new GregorianCalendar(yyyy, mm - 1, dd);
      retD = today.getTime();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    finally {
      return retD;
    }
  }

  /**
   * 取输入日期的下一工作日
   * @param day String
   * @return String
   */
  public static String getNextWorkDay(String day) {
    String nextdaystr = "";
    int[] intday = DateClass.splitDateStr2int(day);
    GregorianCalendar today = new GregorianCalendar(intday[0], intday[1] - 1,
        intday[2]);
    if (today.get(today.DAY_OF_WEEK) == today.FRIDAY) {
      today.add(today.DATE, 3);
    }
    else if (today.get(today.DAY_OF_WEEK) == today.SATURDAY) {
      today.add(today.DATE, 2);
    }
    else {
      today.add(today.DATE, 1);
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    nextdaystr = sdf.format(today.getTime());
    return nextdaystr;
  }

  /**
   * 取输入日期的5个工作日前的日期
   * @param day String
   * @return String
   */
  public static String get5workdaybefore(String day) {
    String theday = "";
    int[] intday = DateClass.splitDateStr2int(day);
    GregorianCalendar today = new GregorianCalendar(intday[0], intday[1] - 1,
        intday[2]);
    if (today.SATURDAY == today.get(today.DAY_OF_WEEK)) {
      today.add(today.DATE, -5);
    }
    else if (today.SUNDAY == today.get(today.DAY_OF_WEEK)) {
      today.add(today.DATE, -6);
    }
    else {
      today.add(today.DATE, -7);
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    theday = sdf.format(today.getTime());
    return theday;
  }

  /**
   * 取得当前系统时间
   * @return 当前系统时间
   */
  public static String getNowTime() {
    Date now = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(now);
  }

  /**
   * 取得当前系统日期
   * @return 当前系统日期
   */
  public static String getNowDate() {
    Date now = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //String nowTime = sdf.format(now);
    // now.parse(now);
    return sdf.format(now);
  }

  /**
   * 根据传入格式取得当前系统日期
   * @return 当前系统日期
   */
  public static String getNowDate( String sfmt ) {
    Date now = new Date();
    if( null == sfmt || "".equals( sfmt ) ) sfmt = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat( sfmt );
    //String nowTime = sdf.format(now);
    // now.parse(now);
    return sdf.format(now);
  }

  /**
   * 取得当前系统日期
   * @return 当前系统日期
   */
  public static String getNowDate2() {
    Date now = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
   // String nowTime = sdf.format(now);
    return sdf.format(now);
  }

  /**
   * 根据入口参数计算对应的上月日期(日期格式必须为yyyy-MM-dd)
   * @param todayStr 输入日期
   * @return 对应的上月日期
   */
  public static String getLastMonth(String todayStr) {
    if (todayStr == null || todayStr.trim().length() <= 0) {
      return "";
    }
    String retDateStr = "";
    int yyyy, mm, dd = 0;
    String year = todayStr.substring(0, 4);
    String month = todayStr.substring(5, 7);
    String day = todayStr.substring(8, 10);
    try {
      yyyy = Integer.parseInt(year);
      mm = Integer.parseInt(month);
      dd = Integer.parseInt(day);
    }
    catch (Exception e) {
      e.printStackTrace();
      return "";
    }
    try {
      GregorianCalendar today = new GregorianCalendar(yyyy, mm - 2, dd);
      Date d = today.getTime();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      retDateStr = sdf.format(d);
      return retDateStr;
    }
    catch (Exception ex) {
      return null;
    }
  }

  /**
   * 比较两个日期,返回较小的日期,如果两个日期相等,返回第二个日期
   * @param date1 日期一
   * @param date2 日期二
   * @return 较小的日期,如果两个日期相等,返回第二个日期
   */
  public static String getMinnerDate(String date1, String date2) {
    if (date1 == null || date1.trim().length() <= 0 || date2 == null ||
        date2.trim().length() <= 0) {
      return "";
    }
    boolean isDate1Minner = false;
    if (date1.trim().length() == date2.trim().length()) {
      for (int i = 0; i < date1.trim().length(); i++) {
        char a = date1.trim().charAt(i);
        char b = date2.trim().charAt(i);
        if (a == b) {
          continue;
        }
        if (a > b) {
          isDate1Minner = false;
          break;
        }
        if (a < b) {
          isDate1Minner = true;
          break;
        }
      }
      if (isDate1Minner) {
        return date1.trim();
      }
      else {
        return date2.trim();
      }
    }
    else {
      System.out.println("日期字符串的长度不一致,无法比较。");
      return null;
    }
  }

  /**
   * 根據输入的long型數與格式返回相同格式的字符串
   * @param _date
   * @param _fmt
   * @return
   * @author 況游波
   */
  public static String getSoucreDate( long _date, String _fmt )
  {
      Date d = new Date();
      d.setTime( _date );
      SimpleDateFormat sdf = null;
      if( null == _fmt || "".equals(_fmt) )
          _fmt = "yyyy-MM-dd HH:mm:ss";
      sdf = new SimpleDateFormat(_fmt);
      return sdf.format( d );
  }

  /**
   * 輸入日期字符串,格式為yyyy-MM-dd,返回long型數字
   * @author 況游波
   * @param _date
   * @return
   */
  static public long getLongDate( String _date )
  {
      if (_date == null || _date.trim().length() != 10) {
          System.out.print("param error in getCorrespondingDate()");
          return -1;
      }
      int[] splitDate = DateClass.splitDateStr2int(_date);

      if (splitDate == null || splitDate.length != 3) {
          System.out.print("日期参数错误,无法计算相应日期");
          return -1;
      }
      try {
      GregorianCalendar retDate = new GregorianCalendar( (splitDate[0] ),
          (splitDate[1] - 1 ), (splitDate[2] ));
      return retDate.getTimeInMillis();
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return -1;
    }
  }

  /**
   * 比較兩個日期之間相隔的天數
   * @param b_date 開始日期
   * @param e_date 結束日期
   * @param _day   比較的天數
   * @return
   */
  public static long compareToDay( String b_date, String e_date, int _day )
  {
      return DateClass.compareToDay( b_date, e_date ) - (_day *86400000l);
  }

  /**
   * 返回兩日期之間的時間差,計算為 long 型數字
   * @param b_date  開始日期
   * @param e_date  結束日期

⌨️ 快捷键说明

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