رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
کلاس JDate (تاریخ شمسی) برای اندروید
#1
مدتی بود که دوستان توی تلگرام و پیام خصوصی در انجمن و... ازم میخواستن یه کلاس برای کار با تاریخ شمسی معرفی کنم. اکثر کتابخانه‌های موجود یا سنگین بودن و یا دقت کافی نداشتن. این‌شد که تصمیم‌گرفتم یک کلاس خیلی کوچک و جمع‌وجور برای تبدیل تاریخ میلادی به شمسی بنویسم. این کد رو توی فایل JDate.java توی پروژه‌ی خودتون ذخیره کنین (اگه لازم بود، اسم package رو متناسب با پروژه‌ی خودتون ویرایش کنین) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package ir.ncis.shamsidate;
 
import java.util.Calendar;
import java.util.Date;
 
/**
* Jalali Date class
* @author Mohammad Mostafa Shahreki (mmshfe@gmail.com)
*/
public class JDate {
   public String weekDay = "";
   public String monthName = "";
 
   public int day;
   public int month;
   public int year;
 
   /**
    * Default constructor converts today to Jalali
    */
   public JDate() {
       calculate(new Date());
   }
 
   /**
    * Constructor with java.util.Date instance
    * @param GregorianDate The desired Gregorian date to convert to Jalali
    */
   public JDate(Date GregorianDate) {
       calculate(GregorianDate);
   }
 
   /**
    * Constructor with year, month, day parameters
    * @param GregorianYear The year
    * @param GregorianMonth The month
    * @param GregorianDay The day
    */
   public JDate(int GregorianYear, int GregorianMonth, int GregorianDay) {
       calculate(GregorianYear, GregorianMonth, GregorianDay);
   }
 
   /**
    * Calculate the Jalali date of Gregorian year, month, day combination
    * @param GregorianYear The year
    * @param GregorianMonth The month
    * @param GregorianDay The day
    */
   public void calculate(int GregorianYear, int GregorianMonth, int GregorianDay) {
       Calendar calendar = Calendar.getInstance();
       calendar.set(GregorianYear, GregorianMonth, GregorianDay);
       calculate(calendar.getTime());
   }
 
   /**
    * Calculate the Jalali date from Gregorian java.util.Date instance
    * @param GregorianDate The desired Gregorian date to convert to Jalali
    */
   public void calculate(Date GregorianDate) {
       int lastDays;
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(GregorianDate);
       int gregorianYear = calendar.get(Calendar.YEAR);
       int gregorianMonth = calendar.get(Calendar.MONTH) + 1;
       int gregorianDate = calendar.get(Calendar.DATE);
       int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
       int[] startDayOnMonths1 = new int[]{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
       int[] startDayOfMonths2 = new int[]{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
       String[] weekDays = new String[]{"یکشنبه", "دوشنبه", "سه‌شنبه", "چهارشنبه", "پنج‌شنبه", "جمعه", "شنبه"};
       String[] monthNames = new String[]{"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند"};
       if ((gregorianYear % 4) != 0) {
           day = startDayOnMonths1[gregorianMonth - 1] + gregorianDate;
           if (day > 79) {
               day = day - 79;
               if (day <= 186) {
                   switch (day % 31) {
                       case 0:
                           month = day / 31;
                           day = 31;
                           break;
                       default:
                           month = (day / 31) + 1;
                           day = (day % 31);
                           break;
                   }
                   year = gregorianYear - 621;
               } else {
                   day = day - 186;
                   switch (day % 30) {
                       case 0:
                           month = (day / 30) + 6;
                           day = 30;
                           break;
                       default:
                           month = (day / 30) + 7;
                           day = (day % 30);
                           break;
                   }
                   year = gregorianYear - 621;
               }
           } else {
               if ((gregorianYear > 1996) && (gregorianYear % 4) == 1) {
                   lastDays = 11;
               } else {
                   lastDays = 10;
               }
               day = day + lastDays;
 
               switch (day % 30) {
                   case 0:
                       month = (day / 30) + 9;
                       day = 30;
                       break;
                   default:
                       month = (day / 30) + 10;
                       day = (day % 30);
                       break;
               }
               year = gregorianYear - 622;
           }
       } else {
           day = startDayOfMonths2[gregorianMonth - 1] + gregorianDate;
           if (gregorianYear >= 1996) {
               lastDays = 79;
           } else {
               lastDays = 80;
           }
           if (day > lastDays) {
               day = day - lastDays;
               if (day <= 186) {
                   switch (day % 31) {
                       case 0:
                           month = (day / 31);
                           day = 31;
                           break;
                       default:
                           month = (day / 31) + 1;
                           day = (day % 31);
                           break;
                   }
                   year = gregorianYear - 621;
               } else {
                   day = day - 186;
 
                   switch (day % 30) {
                       case 0:
                           month = (day / 30) + 6;
                           day = 30;
                           break;
                       default:
                           month = (day / 30) + 7;
                           day = (day % 30);
                           break;
                   }
                   year = gregorianYear - 621;
               }
           } else {
               day = day + 10;
               switch (day % 30) {
                   case 0:
                       month = (day / 30) + 9;
                       day = 30;
                       break;
                   default:
                       month = (day / 30) + 10;
                       day = (day % 30);
                       break;
               }
               year = gregorianYear - 622;
           }
       }
       this.monthName = monthNames[month - 1];
       this.weekDay = weekDays[weekDay - 1];
   }
}

مثالی از نحوه‌ی استفاده از این کلاس:
1
2
3
4
5
6
7
8
JDate jDate = new JDate();
TextView textView = (TextView) findViewById(R.id.txtMessage);
String message = "";
message += jDate.weekDay + " " + jDate.day + " " + jDate.monthName + " (" + jDate.month + ") سال " + jDate.year + " هجری شمسی";
message += "n";
jDate.calculate(2015, 1, 1);
message += jDate.weekDay + " " + jDate.day + " " + jDate.monthName + " (" + jDate.month + ") سال " + jDate.year + " هجری شمسی";
textView.setText(message);

مثالی از خروجی این کد توی تصویر ضمیمه نشون داده شده. البته برای دیدنش باید توی انجمن لاگین کرده باشین.
[عکس: attachment.php?aid=577]


فایل‌های پیوست تصاویر بندانگشتی
   
پاسخ
تشکر شده توسط: mazda




کاربران در حال بازدید این موضوع: 1 مهمان