0

I have a web application(Java EE, Struts 1.1, JSP, Tomcat 5.5.20, WebLogic, Oracle) running on a Linux sever from Red Hat. Some days, the current date of web apps "falls out". For example:

31 January 2013 is changed to: 12 January 1994 So the user has a problem to submit the button on some pages because of the changes of the date.when I restart the tomcat and the webapps comes up,the problem is solved.this problem repeat sometimes on a day and bothers me. what is the problem and how to solve it? Please help me!

code jsp file is:

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

the java file of DateUtil is:

        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());
          }

        }

the java file of ShamsiCalendar:

        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";
      }
...

the java file of FDate:

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());
  }
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • 1
    There's no way to answer this with the information provided. You don't mention where/how the date is being provided, which is the only other reason besides something wrong with the server. – Dave Newton Jan 31 '13 at 16:00
  • the date I use in myWebapps is a java file contains(import java.util.*; import java.text.SimpleDateFormat; import java.text.ParsePosition ) – Zahra Mmajidi Jan 31 '13 at 16:37
  • That doesn't describe how you get the date. – Dave Newton Jan 31 '13 at 16:38
  • better subscribe to time service to synchronize the server time with the universe. – Roman C Jan 31 '13 at 16:49
  • @ZahraMmajidi paste your code in your question between the inside the code block. – Susie Jan 31 '13 at 17:25
  • see http://stackoverflow.com/editing-help for formatting and pasting code in your post – Fusselchen Jan 31 '13 at 17:25
  • 1) What's the FDate constructor doing? Anything interesting? If it suddenly starts returning bogus values, I'd be interested in a closer look at that. 2) Why not use SimpleDateFormat (http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm) to do some of your heavy lifting? Date formatting sucks; don't re-invent that wheel unless you have to. :-) – BlairHippo Jan 31 '13 at 17:51
  • 2
    Consider using JodaTime instead of FDate. It appears that FDate has issue) – DwB Jan 31 '13 at 18:01
  • this webapp are used 7years and doesn't have this problem.this problem occurs newly.Why?what is the problem?I don't know!IS it related to the time of servers?Is it related to cach?Is it related the calling method?the problem is incredibble! – Zahra Mmajidi Jan 31 '13 at 19:23
  • How is FDate compiling? Your constructor for FDate(long) is calling set(long), which doesn't appear to exist; there's set(String), but Java's not going to do that data conversion automatically. – BlairHippo Jan 31 '13 at 20:13
  • Let me rephrase that: This version of FDate shouldn't compile. Either that's not the complete file, or that's not the file that's actually running in your application. (Though if I'm wrong and that "millis" variable is somehow getting converted to a String automatically, I'm willing to bet that's where your problem lives.) – BlairHippo Jan 31 '13 at 20:26
  • public void set(long millis) { internalDate=new Date(millis); internalShamsiDate=ShamsiCalendar.miladiToShamsi(internalDate); } – Zahra Mmajidi Jan 31 '13 at 20:29
  • And what does ShamsiCalendar.miladiToShamsi(Date) look like? – BlairHippo Jan 31 '13 at 20:36
  • I didn't leave the whole code of java file FDate.java here behalf of the limitation.It's part of the src and compile like other files. – Zahra Mmajidi Jan 31 '13 at 20:37

1 Answers1

0

Here's how I'd approach this problem:

It seems as though your FDate(long) constructor is occasionally returning bad dates. To confirm or deny this, I'd send internalDate and internalShamsiDate to a log file or STDOUT just after they get set, and try to reproduce the bug. When you reproduce the bug, look to see if those two values are what you expect.

If they're not, then you know your problem lives somewhere in ShamsiCalendar.miladiToShamsi(Date). If they look good, it lives elsewhere.

And I'd definitely take a closer look at the date-processing methods in ShamsiCalendar. It appears that they all take Strings as inputs, so I presume that code is processing the String to get the information it needs. This approach is inherently error-prone; I wouldn't be at all surprised if there are some Strings they don't process correctly, or if ShamsiCalendar.miladiToShamsi occasionally produces output that's subtly different from what those methods are expecting.

BlairHippo
  • 9,502
  • 10
  • 54
  • 78
  • tnx!why does downning and upping the tomcat,the problem is solved for sometimes? – Zahra Mmajidi Jan 31 '13 at 21:18
  • I honestly don't know; it clears out corrupted data, perhaps? Nor do I know why it was working smoothly for years but just now went stupid. Perhaps there's something about the most recent dates that triggers the String processing issues I suspect may exist? – BlairHippo Jan 31 '13 at 21:32
  • I look at these methods in detail.so it is not related the time of servers.ok? – Zahra Mmajidi Jan 31 '13 at 21:37
  • Wait. "Servers." As in, multiple servers? Is this running on a multi-server cluster? If so, have you made sure ALL the servers are set to the correct date? – BlairHippo Jan 31 '13 at 21:47
  • database oracle on server,and the webapps,earfile on the other server.I don't check the time of the servers.Is it possible the time of the servers fall out and aren't been the same?(in addition,I shutdown the two servers and start them,and so has this problem) – Zahra Mmajidi Jan 31 '13 at 21:56
  • I would definitely check up on that; log into the servers individually and see what time each is set to. – BlairHippo Jan 31 '13 at 22:42
  • ok!if the times of two servers are correct,then what should I check? – Zahra Mmajidi Feb 01 '13 at 06:41
  • See my answer. ShamsiCalendar seems like the most probably culprit, but you need to gather more data before you can say for certain. – BlairHippo Feb 01 '13 at 14:16
  • I checked another project that uses shamsiCalendar and it works fine.Is it possible to be changed the System.currentTimeMillis() after happenning the event(submit the event)? – Zahra Mmajidi Feb 01 '13 at 15:51