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

📄 dateutil.java

📁 java 的通用日期处理函数
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    break;
   case ( Calendar.SATURDAY  ):
    gc.add( Calendar.DATE, 0 );
    break;
  }
  return gc.getTime();
 }

 /**
  * 取得指定日期的所处星期的第一天
  *
  * @param date
  *            指定日期。
  * @return 指定日期的所处星期的第一天
  */
 public static synchronized java.util.Date getFirstDayOfWeek(
  java.util.Date date )
 {
  /**
   * 详细设计:
   * 1.如果date是星期日,则减0天
   * 2.如果date是星期一,则减1天
   * 3.如果date是星期二,则减2天
   * 4.如果date是星期三,则减3天
   * 5.如果date是星期四,则减4天
   * 6.如果date是星期五,则减5天
   * 7.如果date是星期六,则减6天
   */
  GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance();
  gc.setTime( date );
  switch ( gc.get( Calendar.DAY_OF_WEEK ) )
  {
   case ( Calendar.SUNDAY  ):
    gc.add( Calendar.DATE, 0 );
    break;
   case ( Calendar.MONDAY  ):
    gc.add( Calendar.DATE, -1 );
    break;
   case ( Calendar.TUESDAY  ):
    gc.add( Calendar.DATE, -2 );
    break;
   case ( Calendar.WEDNESDAY  ):
    gc.add( Calendar.DATE, -3 );
    break;
   case ( Calendar.THURSDAY  ):
    gc.add( Calendar.DATE, -4 );
    break;
   case ( Calendar.FRIDAY  ):
    gc.add( Calendar.DATE, -5 );
    break;
   case ( Calendar.SATURDAY  ):
    gc.add( Calendar.DATE, -6 );
    break;
  }
  return gc.getTime();
 }
 
 public static synchronized java.util.Calendar getFirstDayOfWeek(
  java.util.Calendar gc )
 {
  /**
   * 详细设计:
   * 1.如果date是星期日,则减0天
   * 2.如果date是星期一,则减1天
   * 3.如果date是星期二,则减2天
   * 4.如果date是星期三,则减3天
   * 5.如果date是星期四,则减4天
   * 6.如果date是星期五,则减5天
   * 7.如果date是星期六,则减6天
   */
  switch ( gc.get( Calendar.DAY_OF_WEEK ) )
  {
   case ( Calendar.SUNDAY  ):
    gc.add( Calendar.DATE, 0 );
    break;
   case ( Calendar.MONDAY  ):
    gc.add( Calendar.DATE, -1 );
    break;
   case ( Calendar.TUESDAY  ):
    gc.add( Calendar.DATE, -2 );
    break;
   case ( Calendar.WEDNESDAY  ):
    gc.add( Calendar.DATE, -3 );
    break;
   case ( Calendar.THURSDAY  ):
    gc.add( Calendar.DATE, -4 );
    break;
   case ( Calendar.FRIDAY  ):
    gc.add( Calendar.DATE, -5 );
    break;
   case ( Calendar.SATURDAY  ):
    gc.add( Calendar.DATE, -6 );
    break;
  }
  return gc;
 }

 /**
  * 取得指定日期的所处月份的最后一天
  *
  * @param date
  *            指定日期。
  * @return 指定日期的所处月份的最后一天
  */
 public static synchronized java.util.Date getLastDayOfMonth(
  java.util.Date date )
 {
  /**
   * 详细设计:
   * 1.如果date在1月,则为31日
   * 2.如果date在2月,则为28日
   * 3.如果date在3月,则为31日
   * 4.如果date在4月,则为30日
   * 5.如果date在5月,则为31日
   * 6.如果date在6月,则为30日
   * 7.如果date在7月,则为31日
   * 8.如果date在8月,则为31日
   * 9.如果date在9月,则为30日
   * 10.如果date在10月,则为31日
   * 11.如果date在11月,则为30日
   * 12.如果date在12月,则为31日
   * 1.如果date在闰年的2月,则为29日
   */
  GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance();
  gc.setTime( date );
  switch ( gc.get( Calendar.MONTH ) )
  {
   case 0:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 1:
    gc.set( Calendar.DAY_OF_MONTH, 28 );
    break;
   case 2:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 3:
    gc.set( Calendar.DAY_OF_MONTH, 30 );
    break;
   case 4:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 5:
    gc.set( Calendar.DAY_OF_MONTH, 30 );
    break;
   case 6:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 7:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 8:
    gc.set( Calendar.DAY_OF_MONTH, 30 );
    break;
   case 9:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 10:
    gc.set( Calendar.DAY_OF_MONTH, 30 );
    break;
   case 11:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
  }
  //检查闰年
  if ( ( gc.get( Calendar.MONTH ) == Calendar.FEBRUARY )
   && ( isLeapYear( gc.get( Calendar.YEAR ) ) ) )
  {
   gc.set( Calendar.DAY_OF_MONTH, 29 );
  }
  return gc.getTime();
 }
 
 public static synchronized java.util.Calendar getLastDayOfMonth(
  java.util.Calendar gc )
 {
  /**
   * 详细设计:
   * 1.如果date在1月,则为31日
   * 2.如果date在2月,则为28日
   * 3.如果date在3月,则为31日
   * 4.如果date在4月,则为30日
   * 5.如果date在5月,则为31日
   * 6.如果date在6月,则为30日
   * 7.如果date在7月,则为31日
   * 8.如果date在8月,则为31日
   * 9.如果date在9月,则为30日
   * 10.如果date在10月,则为31日
   * 11.如果date在11月,则为30日
   * 12.如果date在12月,则为31日
   * 1.如果date在闰年的2月,则为29日
   */
  switch ( gc.get( Calendar.MONTH ) )
  {
   case 0:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 1:
    gc.set( Calendar.DAY_OF_MONTH, 28 );
    break;
   case 2:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 3:
    gc.set( Calendar.DAY_OF_MONTH, 30 );
    break;
   case 4:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 5:
    gc.set( Calendar.DAY_OF_MONTH, 30 );
    break;
   case 6:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 7:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 8:
    gc.set( Calendar.DAY_OF_MONTH, 30 );
    break;
   case 9:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
   case 10:
    gc.set( Calendar.DAY_OF_MONTH, 30 );
    break;
   case 11:
    gc.set( Calendar.DAY_OF_MONTH, 31 );
    break;
  }
  //检查闰年
  if ( ( gc.get( Calendar.MONTH ) == Calendar.FEBRUARY )
   && ( isLeapYear( gc.get( Calendar.YEAR ) ) ) )
  {
   gc.set( Calendar.DAY_OF_MONTH, 29 );
  }
  return gc;
 }

 /**
  * 取得指定日期的所处月份的第一天
  *
  * @param date
  *            指定日期。
  * @return 指定日期的所处月份的第一天
  */
 public static synchronized java.util.Date getFirstDayOfMonth( java.util.Date date )
 {
  /**
   * 详细设计: 1.设置为1号
   */
  GregorianCalendar gc = ( GregorianCalendar ) Calendar.getInstance();
  gc.setTime( date );
  gc.set( Calendar.DAY_OF_MONTH, 1 );
  return gc.getTime();
 }
 
 public static synchronized java.util.Calendar getFirstDayOfMonth( java.util.Calendar gc )
 {
  /**
   * 详细设计: 1.设置为1号
   */
  gc.set( Calendar.DAY_OF_MONTH, 1 );
  return gc;
 }

 /**
  * 将日期对象转换成为指定ORA日期、时间格式的字符串形式。如果日期对象为空,返回 一个空字符串对象,而不是一个空对象。
  *
  * @param theDate
  *            将要转换为字符串的日期对象。
  * @param hasTime
  *            如果返回的字符串带时间则为true
  * @return 转换的结果
  */
 public static synchronized String toOraString( Date theDate, boolean hasTime )
 {
  /**
   * 详细设计:
   * 1.如果有时间,则设置格式为getOraDateTimeFormat()的返回值
   * 2.否则设置格式为getOraDateFormat()的返回值
   * 3.调用toString(Date theDate, DateFormat
   * theDateFormat)
   */
  DateFormat theFormat;
  if ( hasTime )
  {
   theFormat = getOraDateTimeFormat();
  }
  else
  {
   theFormat = getOraDateFormat();
  }
  return toString( theDate, theFormat );
 }

 /**
  * 将日期对象转换成为指定日期、时间格式的字符串形式。如果日期对象为空,返回 一个空字符串对象,而不是一个空对象。
  *
  * @param theDate
  *            将要转换为字符串的日期对象。
  * @param hasTime
  *            如果返回的字符串带时间则为true
  * @return 转换的结果
  */
 public static synchronized String toString( Date theDate, boolean hasTime )
 {
  /**
   * 详细设计:
   * 1.如果有时间,则设置格式为getDateTimeFormat的返回值
   * 2.否则设置格式为getDateFormat的返回值
   * 3.调用toString(Date theDate, DateFormat theDateFormat)
   */
  DateFormat theFormat;
  if ( hasTime )
  {
   theFormat = getDateTimeFormat();
  }
  else
  {
   theFormat = getDateFormat();
  }
  return toString( theDate, theFormat );
 }

 /**
  * 标准日期格式
  */
 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
  "MM/dd/yyyy" );
 /**
  * 标准时间格式
  */
 private static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat(
  "MM/dd/yyyy HH:mm" );
 /**
  * 带时分秒的标准时间格式
  */
 private static final SimpleDateFormat DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat(
  "MM/dd/yyyy HH:mm:ss" );
 /**
  * ORA标准日期格式
  */
 private static final SimpleDateFormat ORA_DATE_FORMAT = new SimpleDateFormat(
  "yyyyMMdd" );
 /**
  * ORA标准时间格式
  */
 private static final SimpleDateFormat ORA_DATE_TIME_FORMAT = new SimpleDateFormat(
  "yyyyMMddHHmm" );
 /**
  * 带时分秒的ORA标准时间格式
  */
 private static final SimpleDateFormat ORA_DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat(
  "yyyyMMddHHmmss" );

 /**
  * 创建一个标准日期格式的克隆
  *
  * @return 标准日期格式的克隆
  */
 public static synchronized DateFormat getDateFormat()
 {
  /**
   * 详细设计: 1.返回DATE_FORMAT
   */
  SimpleDateFormat theDateFormat = ( SimpleDateFormat )
   DATE_FORMAT.clone();
  theDateFormat.setLenient( false );
  return theDateFormat;
 }

 /**
  * 创建一个标准时间格式的克隆
  *
  * @return 标准时间格式的克隆
  */
 public static synchronized DateFormat getDateTimeFormat()
 {
  /**
   * 详细设计: 1.返回DATE_TIME_FORMAT
   */
  SimpleDateFormat theDateTimeFormat = ( SimpleDateFormat ) DATE_TIME_FORMAT
   .clone();
  theDateTimeFormat.setLenient( false );
  return theDateTimeFormat;
 }

 /**
  * 创建一个标准ORA日期格式的克隆
  *
  * @return 标准ORA日期格式的克隆
  */
 public static synchronized DateFormat getOraDateFormat()
 {
  /**
   * 详细设计: 1.返回ORA_DATE_FORMAT
   */
  SimpleDateFormat theDateFormat = ( SimpleDateFormat ) ORA_DATE_FORMAT
   .clone();
  theDateFormat.setLenient( false );
  return theDateFormat;
 }

 /**
  * 创建一个标准ORA时间格式的克隆
  *
  * @return 标准ORA时间格式的克隆
  */
 public static synchronized DateFormat getOraDateTimeFormat()
 {
  /**
   * 详细设计: 1.返回ORA_DATE_TIME_FORMAT
   */
  SimpleDateFormat theDateTimeFormat = ( SimpleDateFormat )
   ORA_DATE_TIME_FORMAT.clone();
  theDateTimeFormat.setLenient( false );
  return theDateTimeFormat;
 }

 /**
  * 将一个日期对象转换成为指定日期、时间格式的字符串。 如果日期对象为空,返回一个空字符串,而不是一个空对象。
  *
  * @param theDate
  *            要转换的日期对象
  * @param theDateFormat
  *            返回的日期字符串的格式
  * @return 转换结果
  */
 public static synchronized String toString( Date theDate,
  DateFormat theDateFormat )
 {
  /**
   * 详细设计:
   * 1.theDate为空,则返回""
   * 2.否则使用theDateFormat格式化
   */
  if ( theDate == null )
   return "";
  return theDateFormat.format( theDate );
 }
}

⌨️ 快捷键说明

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