0

我有一个在 Red Hat 的 Linux 服务器上运行的 Web 应用程序(Java EE、Struts 1.1、JSP、Tomcat 5.5.20、WebLogic、Oracle)。有时,网络应用程序的当前日期会“失效”。例如:

2013年1月31日更改为:1994年1月12日所以由于日期的更改,用户在某些页面上提交按钮时出现问题。当我重新启动tomcat并且webapps出现时,问题解决了。这个问题重复了有时在一天和困扰我。问题是什么以及如何解决?请帮我!

代码jsp文件为:

        <TD ALIGN="center" width="35%"><P CLASS="SmallBlackRemark"><font class="RedColor"><b><%=DateUtil.stringDayMountYear()%></b></font></P></TD>

DateUtil 的 java 文件是:

        import java.util.*;

        import java.text.SimpleDateFormat;

        import java.text.ParsePosition;

        import com.fdate.*;

        public class DateUtil{

          public static String getCurrentDate()
          {
            FDate curDate = new FDate(System.currentTimeMillis());
            return curDate.toString();
          }

          public static String getCurrentTime()
          {
            FDate curDate = new FDate(System.currentTimeMillis());
            String time = curDate.getHour() + ":"+curDate.getMinute()+":"+curDate.getSecond();
            return time;
          }

          public static int getCurrentYear()
          {
            FDate curDate = new FDate(System.currentTimeMillis());
            return curDate.getYear();
          }

          public static int getCurrentMonth()
          {
            FDate curDate = new FDate(System.currentTimeMillis());
            return curDate.getMonth();
          }


          public static int getCurrentDay()
          {
            FDate curDate = new FDate(System.currentTimeMillis());
            return curDate.getDate();
          }


        //reverse the order of dd and yyyy in a farsi date string just for fixing farsi presentation problem
          //suppose that input date is in the form of yyyy/mm/dd
          //the output would be as dd/mm/yyyy

          public static String stringDayMountYear(){
            return ShamsiCalendar.weekDayName(ShamsiCalendar.dayOfWeek(DateUtil.getCurrentDate()))+ " "+
                ShamsiCalendar.monthDayName(DateUtil.getCurrentDay())+
                ShamsiCalendar.monthName(DateUtil.getCurrentMonth()) +""  +
                String.valueOf(DateUtil.getCurrentYear());
          }

        }

