Skip to content

Commit 3a8d9a0

Browse files
committed
1.bug fix : current jalali date goes to (2 dey 2018)
2.add timezone and unixtime to SystemDate and remove date model 3.get system date changed to get unixtime every where
1 parent 40fc62b commit 3a8d9a0

File tree

9 files changed

+99
-81
lines changed

9 files changed

+99
-81
lines changed

app/src/main/java/com/ali/universaldatetools/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
2626
Button getTime = findViewById(R.id.btn_get_time);
2727
TextView timeView = findViewById(R.id.text_time_show);
2828

29-
a.ShowDatePicker(getSupportFragmentManager(), Calendar.Hijri);
29+
a.ShowDatePicker(getSupportFragmentManager(), Calendar.Jalali);
3030
a.setOnDateSelected((dateSystem, unixTime) -> {
3131
timeView.setText("unix time is: " + unixTime);
3232
timeView.append("\ndate is: " + dateSystem);

uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateSystem.java

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,96 @@
66
import android.util.Log;
77

88
import com.ali.uneversaldatetools.model.DateModel;
9-
import com.ali.uneversaldatetools.tools.DateTools;
9+
import com.ali.uneversaldatetools.tools.UnixTimeTools;
1010

1111
import java.util.Date;
1212
import java.util.Objects;
13+
import java.util.TimeZone;
1314

1415
/**
1516
* Created by ali on 9/5/18.
1617
*/
1718

18-
//this is struct
1919
public class DateSystem implements IDate {
2020

2121
private Calendar Calendar;
2222
private DateModel Date;
2323
private IDate Date_SD;
2424

25-
public DateSystem(DateModel date, Calendar calendar) {
25+
public DateSystem(int year, int month, int day, Calendar calendar) {
2626
Calendar = calendar;
2727

28-
if (date.month == 0) throw new IllegalArgumentException("month cant be 0");
28+
Log.d("month: ", String.valueOf(month));
29+
if (month > 12) month -= 12;
30+
2931
switch (Calendar) {
30-
case Jalali: {
31-
Date_SD = new JalaliDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());
32+
33+
case Jalali:
34+
Date_SD = new JalaliDateTime(year, month, day);
3235
break;
33-
}
34-
case Gregorian: {
35-
Date_SD = new GregorianDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());
36+
37+
case Gregorian:
38+
Date_SD = new GregorianDateTime(year, month, day);
3639
break;
37-
}
38-
case Hijri: {
39-
Date_SD = new HijriDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());
40+
41+
case Hijri:
42+
Date_SD = new HijriDateTime(year, month, day);
4043
break;
41-
}
42-
default: {
44+
45+
default:
4346
throw new RuntimeException("Invalid Calendar Type!");
44-
}
45-
}
4647

47-
Date = date;
48+
}
49+
Date = Date_SD.getDate();
4850
}
4951

