persiancoders.com

کار بار تاریخ شمسی و میلادی در جاوا (اندروید)

1 ارسال در این موضوع قرار دارد

[align=justify]استفاده از تاریخ شمسی و تبدیل تاریخ میلادی به شمسی و بالعکس یکی از مهمترین بخش های برنامه های فارسی زبان است .

در این پست ۳ کلاس قرار خواهم داد که در مجموع با استفاده از آن ها می توانید به راحتی تاریخ میلادی را به شمسی و بالعکس تبدیل کرده و

همچنین اعمالی از قبیل نمایش روز و ماه به صورت متنی (مثلا دوم آبان ماه) ، روی آن انجام دهید و در پروژه هایی که با جاوا می نویسید استفاده نمایید.[/align]

[align=justify]یک پکیج در پروژه خود ایجاد کنید (مثلا Date )  و کلاس های زیر رار در آن بسازید (ShamsiCalendar وShamsiCalendar و ChangeDate):[/align]

[align=justify][shcode=java]package Date;

import java.text.DecimalFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.TimeZone;

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 static final String SHANBEH_TEXT="شنبه";

  public static final String YEKSHANBEH_TEXT="يکشنبه";

  public static final String DOSHANBEH_TEXT="دوشنبه";

  public static final String SESHANBEH_TEXT="سه شنبه";

  public static final String CHAHARSHANBEH_TEXT="چهار شنبه";

  public static final String PANJSHANBEH_TEXT="پنج شنبه";

  public static final String JOMEH_TEXT="جمعه";

  public static final int FARVARDIN=Calendar.JANUARY;

  public static final int ORDIBEHESHT=Calendar.FEBRUARY;

  public static final int KHORDAD=Calendar.MARCH;

  public static final int TIR=Calendar.FEBRUARY;

  public static final int MORDAD=Calendar.APRIL;

  public static final int SHAHRIVAR=Calendar.MAY;

  public static final int MEHR=Calendar.JUNE;

  public static final int ABAN=Calendar.JULY;

  public static final int AZAR=Calendar.AUGUST;

  public static final int DAYMAH=Calendar.SEPTEMBER;

  public static final int BAHMAN=Calendar.OCTOBER;

  public static final int ESFAND=Calendar.DECEMBER;

  public static final String FARVARDIN_TEXT="فروردين";

  public static final String ORDIBEHESHT_TEXT="ارديبهشت";

  public static final String KHORDAD_TEXT="خرداد";

  public static final String TIR_TEXT="تير";

  public static final String MORDAD_TEXT="مرداد";

  public static final String SHAHRIVAR_TEXT="شهريور";

  public static final String MEHR_TEXT="مهر";

  public static final String ABAN_TEXT="آبان";

  public static final String AZAR_TEXT="آذر";

  public static final String DAYMAH_TEXT="دي";

  public static final String BAHMAN_TEXT="بهمن";

  public static final String ESFAND_TEXT="اسفند";

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  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 long minusDate(Date d1,Date d2)

  {

    Date td1=truncate(d1);

    Date td2=truncate(d2);

    return millisToDay(td1.getTime()-td2.getTime());

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static Date plusDay(Date miDate,long dayCount)

  {

    return new Date(miDate.getTime()+(dayCount*DAY_MILLIS));

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static Date truncate(Date miDate)

  {

    double mil=miDate.getTime() + TIMEZONE_RAW_OFFSET;

    return new Date(DAY_MILLIS*((long)mil/DAY_MILLIS));

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String format(Date miDate,String formatStr)

  {

    String retStr;

    SimpleDateFormat sdf=new  SimpleDateFormat();

    try{

      sdf.applyPattern(formatStr);

      }

    catch(Exception e)

    {

      sdf.applyPattern(STANDARD_FORMAT_PATTERN);

    }

    try

    {

      retStr=sdf.format(miDate);

    }

    catch(Exception e)

    {

      retStr=sdf.format(miDate);

    }

    return retStr;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static long toMillis(Date miDate)

  {

    return miDate.getTime();

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String compositeDate(int yyyy,int mm,int dd) {

    DecimalFormat df=new DecimalFormat();

    df.applyPattern("0000");

    String ys=df.format(yyyy);

    df.applyPattern("00");

    String ms=df.format(mm);

    df.applyPattern("00");

    String ds=df.format(dd);

    String retstr=df.format(dd);

    retstr=ys + "/" + ms + "/" + ds ;

    return retstr;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String decompositeYear(String shDate)

  {

    return shDate.substring(0,4);

  }

  public static String decompositeMonth(String shDate)

  {

    return shDate.substring(5,7);

  }

  public static String decompositeDay(String shDate)

  {

    return shDate.substring(8);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int getYear(String shDate)

  {

    return Integer.parseInt(shDate.substring(0,4));

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int getMonth(String shDate)

  {

    return Integer.parseInt(shDate.substring(5,7));

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int getDate(String shDate)

  {

    return Integer.parseInt(shDate.substring(8));

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static boolean isLeepYear(int yyyy)

  {

    if( ((yyyy+38)*31)%128<=30 )

      return true;

    else

      return false;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int yearDayCount(int yyyy)

  {

    if(isLeepYear(yyyy))

      return 366;

    else

      return 365;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String monthDayName(int intDay){

  switch (intDay) {

    case 1:      return "اول ";

    case 2:      return "دوم ";

    case 3:      return "سوم ";

    case 4:      return "چهارم ";

    case 5:      return "پنجم ";

    case 6:      return "ششم ";

    case 7:      return "هفتم ";

    case 8:      return "هشتم ";

    case 9:      return "نهم ";

    case 10:     return "دهم ";

    case 11:     return "يازدهم ";

    case 12:     return "دوازدهم ";

    case 13:     return "سيزدهم ";

    case 14:     return "چهاردهم ";

    case 15:     return "پانزدهم ";

    case 16:     return "شانزدهم ";

    case 17:     return "هفدهم ";

    case 18:     return "هجدهم ";

    case 19:     return "نوزدهم ";

    case 20:     return "بيستم ";

    case 21:     return "بيست و يکم ";

    case 22:     return "بيست و دوم ";

    case 23:     return "بيست و سوم ";

    case 24:     return "بيست و چهارم ";

    case 25:     return "بيست و پنجم ";

    case 26:     return "بيست و ششم ";

    case 27:     return "بيست و هفتم ";

    case 28:     return "بيست و هشتم ";

    case 29:     return "بيست و نهم ";

    case 30:     return "سي ام ";

    case 31:     return "سي و يکم ";

    default:     return "شتباه : "+ Integer.toString(intDay);

  }

}

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String monthName(int mm)  {

    switch(mm)    {

      case 1:        return FARVARDIN_TEXT;

      case 2:        return ORDIBEHESHT_TEXT;

      case 3:        return KHORDAD_TEXT;

      case 4:        return TIR_TEXT;

      case 5:        return MORDAD_TEXT;

      case 6:        return SHAHRIVAR_TEXT;

      case 7:        return MEHR_TEXT;

      case 8:        return ABAN_TEXT;

      case 9:        return AZAR_TEXT;

      case 10:       return DAYMAH_TEXT;

      case 11:       return BAHMAN_TEXT;

      case 12:       return ESFAND_TEXT;

      default:       return "اشتباه : " + Integer.toString(mm);

    }

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int monthDayCount(int yyyy,int mm)

  {

    switch(mm)

      {

        case 1: case 2: case 3: case 4: case 5: case 6:

     return 31;

        case 7: case 8: case 9: case 10: case 11:

      return 30;

   case 12:

     if(isLeepYear(yyyy))

      return 30;

     else

      return 29;

   default:

     return 0;

      }

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int monthDayCount(String shDate)

  {

   return monthDayCount(getYear(shDate),getMonth(shDate));

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

  }

  public static String weekDayName(int dd)  {

    String ds=new String("");

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

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String nextDay(String shDate)

  {

    int Y=getYear(shDate);

    int M=getMonth(shDate);

    int D=getDate(shDate);

    if(D==monthDayCount(Y,M))

      {

        if(M==12)

        {

          Y=Y+1;

          M=1;

          D=1;

        }

        else

        {

      M=M+1;

      D=1;

    }

      }

     else

       D=D+1;

    return compositeDate(Y,M,D);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String prevDay(String shDate)

  {

    int Y=getYear(shDate);

    int M=getMonth(shDate);

    int D=getDate(shDate);

    if(D==1)

    {

     if(M==1)

          {

       Y=Y-1;

       M=12;

       D=monthDayCount(Y,M);

          }

     else

          {

       M=M-1;

       D=monthDayCount(Y,M);

          }

      }

      else

     D=D-1;

    return compositeDate(Y,M,D);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String nextMonth(String shDate)

  {

    int Y=getYear(shDate);

    int M=getMonth(shDate);

    int D=getDate(shDate);

    if(M==12)

    {

      M=1 ;

      Y=Y+1;

    }

    else

       M=M+1;

    if(D>monthDayCount(Y,M))

      D=monthDayCount(Y,M);

    return compositeDate(Y,M,D);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String prevMonth(String shDate)

  {

    int Y=getYear(shDate);

    int M=getMonth(shDate);

    int D=getDate(shDate);

    if(M==1)

    {

      M=12 ;

      Y=Y-1;

    }

    else

      M=M-1;

    if(D>monthDayCount(Y,M))

      D=monthDayCount(Y,M);

    return compositeDate(Y,M,D);

}

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String nextYear(String shDate)

  {

    int Y=getYear(shDate);

    int M=getMonth(shDate);

    int D=getDate(shDate);

    Y=Y+1;

    if(D>monthDayCount(Y,M))

      D=monthDayCount(Y,M);

    return compositeDate(Y,M,D);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String prevYear(String shDate)

  {

    int Y=getYear(shDate);

    int M=getMonth(shDate);

    int D=getDate(shDate);

    Y--;

    if(D>monthDayCount(Y,M))

      D=monthDayCount(Y,M);

    return compositeDate(Y,M,D);

}

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String addYear(String shDate,int Y_count)

  {

    int i;

    String TMP_DATE=new String(shDate);

    if(Y_count==0)

      return shDate;

    else if(Y_count>0)

      for(i=1;i<=Y_count;i++)

        TMP_DATE=nextYear(TMP_DATE);

    else if(Y_count<0)

      for(i=1;i<=Math.abs(Y_count);i++)

        TMP_DATE=prevYear(TMP_DATE);

    return TMP_DATE;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String addMonth(String shDate,int M_count)

  {

    int i;

    String TMP_DATE=new String(shDate);

    if(M_count==0)

      return shDate;

    else if(M_count>0)

      for(i=1;i<=M_count;i++)

        TMP_DATE=nextMonth(TMP_DATE);

    else if(M_count<0)

      for(i=1;i<=Math.abs(M_count);i++)

        TMP_DATE=prevMonth(TMP_DATE);

    return TMP_DATE;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static long miBetween(Date d1,Date  d2)

  {

    return minusDate(d1,d2);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int shBetween(String shDate1,String shDate2)

  {

   String DT1=new String("");

   String DT2=new String("");

   int Y1;

   int M1;

   int D1;

   int Y2;

   int M2;

   int D2;

   int SGN=1;

   int count=0;

   int i  ;

   if(shDate1.equals(shDate2))

    return 0;

   else if(shDate1.compareTo(shDate2)<0)

   {

   DT1=shDate2;

   DT2=shDate1;

   SGN=-1;

   }

   else if(shDate1.compareTo(shDate2)>0)

   {

   DT1=shDate1;

   DT2=shDate2;

   SGN=1;

  }

  Y1=getYear(DT1);M1=getMonth(DT1);D1=getDate(DT1);

  Y2=getYear(DT2);M2=getMonth(DT2);D2=getDate(DT2);

  if(Y1==Y2)

  {

      if(M1==M2)

        count=D1-D2;

      else

      {

      count=count+monthDayCount(Y2,M2)-D2;

      for(i=M2+1;i<=M1-1;i++)

             count=count+monthDayCount(Y1,i);

      count=count+D1;

       }

    }

   else

   {

      count=count+monthDayCount(Y2,M2)-D2;

      for(i=M2+1;i<=12;i++)

       count=count+monthDayCount(Y2,i);

      for(i=Y2+1;i<=Y1-1;i++)

       count=count+yearDayCount(i);

      for(i=1;i<=M1-1;i++)

       count=count+monthDayCount(Y1,i);

      count=count+D1;

    }

  return SGN*(count);

 }

 //+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 public static String minusDay(String shDate,int D_count)

 {

    int count =Math.abs(D_count);

    int Y=getYear(shDate);

    int M=getMonth(shDate);

    int D=getDate(shDate);

    int NY=Y;

    int NM=M;

    int ND=D;

    if(count==0)

   return shDate;

    else

      if(count>=ND)

          {

         count=count-ND;

         if(NM==1)

                        {

                          NM=12;

           NY=NY-1;

                        }

         else

                        {

                              NM=NM-1;

                              while(count>monthDayCount(NY,NM) && NM>=1)

                              {

                 count=count-monthDayCount(NY,NM);

                 NM=NM-1;

               }

                              if(NM<1)

                              {

            NM=12;

            NY=NY-1;

                              }

          }

              while(count>yearDayCount(NY))

                        {

            count=count-yearDayCount(NY);

            NY=NY-1;

                        }

              while(count>monthDayCount(NY,NM))

                        {

               count=count-monthDayCount(NY,NM);

               NM=NM-1;

                        }

              if( count==0 )

             ND=monthDayCount(NY,NM);

                  else

             ND=monthDayCount(NY,NM)-count;

              }

              else

           ND=ND-count;

      return compositeDate(NY,NM,ND);

    }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String plusDay(String shDate,int dayCount)

  {

    int count=dayCount;

    int Y=getYear(shDate);

    int M=getMonth(shDate);

    int D=getDate(shDate);

    int NY=Y;

    int NM=M;

    int ND=D;

    if(count<0)

        return minusDay(shDate,dayCount);

    else if( count==0 )

        return shDate;

    else if(count>0)

        {

      if(count>(monthDayCount(NY,NM)-ND))

                {

                    count=count-(monthDayCount(NY,NM)-ND);

          if(NM==12)

               {

                      NM=1;

            NY=NY+1;

          }

          else

                    {

              NM=NM+1;

              while(count>monthDayCount(NY,NM) && NM<=12)

                        {

           count=count-monthDayCount(NY,NM);

           NM=NM+1;

                        }

                        if(NM>12)

                        {

           NM=1;

           NY=NY+1;

                        }

                    }

          while(count>yearDayCount(NY))

                    {

            count=count-yearDayCount(NY);

            NY=NY+1;

          }

          while(count>monthDayCount(NY,NM))

                    {

                      count=count-monthDayCount(NY,NM);

            NM=NM+1;

                    }

          if(count==0)

            ND=1;

          else

            ND=count;

                }

      else

         ND=ND+count;

          return compositeDate(NY,NM,ND);

   }

        else

          return shDate;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String plusSomeDay(String shDate,int dayCount)

  {

    int count=dayCount;

    int i;

    String T_D=new String(shDate);

    if(count<0)

   return minusSomeDay(shDate,dayCount);

    else if(count==0)

      return shDate;

    else //count>0

    {

      for(i=1;i<=count;i++)

        T_D=nextDay(T_D);

      return T_D;

    }

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String minusSomeDay(String shDate,int dayCount)

  {

    int count=Math.abs(dayCount);

    int i;

    String T_D= new String(shDate);

    if(count==0)

   return shDate;

    else

    {

   for(i=1;i<=count;i++)

      T_D=prevDay(T_D);

   return T_D;

    }

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String miladiToShamsi_persiancoders_com(Date miDate)

  {

    int btw;

    btw=(int)miBetween(miDate,MI_ORIGIN_DATE);

    return plusDay(SH_ORIGIN_DATE,btw);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static Date shamsiToMiladi_persiancoders(String shDate)

  {

    int count;

    count=shBetween(shDate,SH_ORIGIN_DATE);

    if(count==0)

        return MI_ORIGIN_DATE;

    else

   return plusDay(MI_ORIGIN_DATE,count);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static String shSysDate()

  {

      return miladiToShamsi_persiancoders_com(new Date(System.currentTimeMillis()));

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int dayOfWeek(Date miDate)

  {

    int N=0;

    GregorianCalendar gc=new GregorianCalendar();

    gc.setTime(miDate);

    N=gc.get(Calendar.DAY_OF_WEEK);

    if(N==7)

     return 1;

    else

     return N+1;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static int dayOfWeek(String shDate)

  {

    int count=shBetween(shDate,SH_ORIGIN_DATE);

    int m_day;

    if(count==0)

   return ORIGIN_WEEK_DAY;

    else

    {

     m_day=Math.abs(count)%7;

     if(count<0)

      switch( m_day){

                  case 0:

         return DOSHANBEH;

                  case 1:

         return YEKSHANBEH;

                  case 2:

         return SHANBEH;

                  case 3:

         return JOMEH;

                  case 4:

         return PANJSHANBEH;

                  case 5:

         return CHAHARSHANBEH;

                  case 6:

         return SESHANBEH;

                }

     else

      switch( m_day){

                  case 0:

         return DOSHANBEH;

                  case 1:

         return SESHANBEH;

                  case 2:

         return CHAHARSHANBEH;

                  case 3:

         return PANJSHANBEH;

                  case 4:

         return JOMEH;

                  case 5:

         return SHANBEH;

                  case 6:

         return YEKSHANBEH;

                }

        }

        return -1;

    }

}[/shcode][/align]

[align=justify][shcode=java]package Date;

import java.text.DecimalFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

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(int year, int month, int date,int hrs, int min, int sec){

    set(year,month,date,hrs,min,sec);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(int year, int month, int date,int hrs, int min){

    set(year,month,date,hrs,min);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(String shDate) {

    set(shDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(String shDate,long timeMillis) {

    set(shDate,timeMillis);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(String shDate ,int hrs, int min, int sec ) {

    set(shDate,hrs,min,sec);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(String shDate ,int hrs, int min) {

    set(shDate,hrs,min);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(Date miDate) {

    set(miDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(Date miDate,long timeMillis) {

     set(miDate,timeMillis);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(Date miDate ,int hrs, int min, int sec ) {

    set(miDate,hrs,min,sec);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public FDate(Date miDate ,int hrs, int min) {

    set(miDate,hrs,min);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  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 Date get(){

    return internalDate;

  }

 //+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(long millis) {

    internalDate=new Date(millis);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(){

    internalDate=new Date();

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(int year, int month, int date) {

    internalDate=ShamsiCalendar.shamsiToMiladi_persiancoders(composite(year, month, date));

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(int year, int month, int date,int hrs, int min, int sec){

    internalDate=ShamsiCalendar.shamsiToMiladi_persiancoders(composite(year, month, date));

    setField(Calendar.HOUR_OF_DAY,hrs);

    setField(Calendar.MINUTE,min);

    setField(Calendar.SECOND,sec);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(int year, int month, int date,int hrs, int min){

    internalDate=ShamsiCalendar.shamsiToMiladi_persiancoders(composite(year, month, date));

    setField(Calendar.HOUR_OF_DAY,hrs);

    setField(Calendar.MINUTE,min);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(String shDate) {

    internalDate=ShamsiCalendar.shamsiToMiladi_persiancoders(shDate);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(String shDate,long timeMillis) {

    internalDate=ShamsiCalendar.shamsiToMiladi_persiancoders(shDate);

    internalDate= new Date(internalDate.getTime()+timeMillis);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(String shDate ,int hrs, int min, int sec ) {

    internalDate=ShamsiCalendar.shamsiToMiladi_persiancoders(shDate);

    setField(Calendar.HOUR_OF_DAY,hrs);

    setField(Calendar.MINUTE,min);

    setField(Calendar.SECOND,sec);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(String shDate ,int hrs, int min) {

    internalDate=ShamsiCalendar.shamsiToMiladi_persiancoders(shDate);

    setField(Calendar.HOUR_OF_DAY,hrs);

    setField(Calendar.MINUTE,min);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(Date miDate) {

    internalDate=miDate;

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(Date miDate,long timeMillis) {

    internalDate=new Date(miDate.getTime()+timeMillis);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(Date miDate ,int hrs, int min, int sec ) {

    internalDate=miDate;

    setField(Calendar.HOUR_OF_DAY,hrs);

    setField(Calendar.MINUTE,min);

    setField(Calendar.SECOND,sec);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void set(Date miDate ,int hrs, int min) {

    internalDate=miDate;

    setField(Calendar.HOUR_OF_DAY,hrs);

    setField(Calendar.MINUTE,min);

    internalShamsiDate=ShamsiCalendar.miladiToShamsi_persiancoders_com(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_persiancoders_com(internalDate));

  }

  public int getMonth(){

    return ShamsiCalendar.getMonth(ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate));

  }

  public int getDate(){

    return ShamsiCalendar.getDate(ShamsiCalendar.miladiToShamsi_persiancoders_com(internalDate));

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public int dayOfWeek(){

    return getField(Calendar.DAY_OF_WEEK);

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public Date toMiladi(){

    return internalDate;

  }

  public String toString(){

    return internalShamsiDate;

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  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 boolean equals(Date when){

    return internalDate.equals(when);

  }

  public boolean equals(FDate when){

    return internalDate.equals(when.get());

  }

  public int compareTo(Date when){

    return internalDate.compareTo(when);

  }

  public int compareTo(FDate when){

    return internalDate.compareTo(when.get());

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public long getMillis(){

   return internalDate.getTime();

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  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_persiancoders(internalShamsiDate);

  }

  public void prevMonth(){

    internalShamsiDate=ShamsiCalendar.prevMonth(internalShamsiDate);

    set(ShamsiCalendar.shamsiToMiladi_persiancoders(internalShamsiDate),getHour(),getMinute(),getSecond());

  }

  public void nextYear(){

    internalShamsiDate=ShamsiCalendar.nextYear(internalShamsiDate);

    internalDate=ShamsiCalendar.shamsiToMiladi_persiancoders(internalShamsiDate);

  }

  public void prevYear(){

    internalShamsiDate=ShamsiCalendar.prevYear(internalShamsiDate);

    set(ShamsiCalendar.shamsiToMiladi_persiancoders(internalShamsiDate),getHour(),getMinute(),getSecond());

  }

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//|

//|

//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public void plusDay(int dayCount){

    internalShamsiDate=ShamsiCalendar.plusDay(internalShamsiDate,dayCount);

    set(ShamsiCalendar.shamsiToMiladi_persiancoders(internalShamsiDate),getHour(),getMinute(),getSecond());

  }

  public void minusDay(int dayCount){

    internalShamsiDate=ShamsiCalendar.minusDay(internalShamsiDate,dayCount);

    set(ShamsiCalendar.shamsiToMiladi_persiancoders(internalShamsiDate),getHour(),getMinute(),getSecond());

  }

  public int minusDate(String shDate)

  {

    long m1 = getMillis();

    long m2 = (new FDate(shDate)).getMillis();

    long diff = m1-m2;

    if (diff<0)

      diff *= -1;

    return (int)ShamsiCalendar.millisToDay(diff);

  }

  public static int diffDate(String shDt1, String shDt2)

  {

    FDate fd = new FDate(shDt1);

    return fd.minusDate(shDt2);

  }

}[/shcode][/align]

[align=justify][shcode=java]package Date;

import java.text.ParsePosition;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.StringTokenizer;

public class ChangeDate

{

  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 getCompleteTimeString_persiancoders(curDate);

  }

  public static String getCompleteTimeString_persiancoders(FDate fdate)

  {

    StringBuffer b = new StringBuffer();

    b.append((fdate.getHour() < 10) ? "0" + (fdate.getHour()) :

               String.valueOf(fdate.getHour()));

    b.append(":");

    b.append((fdate.getMinute() < 10) ? "0" + (fdate.getMinute()) :

               String.valueOf(fdate.getMinute()));

    b.append(":");

    b.append((fdate.getSecond() < 10) ? "0" + (fdate.getSecond()) :

             String.valueOf(fdate.getSecond()));

    return b.toString();

  }

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

  }

  public static Date toDate(String formattedDate)

  {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

    ParsePosition pos = new ParsePosition(0);

    Date d = dateFormat.parse(formattedDate,pos);

    return d;

  }

  public static String invertDate(String fdate)

  {

    String yyyy = null;

    String mm = null;

    String dd = null;

    if (fdate==null || fdate.length()==0)

      return "";

    StringTokenizer strTokenizer = new StringTokenizer(fdate,"/");

    if (strTokenizer.hasMoreTokens())

    {

      yyyy = strTokenizer.nextToken();

      if (strTokenizer.hasMoreTokens())

      {

        mm = strTokenizer.nextToken();

        if (strTokenizer.hasMoreTokens())

        {

          dd = strTokenizer.nextToken();

          return dd+"/"+mm+"/"+yyyy;

        }

      }

    }

    return fdate;

  }

  public static String changeFarsiToMiladi(String farsiDate)

  {

    Date miladiDate = ShamsiCalendar.shamsiToMiladi_persiancoders(farsiDate);

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

    return dateFormat.format(miladiDate);

  }

  public static String changeMiladiToFarsi(String miladiDate)

  {

    return ShamsiCalendar.miladiToShamsi_persiancoders_com(toDate(miladiDate));

  }

  public static String getDayMounthYear(){

    return ShamsiCalendar.weekDayName(ShamsiCalendar.dayOfWeek(ChangeDate.getCurrentDate()))+ " "+

           ShamsiCalendar.monthDayName(ChangeDate.getCurrentDay())+

           ShamsiCalendar.monthName(ChangeDate.getCurrentMonth()) +" ماه "  +

           String.valueOf(ChangeDate.getCurrentYear());

  }

    public static String decreaseYear (String CurrDate , int cnt){

        String year = CurrDate.substring(0,4);

        int ny = Integer.decode(year) - cnt;

        return String.valueOf(ny) + CurrDate.substring(4);

    }

    public static String decreaseCurrentYear (int cnt){

        String cur = getCurrentDate();

        String year = cur.substring(0,4);

        int ny = Integer.decode(year) - cnt;

        return String.valueOf(ny) + cur.substring(4);

    }

    public static String increaseYear (String tavalodDate , int cnt){

        String year = tavalodDate.substring(0,4);

        int ny = Integer.decode(year) + cnt;

        return String.valueOf(ny) + tavalodDate.substring(4);

    }

    public static String increaseCurrentYear (int cnt){

        String cur = getCurrentDate();

        String year = cur.substring(0,4);

        int ny = Integer.decode(year) + cnt;

        return String.valueOf(ny) + cur.substring(4);

    }

    public static String getCurrentDateTimeString()

    {

      FDate curDate = new FDate(System.currentTimeMillis());

      return curDate.toString().concat(" ").concat(getCompleteTimeString_persiancoders(curDate));

    }

}[/shcode][/align]

[align=justify]حال در هرجای پروژه که نیاز داشته باشید ، می توانید تاریخ را تبدیل کرده یا اعمالی روی آن انجام دهید .

در ادامه مثال هایی برای بیش از ۱۵ تابع موجود در این کلاس ها بهمراه نتیجه ی اجرای آن ها ذکر خواهد شد .

شما نیز می توانید از توابع ShamsiCalendar و FDate که در مثال زیر موجود نیست ، استفاده نمایید.[/align]

[align=justify][shcode=java]import Date.ChangeDate;

import Date.FDate;

/**

 * Created by Hamed on 06/03/2015.

 */

public class Home {

    public static void main(String[] args) {

        System.out.println("Current Date :   "+ ChangeDate.getCurrentDate());

        System.out.println("Shamsi To Miladi :   "+ ChangeDate.changeFarsiToMiladi("1393/12/15"));

        System.out.println("Mildai To Shamsi :   "+ ChangeDate.changeMiladiToFarsi("2015/03/07"));

        System.out.println("kam kardane tedadi saal az tarikhe fe'li :   "+ ChangeDate.decreaseCurrentYear(3));

        System.out.println("kam kardane tedadi saal az tarikhe Morede Nazar :   "+ ChangeDate.decreaseYear("1390/12/15", 3));

        System.out.println("Afzayesh e saal :   "+ ChangeDate.increaseCurrentYear(2));

        System.out.println("Afzayesh e saal (tarikhe entekhabi) :   "+ ChangeDate.increaseYear("1390/12/15",2));

        System.out.println("Date and Time :   "+ ChangeDate.getCurrentDateTimeString());

        System.out.println("Time :   "+ ChangeDate.getCurrentTime());

        System.out.println("Rooze Jaari :   "+ ChangeDate.getCurrentDay());

        System.out.println("Maahe Jaari :   "+ ChangeDate.getCurrentMonth());

        System.out.println("Saale Jaari :   "+ ChangeDate.getCurrentYear());

        System.out.println("tarikhe shamsi be horoof :   "+ ChangeDate.getDayMounthYear());

  // Ba vorood be getDayMounthYear mitavanid dastoorate daryaafte naam rooz o maah ra moshahede konid

        System.out.println("Invert :   "+ ChangeDate.invertDate("1393/12/15"));

        FDate fdate = new FDate(System.currentTimeMillis());

        fdate.minusDay(5);

        System.out.println("Minus5 day :   "+ fdate);

        fdate.plusDay(8);

        System.out.println("Plus 8 day :   "+ fdate);

    }

}

[/shcode][/align]

[align=justify]نتیجه ی اجرای کد بالا ، بصورت زیر خواهد بود :[/align]

[align=justify][shcode=java]Current Date :   1393/12/15

Shamsi To Miladi :   2015/03/06

Mildai To Shamsi :   1393/12/16

kam kardane tedadi saal az tarikhe fe'li :   1390/12/15

kam kardane tedadi saal az tarikhe Morede Nazar :   1387/12/15

Afzayesh e saal :   1395/12/15

Afzayesh e saal (tarikhe entekhabi) :   1392/12/15

Date and Time :   1393/12/15 19:59:17

Time :   19:59:17

Rooze Jaari :   15

Maahe Jaari :   12

Saale Jaari :   1393

tarikhe shamsi be horoof :   جمعه پانزدهم اسفند ماه 1393

Invert :   15/12/1393

Minus5 day :   1393/12/10

Plus 8 day :   1393/12/18[/shcode][/align]

[align=justify]لطفا در صورت استفاده از کلاس ها در برنامه های رایگان یا سایت های دیگر نام سایت بعنوان منبع را ذکر کنید.[/align]

به اشتراک گذاری این پست


لینک به ارسال
به اشتراک گذاری در سایت های دیگر

برای ارسال نظر یک حساب کاربری ایجاد کنید یا وارد حساب خود شوید

برای اینکه بتوانید نظر ارسال کنید نیاز دارید که کاربر سایت شوید

ایجاد یک حساب کاربری

برای حساب کاربری جدید در انجمن ما ثبت نام کنید. عضویت خیلی ساده است !


ثبت نام یک حساب کاربری جدید

ورود به حساب کاربری

دارای حساب کاربری هستید؟ از اینجا وارد شوید


ورود به حساب کاربری