ShamsiCalendar 的 java 文件:

        package com.fdate;

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

        public class ShamsiCalendar
        {
          protected static final TimeZone DEFAULT_TIMEZONE=TimeZone.getDefault();
          protected static final long DAY_MILLIS=1000*60*60*24;
          protected static final int HOUR_MILLIS=1000*60*60;
          protected static final int MINUTE_MILLIS=1000*60;
          protected static final String SH_ORIGIN_DATE="1379/01/01";
          protected static final int TIMEZONE_RAW_OFFSET=DEFAULT_TIMEZONE.getRawOffset();

          protected static final int TIMEZONE_RAW_OFFSET_HOUR=TIMEZONE_RAW_OFFSET/HOUR_MILLIS;

          protected static final int TIMEZONE_RAW_OFFSET_MINUTE=(TIMEZONE_RAW_OFFSET-(HOUR_MILLIS*TIMEZONE_RAW_OFFSET_HOUR))/MINUTE_MILLIS;

        //  protected static final Date MI_ORIGIN_DATE=new Date(new GregorianCalendar(2000,Calendar.MARCH,20,0,0,0).getTimeInMillis());

          protected static final Date MI_ORIGIN_DATE=new Date(new GregorianCalendar(2000,Calendar.MARCH,20,0,0,0).getTime().getTime());

          protected static final int ORIGIN_WEEK_DAY=Calendar.MONDAY;
          protected static final String STANDARD_FORMAT_PATTERN="yyyy/MM/dd";

          public static final int SATURDAY=Calendar.SATURDAY;
          public static final int SUNDAY=Calendar.SUNDAY;
          public static final int MONDAY=Calendar.MONDAY;
          public static final int TUESDAY=Calendar.TUESDAY;
          public static final int WEDNESDAY=Calendar.WEDNESDAY;
          public static final int THURSDAY=Calendar.THURSDAY;
          public static final int FRIDAY=Calendar.FRIDAY;

          public static final int SHANBEH=Calendar.SATURDAY;
          public static final int YEKSHANBEH=Calendar.SUNDAY;
          public static final int DOSHANBEH=Calendar.MONDAY;
          public static final int SESHANBEH=Calendar.TUESDAY;
          public static final int CHAHARSHANBEH=Calendar.WEDNESDAY;
          public static final int PANJSHANBEH=Calendar.THURSDAY;
          public static final int JOMEH=Calendar.FRIDAY;

        ...

    public ShamsiCalendar() {
      }

      public static Date sysDate()
      {
        return new Date(System.currentTimeMillis());
      }

     public static long millisToDay(long millis )
      {
        return (long)(millis/DAY_MILLIS);
      }

     public static long dayToMillis(long day )
      {
        return day*DAY_MILLIS;
      }

      public static String weekDayName(int dd,boolean brf)  {
        String ds=new String("");

        if(brf)    {
            switch(dd){
                case SHANBEH:                return SHANBEH_TEXT;
                case YEKSHANBEH:             return SHANBEH_TEXT + " 1";
                case DOSHANBEH:              return SHANBEH_TEXT + " 2";
                case SESHANBEH:              return SHANBEH_TEXT + " 3";
                case CHAHARSHANBEH:          return SHANBEH_TEXT + " 4";
                case PANJSHANBEH:            return SHANBEH_TEXT + " 5";
                case JOMEH:                  return JOMEH_TEXT;
              }
          }
          else      {
          switch(dd){
                case SHANBEH:                return SHANBEH_TEXT;
                case YEKSHANBEH:             return YEKSHANBEH_TEXT;
                case DOSHANBEH:              return DOSHANBEH_TEXT;
                case SESHANBEH:              return SESHANBEH_TEXT;
                case CHAHARSHANBEH:          return CHAHARSHANBEH_TEXT;
                case PANJSHANBEH:            return PANJSHANBEH_TEXT;
                case JOMEH:                  return JOMEH_TEXT;
              }
            }
            return SHANBEH_TEXT + Integer.toString(dd) +"_E";
      }
...

FDate的java文件:

package com.fdate;

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

public class FDate
{
  protected static final long DAY_MILLIS=1000*60*60*24;
  protected static final int HOUR_MILLIS=1000*60*60;
  protected static final int MINUTE_MILLIS=1000*60;

  protected Date internalDate;

  protected String internalShamsiDate;

  public FDate(long millis) {
    set(millis);
  }

 public FDate(){
    set();
  }

  public FDate(int year, int month, int date) {
    set(year,month,date);
  }

  public FDate(String shDate) {
    set(shDate);
  }

public String composite(int year , int month , int date){
    String ys;
    String ms;
    String ds;
    DecimalFormat df=new DecimalFormat();
    df.applyPattern("0000");
    ys=df.format(year);
    df.applyPattern("00");
    ms=df.format(month);
    df.applyPattern("00");
    ds=df.format(date);
    return ys + "/" + ms + "/" + ds;
  }

 public int getField(int field){
    GregorianCalendar gc=new GregorianCalendar();
    gc.setTime(internalDate);
    return gc.get(field);
  }

  public void setField(int field, int newValue){
    GregorianCalendar gc=new GregorianCalendar();
    gc.setTime(internalDate);
    gc.set(field,newValue);
    internalDate=gc.getTime();
  }

  public void set(){
    internalDate=new Date();
    internalShamsiDate=ShamsiCalendar.miladiToShamsi(internalDate);
  }

  public void set(int year, int month, int date) {
    internalDate=ShamsiCalendar.shamsiToMiladi(composite(year,month,date));
    internalShamsiDate=ShamsiCalendar.miladiToShamsi(internalDate);
  }

public void set(String shDate) {
    internalDate=ShamsiCalendar.shamsiToMiladi(shDate);
    internalShamsiDate=ShamsiCalendar.miladiToShamsi(internalDate);
  }

public void setTime(int hrs,int min){
    setField(Calendar.HOUR_OF_DAY,hrs);
    setField(Calendar.MINUTE,min);
  }

  public void setTime(int hrs,int min,int sec){
    setField(Calendar.HOUR_OF_DAY,hrs);
    setField(Calendar.MINUTE,min);
    setField(Calendar.SECOND,sec);
  }

  public int getHour(){
    return getField(Calendar.HOUR_OF_DAY);
  }

  public int getMinute(){
    return getField(Calendar.MINUTE);
  }

  public int getSecond(){
    return getField(Calendar.SECOND);
  }

 public int getYear(){
    return ShamsiCalendar.getYear(ShamsiCalendar.miladiToShamsi(internalDate));
  }

  public int getMonth(){
    return ShamsiCalendar.getMonth(ShamsiCalendar.miladiToShamsi(internalDate));
  }

  public int getDate(){
    return ShamsiCalendar.getDate(ShamsiCalendar.miladiToShamsi(internalDate));
  }

  public int dayOfWeek(){
    return getField(Calendar.DAY_OF_WEEK);
  }

  public boolean after(Date when){
    return internalDate.after(when);
  }

  public boolean after(FDate when){
    return internalDate.after(when.get());
  }

  public boolean before(Date when){
    return internalDate.before(when);
  }

  public boolean before(FDate when){
    return internalDate.before(when.get());
  }

  public void nextDay(){
    internalDate.setTime(internalDate.getTime()+DAY_MILLIS);
    internalShamsiDate=ShamsiCalendar.nextDay(internalShamsiDate);
  }

  public void prevDay(){
    internalDate.setTime(internalDate.getTime()-DAY_MILLIS);
    internalShamsiDate=ShamsiCalendar.prevDay(internalShamsiDate);
  }

  public void nextMonth(){
    internalShamsiDate=ShamsiCalendar.nextMonth(internalShamsiDate);
    internalDate=ShamsiCalendar.shamsiToMiladi(internalShamsiDate);
  }

  public void prevMonth(){
    internalShamsiDate=ShamsiCalendar.prevMonth(internalShamsiDate);
    set(ShamsiCalendar.shamsiToMiladi(internalShamsiDate),getHour(),getMinute(),getSecond());
  }

  public void nextYear(){
    internalShamsiDate=ShamsiCalendar.nextYear(internalShamsiDate);
    internalDate=ShamsiCalendar.shamsiToMiladi(internalShamsiDate);
  }


  public void prevYear(){
    internalShamsiDate=ShamsiCalendar.prevYear(internalShamsiDate);
    set(ShamsiCalendar.shamsiToMiladi(internalShamsiDate),getHour(),getMinute(),getSecond());
  }
4

1 回答 1

0

这是我解决这个问题的方法:

似乎您的 FDate(long) 构造函数偶尔会返回错误的日期。为了确认或否认这一点,我会在它们设置后将 internalDate 和 internalShamsiDate 发送到日志文件或 STDOUT,并尝试重现错误。当您重现该错误时,请查看这两个值是否符合您的预期。

如果不是,那么您知道您的问题存在于 ShamsiCalendar.miladiToShamsi(Date) 中的某个地方。如果它们看起来不错,它就住在别处。

我肯定会仔细研究 ShamsiCalendar 中的日期处理方法。似乎它们都将字符串作为输入,因此我假设代码正在处理字符串以获取所需的信息。这种方法本质上容易出错;如果有一些字符串处理不正确,或者 ShamsiCalendar.miladiToShamsi 偶尔会产生与这些方法所期望的略有不同的输出,我一点也不感到惊讶。

于 2013-01-31T20:49:28.747 回答