50-
public DateSystem(int year, int month, int day, Calendar calendar) {
52+
public DateSystem(int year, int month, int day, int hour, int min, int sec, TimeZone timeZone, Calendar calendar) {
5153
Calendar = calendar;
5254

5355
Log.d("month: ", String.valueOf(month));
5456
if (month > 12) month -= 12;
55-
else if (month < 1) month += 12;
5657

5758
switch (Calendar) {
58-
case Jalali: {
59-
Date_SD = new JalaliDateTime(year, month, day);
59+
60+
case Jalali:
61+
Date_SD = new JalaliDateTime(year, month, day, hour, min, sec, timeZone);
6062
break;
61-
}
62-
case Gregorian: {
63-
Date_SD = new GregorianDateTime(year, month, day);
63+
64+
case Gregorian:
65+
Date_SD = new GregorianDateTime(year, month, day, hour, min, sec, timeZone);
6466
break;
65-
}
66-
case Hijri: {
67-
Date_SD = new HijriDateTime(year, month, day);
67+
68+
case Hijri:
69+
Date_SD = new HijriDateTime(year, month, day, hour, min, sec, timeZone);
6870
break;
69-
}
70-
default: {
71+
72+
default:
7173
throw new RuntimeException("Invalid Calendar Type!");
72-
}
74+
75+
}
76+
Date = Date_SD.getDate();
77+
}
78+
79+
public DateSystem(int unixTime, TimeZone timeZone, Calendar calendar) {
80+
Calendar = calendar;
81+
82+
switch (Calendar) {
83+
84+
case Jalali:
85+
Date_SD = new JalaliDateTime(unixTime, timeZone);
86+
break;
87+
88+
case Gregorian:
89+
Date_SD = new GregorianDateTime(unixTime, timeZone);
90+
break;
91+
92+
case Hijri:
93+
Date_SD = new HijriDateTime(unixTime, timeZone);
94+
break;
95+
96+
default:
97+
throw new RuntimeException("Invalid Calendar Type!");
98+
7399
}
74100
Date = Date_SD.getDate();
75101
}
@@ -189,20 +215,23 @@ public DateModel AddDays(int days) {
189215
return Date_SD.AddDays(days);
190216
}
191217

192-
public static DateSystem Now(Calendar calendar) {
193-
return new DateSystem(DateTools.getCurrentDate(), calendar);
218+
public static DateSystem Now(TimeZone timeZone, Calendar calendar) {
219+
return new DateSystem(UnixTimeTools.getCurrentUnixTime(), timeZone, calendar);
194220
}
195221

196222
public static DateSystem Parse(String date, Calendar calendar) {
197223
switch (calendar) {
198224
case Jalali: {
199-
return new DateSystem(JalaliDateTime.Parse(date).getDate(), calendar);
225+
DateModel d = JalaliDateTime.Parse(date).getDate();
226+
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
200227
}
201228
case Gregorian: {
202-
return new DateSystem(GregorianDateTime.Parse(date).getDate(), calendar);
229+
DateModel d = GregorianDateTime.Parse(date).getDate();
230+
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
203231
}
204232
case Hijri: {
205-
return new DateSystem(HijriDateTime.Parse(date).getDate(), calendar);
233+
DateModel d = HijriDateTime.Parse(date).getDate();
234+
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
206235
}
207236
default: {
208237
throw new RuntimeException("Invalid Calendar Type!");
@@ -213,13 +242,16 @@ public static DateSystem Parse(String date, Calendar calendar) {
213242
public static DateSystem Parse(String yearMonth, int day, Calendar calendar) {
214243
switch (calendar) {
215244
case Jalali: {
216-
return new DateSystem(JalaliDateTime.Parse(yearMonth, day).getDate(), calendar);
245+
DateModel d = JalaliDateTime.Parse(yearMonth, day).getDate();
246+
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
217247
}
218248
case Gregorian: {
219-
return new DateSystem(GregorianDateTime.Parse(yearMonth, day).getDate(), calendar);
249+
DateModel d = GregorianDateTime.Parse(yearMonth, day).getDate();
250+
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
220251
}
221252
case Hijri: {
222-
return new DateSystem(HijriDateTime.Parse(yearMonth, day).getDate(), calendar);
253+
DateModel d = HijriDateTime.Parse(yearMonth, day).getDate();
254+
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
223255
}
224256
default: {
225257
throw new RuntimeException("Invalid Calendar Type!");

uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/GregorianDateTime.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.support.annotation.NonNull;
44

55
import com.ali.uneversaldatetools.model.DateModel;
6-
import com.ali.uneversaldatetools.tools.DateTools;
6+
import com.ali.uneversaldatetools.tools.UnixTimeTools;
77

88
import java.util.TimeZone;
99

@@ -21,7 +21,7 @@ public class GregorianDateTime implements IDate, Comparable<GregorianDateTime> {
2121
private int Sec;
2222
private TimeZone TimeZone;
2323

24-
public static final int[] DaysInMonth = {
24+
static final int[] DaysInMonth = {
2525
0,
2626
31,
2727
29,
@@ -105,8 +105,7 @@ public static GregorianDateTime ParseYearMonth(String s) {
105105
}
106106

107107
public static GregorianDateTime Now() {
108-
DateModel crnt = DateTools.getCurrentDate();
109-
return new GregorianDateTime(crnt.year, crnt.month, crnt.day, crnt.hour, crnt.min, crnt.sec, TimeZoneHelper.getSystemTimeZone());
108+
return new GregorianDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone());
110109
}
111110

112111

uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/HijriDateTime.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.support.annotation.NonNull;
44

55
import com.ali.uneversaldatetools.model.DateModel;
6-
import com.ali.uneversaldatetools.tools.DateTools;
6+
import com.ali.uneversaldatetools.tools.UnixTimeTools;
77

88
import java.util.TimeZone;
99

@@ -21,7 +21,7 @@ public class HijriDateTime implements IDate, Comparable<HijriDateTime> {
2121
private int Sec;
2222
private TimeZone TimeZone;
2323

24-
public static final int[] DaysInMonth = {
24+
static final int[] DaysInMonth = {
2525
0,
2626
30,
2727
29,
@@ -105,8 +105,7 @@ public static HijriDateTime ParseYearMonth(String yearMonth) {
105105
}
106106

107107
public static HijriDateTime Now() {
108-
DateModel d = DateTools.getCurrentDate();
109-
return new HijriDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone());
108+
return new HijriDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone());
110109
}
111110

112111
public DateModel getDate() {

uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/JalaliDateTime.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.support.annotation.NonNull;
44

55
import com.ali.uneversaldatetools.model.DateModel;
6-
import com.ali.uneversaldatetools.tools.DateTools;
6+
import com.ali.uneversaldatetools.tools.UnixTimeTools;
77

88
import java.util.TimeZone;
99

@@ -21,7 +21,7 @@ public class JalaliDateTime implements IDate, Comparable<JalaliDateTime> {
2121
private int Sec;
2222
private TimeZone TimeZone;
2323

24-
public static final int[] DaysInMonth = {
24+
static final int[] DaysInMonth = {
2525
0,
2626
31,
2727
31,
@@ -110,9 +110,7 @@ public static JalaliDateTime ParseYearMonth(String s) {
110110
}
111111

112112
public static JalaliDateTime Now() {
113-
DateModel crnt = DateTools.getCurrentDate();
114-
DateConverter.GregorianToJalali(crnt.year, crnt.month, crnt.day);
115-
return new JalaliDateTime(crnt.year, crnt.month, crnt.day, crnt.hour, crnt.min, crnt.sec, TimeZoneHelper.getSystemTimeZone());
113+
return new JalaliDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone());
116114
}
117115

118116
public DateModel getDate() {

uneversaldatetools/src/main/java/com/ali/uneversaldatetools/datePicker/UDatePicker.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
import com.ali.uneversaldatetools.R;
2222
import com.ali.uneversaldatetools.date.Calendar;
2323
import com.ali.uneversaldatetools.date.DateSystem;
24+
import com.ali.uneversaldatetools.date.TimeZoneHelper;
2425
import com.ali.uneversaldatetools.model.Month;
2526
import com.ali.uneversaldatetools.tools.Convert;
26-
import com.ali.uneversaldatetools.tools.DateTools;
2727
import com.ali.uneversaldatetools.tools.ExpandAndCollapseAnimation;
2828
import com.ali.uneversaldatetools.tools.StringGenerator;
29+
import com.ali.uneversaldatetools.tools.UnixTimeTools;
2930

3031
import java.util.ArrayList;
3132
import java.util.List;
@@ -88,7 +89,7 @@ public void ShowDatePicker(FragmentManager appCompatActivity, @NonNull DateSyste
8889
}
8990

9091
public void ShowDatePicker(FragmentManager appCompatActivity, Calendar calendar) {
91-
DateSystem defaultDate = new DateSystem(DateTools.getCurrentDate(), calendar); // now
92+
DateSystem defaultDate = new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), calendar); // now
9293
ShowDatePicker(appCompatActivity, defaultDate);
9394
}
9495

@@ -378,12 +379,12 @@ public int selectedDayProvider() {
378379

379380
@Override
380381
public int currentMonthProvider() {
381-
return new DateSystem(DateTools.getCurrentDate(), mDateSystem.getCalendar()).getMonth();
382+
return new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), mDateSystem.getCalendar()).getMonth();
382383
}
383384

384385
@Override
385386
public int currentDayProvider() {
386-
return new DateSystem(DateTools.getCurrentDate(), mDateSystem.getCalendar()).getDay();
387+
return new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), mDateSystem.getCalendar()).getDay();
387388
}
388389

389390

uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/DateTools.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ali.uneversaldatetools.tools;
2+
3+
/**
4+
* Created by ali on 9/11/18.
5+
*/
6+
7+
public class UnixTimeTools {
8+
9+
public static int getCurrentUnixTime() {
10+
return (int) (System.currentTimeMillis() / 1000L); //toSec
11+
}
12+
}

uneversaldatetools/src/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.ali.uneversaldatetools.date.JalaliDateTime;
77
import com.ali.uneversaldatetools.date.TimeZoneHelper;
88
import com.ali.uneversaldatetools.model.DateModel;
9-
import com.ali.uneversaldatetools.tools.DateTools;
9+
import com.ali.uneversaldatetools.tools.UnixTimeTools;
1010

1111
import org.junit.Assert;
1212
import org.junit.Test;
@@ -37,7 +37,7 @@ public void ToStringTest() {
3737

3838
@Test
3939
public void UnixTest() {
40-
int unixTime = DateTools.getCurrentUnixTime();
40+
int unixTime = UnixTimeTools.getCurrentUnixTime();
4141

4242
Log(unixTime);
4343
DateModel dateModel = DateConverter.UnixToJalali(unixTime);

0 commit comments

Comments
 (0)