Java对时间格式数据的一些处理

1、这个类是对时间的一些处理


package ideal.com.util;
/**************************
 * 继承java.util.GregorianCalendar.
 * 增强有关功能.
 * @author:Wang F.
 * @date:2001.12.25
 */
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class CalendarPlus extends GregorianCalendar{
   private DateFormat df;
   private GregorianCalendar cld;
   public String strErr;

   public CalendarPlus(){
     super();
     cld=new GregorianCalendar(Locale.CHINA);
   }

  /****************
   * 根据某个时间字符串取得java.util.Date类型.
   * dateStr="yyyy-MM-dd"
   */
  public java.util.Date getMyDate(String dateStr){
   java.util.Date ddd=new java.util.Date();
   //if((dateStr.trim()).length()<9){
    //   return ddd;
   //  }
   try{
      SimpleDateFormat formatter
         = new SimpleDateFormat("yyyy-MM-dd");
       formatter.applyPattern("yyyy-MM-dd");
       ddd=formatter.parse(dateStr);
     }catch(Exception e){
       strErr+=e.toString();
     }
   return ddd;
  }

  /*******************************
   * 将日期字符串转换成中文日期描述
   *dateStr="yyyy-MM-dd"
   */
  public String getChsDate(String dateStr)
  {
      java.util.Date date = getMyDate(dateStr);
      return getChsDate(date);
  }

  /*******************************
   * 将日期转换成中文日期描述
   *dateStr="yyyy-MM-dd HH:mm:ss"
   */
  public String getChsDate(java.util.Date date)
  {
      int year = getYear(date);
      int month= getActualMonth(date);
      int day  = getDay(date);
      String chsDate = String.valueOf(year)+"年"+String.valueOf(month)+"月"+String.valueOf(day)+"日";
      return chsDate;
  }

  /*******************************
   * 根据某个时间字符串取得java.util.Date类型.(包含日期和时间)
   *dateStr="yyyy-MM-dd HH:mm:ss"
   */
  public java.util.Date getMyDateTime(String dateStr){
    java.util.Date ddd=new java.util.Date();
   // if((dateStr.trim()).length()<15){
   //    return ddd;
   //  }
   try{
       SimpleDateFormat formatter
         = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       formatter.applyPattern("yyyy-MM-dd HH:mm:ss");
       if (dateStr.indexOf(" ")<0)
        dateStr += " 00:00:00";
       ddd=formatter.parse(dateStr);
     }catch(Exception e){
       strErr+=e.toString();
     }
   return ddd;
  }


  /**********
   * 取得某个日期的所在年度
   */
  public int getYear(java.util.Date theDate){
    super.setTime(theDate);
    return super.get(Calendar.YEAR);
  }

 /***************************
  * 取得某个日期的月份
  * 比原来java内部的定义都加了1;
  */
  public int getActualMonth(java.util.Date theDate){
   cld.setTime(theDate);
   return cld.get(Calendar.MONTH)+1;
  }

 /***************************
  *取得当前日期的月份
  */
  public int getMonth(java.util.Date theDate){
   cld.setTime(theDate);
   return cld.get(Calendar.MONTH);
  }
 /******************************
  * 取得当前是几号
  */
  public int getDay(java.util.Date theDate){
    cld.setTime(theDate);
    return  cld.get(Calendar.DAY_OF_MONTH);
  }
 /****************************
  * 取得当前是礼拜几,中文
  */
  public String getDayInWeekChs(java.util.Date theDate){
    cld.setTime(theDate);
    int weekDay=-1;
    weekDay=cld.get(Calendar.DAY_OF_WEEK);
      switch(weekDay){
        case 1:
          return "星期日";
        case 2:
           return "星期一";
        case 3:
           return "星期二";
        case 4:
           return "星期三";
        case 5:
           return "星期四";
        case 6:
           return "星期五";
        case 7:
           return "星期六";
        default:
           return "不可能";
      }
  }
 
  public int getDayInWeek(java.util.Date theDate)
  {
 cld.setTime(theDate);
 int weekDay=cld.get(Calendar.DAY_OF_WEEK);
 return weekDay;
  }
  /************************
   * 取得本月最后一天是几号.
   */
  public int getLastDayInMonth(java.util.Date theDate) {
      int dayCount=0;
      int nMonth;
      int nYear;
      nMonth=getActualMonth(theDate);
      nYear=getYear(theDate);
       switch (nMonth){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
                dayCount = 31;
                break;
        case 2:
                if (isRunYear(nYear))
                  dayCount = 29;
                else
                  dayCount = 28;
               break;
        case 4:
        case 6:
        case 9:
        case 11:
            dayCount = 30;

      }
    return dayCount;
  }
  /******************
   *判断是否是闰年
   */
  public boolean isRunYear(int theYear ){
    boolean isIt;
    if ((theYear%4)==0)
        if ((theYear%100) == 0)
           if ((theYear%400) == 0){
               isIt = true;
               return isIt;
            }else{
               isIt = false;
               return isIt;
           }
       else{
          isIt= true;
          return isIt;
       }
   else{
       isIt = false;
       return isIt;
     }


  }
  /***********************
   * 取得某个日期所在的一周的第一天的日期.
   */
  public java.util.Date getStartWeekDay(java.util.Date theDate){
    java.util.Date ddd=new java.util.Date();
    cld.setTime(theDate);
    int  i=cld.get(Calendar.DAY_OF_WEEK)-cld.MONDAY;
    cld.add(Calendar.DATE,-i);
   try{
      df=DateFormat.getDateInstance();
      ddd =df.parse(df.format(cld.getTime()).toString());
     }catch(Exception e){

     }
    return ddd;
  }
 /********************************
  * 取增加若干天后的日期.
  * gap--增加的
  */
 public  java.util.Date add(java.util.Date theDate,int gap){
    cld.setTime(theDate);
    cld.add(Calendar.DATE,gap);
    return cld.getTime();
  }
 /*******************************
  * 取得标准'yyyy-mm-dd'类型的日期字符
  */
  public String fmtDate(java.util.Date theDate){
   SimpleDateFormat formatter
     = new SimpleDateFormat ("yyyy-MM-dd");
  String dateString = formatter.format(theDate);
   return dateString;
  }

 /***********************************
  *取得时间'yyyy-mm-dd hh:mm:ss'格式
  */
 public String fmtDateTime(java.util.Date theDateTime){
    SimpleDateFormat formatter
     = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
    String dateString = formatter.format(theDateTime);
    return dateString;
 }


  /**************************
   * 显示当前年度下拉列表框
   * y--如果取默认当前年,y=-1;
   * y=0 ,代表默认为空.
   * y>0代表要作为默认值的年
   */
  public String getYearList(int y){
    java.util.Date myDateNow= new  java.util.Date();
    int yearNow;
    int i;

    if(y<=0){
       yearNow=getYear(myDateNow);
     }else{
       yearNow=y;
     }
    i=yearNow-5;
    String rtnStr="<option value=/"/"></option>";
    while(i<(yearNow+5)){
      if(i==yearNow && (y==-1 || y>0) )
         rtnStr+="<option value="+i+" selected >"+i+"</option>";
      else
        rtnStr+="<option value="+i+" >"+i+"</option>";
      i++;
    }
    return rtnStr;
  }

  public String getChsTime(java.util.Date date)
  {
      int hour=getHour(date);
      int minute=getMinutes(date);
      String chsTime = String.valueOf(hour)+"时"+String.valueOf(minute)+"分";
      return chsTime;
  }

  public String getChsTime(String dateStr)
  {
      java.util.Date date = getMyDateTime(dateStr);
      return getChsTime(date);
  }

  public int getHour(java.util.Date theDate)
  {
    super.setTime(theDate);
    return super.get(Calendar.HOUR_OF_DAY);
  }

  public int getMinutes(java.util.Date theDate)
  {
    super.setTime(theDate);
    return super.get(Calendar.MINUTE);
  }

}

2、页面中实现显示当前时间为大写形式

<%@ page contentType="text/html; charset=GBK"%>
<jsp:directive.page import="ideal.com.util.CalendarPlus"/>
<html>
<head>
</head>
<%
java.util.Date today=new java.util.Date();
CalendarPlus cldPlus=new CalendarPlus();

%>
<body>
<form name="f1">

<table width=100%>
 
<td width=50% align=right><font align=right style="font-size:9pt">当前日期:

<%=cldPlus.getChsDate(today)%> (<%=cldPlus.getDayInWeekChs(today)%>)</font></td>


</table>
</form>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值