diff --git a/app/src/main/java/com/ali/universaldatetools/MainActivity.java b/app/src/main/java/com/ali/universaldatetools/MainActivity.java index a7faff5..286f992 100755 --- a/app/src/main/java/com/ali/universaldatetools/MainActivity.java +++ b/app/src/main/java/com/ali/universaldatetools/MainActivity.java @@ -26,7 +26,7 @@ protected void onCreate(Bundle savedInstanceState) { Button getTime = findViewById(R.id.btn_get_time); TextView timeView = findViewById(R.id.text_time_show); - a.ShowDatePicker(getSupportFragmentManager(), Calendar.Hijri); + a.ShowDatePicker(getSupportFragmentManager(), Calendar.Jalali); a.setOnDateSelected((dateSystem, unixTime) -> { timeView.setText("unix time is: " + unixTime); timeView.append("\ndate is: " + dateSystem); diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateSystem.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateSystem.java index fb59d91..a8a81eb 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateSystem.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateSystem.java @@ -6,70 +6,96 @@ import android.util.Log; import com.ali.uneversaldatetools.model.DateModel; -import com.ali.uneversaldatetools.tools.DateTools; +import com.ali.uneversaldatetools.tools.UnixTimeTools; import java.util.Date; import java.util.Objects; +import java.util.TimeZone; /** * Created by ali on 9/5/18. */ -//this is struct public class DateSystem implements IDate { private Calendar Calendar; private DateModel Date; private IDate Date_SD; - public DateSystem(DateModel date, Calendar calendar) { + public DateSystem(int year, int month, int day, Calendar calendar) { Calendar = calendar; - if (date.month == 0) throw new IllegalArgumentException("month cant be 0"); + Log.d("month: ", String.valueOf(month)); + if (month > 12) month -= 12; + switch (Calendar) { - case Jalali: { - Date_SD = new JalaliDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone()); + + case Jalali: + Date_SD = new JalaliDateTime(year, month, day); break; - } - case Gregorian: { - Date_SD = new GregorianDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone()); + + case Gregorian: + Date_SD = new GregorianDateTime(year, month, day); break; - } - case Hijri: { - Date_SD = new HijriDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone()); + + case Hijri: + Date_SD = new HijriDateTime(year, month, day); break; - } - default: { + + default: throw new RuntimeException("Invalid Calendar Type!"); - } - } - Date = date; + } + Date = Date_SD.getDate(); } - public DateSystem(int year, int month, int day, Calendar calendar) { + public DateSystem(int year, int month, int day, int hour, int min, int sec, TimeZone timeZone, Calendar calendar) { Calendar = calendar; Log.d("month: ", String.valueOf(month)); if (month > 12) month -= 12; - else if (month < 1) month += 12; switch (Calendar) { - case Jalali: { - Date_SD = new JalaliDateTime(year, month, day); + + case Jalali: + Date_SD = new JalaliDateTime(year, month, day, hour, min, sec, timeZone); break; - } - case Gregorian: { - Date_SD = new GregorianDateTime(year, month, day); + + case Gregorian: + Date_SD = new GregorianDateTime(year, month, day, hour, min, sec, timeZone); break; - } - case Hijri: { - Date_SD = new HijriDateTime(year, month, day); + + case Hijri: + Date_SD = new HijriDateTime(year, month, day, hour, min, sec, timeZone); break; - } - default: { + + default: throw new RuntimeException("Invalid Calendar Type!"); - } + + } + Date = Date_SD.getDate(); + } + + public DateSystem(int unixTime, TimeZone timeZone, Calendar calendar) { + Calendar = calendar; + + switch (Calendar) { + + case Jalali: + Date_SD = new JalaliDateTime(unixTime, timeZone); + break; + + case Gregorian: + Date_SD = new GregorianDateTime(unixTime, timeZone); + break; + + case Hijri: + Date_SD = new HijriDateTime(unixTime, timeZone); + break; + + default: + throw new RuntimeException("Invalid Calendar Type!"); + } Date = Date_SD.getDate(); } @@ -189,20 +215,23 @@ public DateModel AddDays(int days) { return Date_SD.AddDays(days); } - public static DateSystem Now(Calendar calendar) { - return new DateSystem(DateTools.getCurrentDate(), calendar); + public static DateSystem Now(TimeZone timeZone, Calendar calendar) { + return new DateSystem(UnixTimeTools.getCurrentUnixTime(), timeZone, calendar); } public static DateSystem Parse(String date, Calendar calendar) { switch (calendar) { case Jalali: { - return new DateSystem(JalaliDateTime.Parse(date).getDate(), calendar); + DateModel d = JalaliDateTime.Parse(date).getDate(); + return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar); } case Gregorian: { - return new DateSystem(GregorianDateTime.Parse(date).getDate(), calendar); + DateModel d = GregorianDateTime.Parse(date).getDate(); + return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar); } case Hijri: { - return new DateSystem(HijriDateTime.Parse(date).getDate(), calendar); + DateModel d = HijriDateTime.Parse(date).getDate(); + return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar); } default: { throw new RuntimeException("Invalid Calendar Type!"); @@ -213,13 +242,16 @@ public static DateSystem Parse(String date, Calendar calendar) { public static DateSystem Parse(String yearMonth, int day, Calendar calendar) { switch (calendar) { case Jalali: { - return new DateSystem(JalaliDateTime.Parse(yearMonth, day).getDate(), calendar); + DateModel d = JalaliDateTime.Parse(yearMonth, day).getDate(); + return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar); } case Gregorian: { - return new DateSystem(GregorianDateTime.Parse(yearMonth, day).getDate(), calendar); + DateModel d = GregorianDateTime.Parse(yearMonth, day).getDate(); + return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar); } case Hijri: { - return new DateSystem(HijriDateTime.Parse(yearMonth, day).getDate(), calendar); + DateModel d = HijriDateTime.Parse(yearMonth, day).getDate(); + return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar); } default: { throw new RuntimeException("Invalid Calendar Type!"); diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/GregorianDateTime.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/GregorianDateTime.java index 714c6b2..d0ee293 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/GregorianDateTime.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/GregorianDateTime.java @@ -3,7 +3,7 @@ import android.support.annotation.NonNull; import com.ali.uneversaldatetools.model.DateModel; -import com.ali.uneversaldatetools.tools.DateTools; +import com.ali.uneversaldatetools.tools.UnixTimeTools; import java.util.TimeZone; @@ -21,7 +21,7 @@ public class GregorianDateTime implements IDate, Comparable { private int Sec; private TimeZone TimeZone; - public static final int[] DaysInMonth = { + static final int[] DaysInMonth = { 0, 31, 29, @@ -105,8 +105,7 @@ public static GregorianDateTime ParseYearMonth(String s) { } public static GregorianDateTime Now() { - DateModel crnt = DateTools.getCurrentDate(); - return new GregorianDateTime(crnt.year, crnt.month, crnt.day, crnt.hour, crnt.min, crnt.sec, TimeZoneHelper.getSystemTimeZone()); + return new GregorianDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone()); } diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/HijriDateTime.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/HijriDateTime.java index 57f86d3..58390d4 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/HijriDateTime.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/HijriDateTime.java @@ -3,7 +3,7 @@ import android.support.annotation.NonNull; import com.ali.uneversaldatetools.model.DateModel; -import com.ali.uneversaldatetools.tools.DateTools; +import com.ali.uneversaldatetools.tools.UnixTimeTools; import java.util.TimeZone; @@ -21,7 +21,7 @@ public class HijriDateTime implements IDate, Comparable { private int Sec; private TimeZone TimeZone; - public static final int[] DaysInMonth = { + static final int[] DaysInMonth = { 0, 30, 29, @@ -105,8 +105,7 @@ public static HijriDateTime ParseYearMonth(String yearMonth) { } public static HijriDateTime Now() { - DateModel d = DateTools.getCurrentDate(); - return new HijriDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone()); + return new HijriDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone()); } public DateModel getDate() { diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/JalaliDateTime.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/JalaliDateTime.java index a719ea7..10b4bb4 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/JalaliDateTime.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/JalaliDateTime.java @@ -3,7 +3,7 @@ import android.support.annotation.NonNull; import com.ali.uneversaldatetools.model.DateModel; -import com.ali.uneversaldatetools.tools.DateTools; +import com.ali.uneversaldatetools.tools.UnixTimeTools; import java.util.TimeZone; @@ -21,7 +21,7 @@ public class JalaliDateTime implements IDate, Comparable { private int Sec; private TimeZone TimeZone; - public static final int[] DaysInMonth = { + static final int[] DaysInMonth = { 0, 31, 31, @@ -110,9 +110,7 @@ public static JalaliDateTime ParseYearMonth(String s) { } public static JalaliDateTime Now() { - DateModel crnt = DateTools.getCurrentDate(); - DateConverter.GregorianToJalali(crnt.year, crnt.month, crnt.day); - return new JalaliDateTime(crnt.year, crnt.month, crnt.day, crnt.hour, crnt.min, crnt.sec, TimeZoneHelper.getSystemTimeZone()); + return new JalaliDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone()); } public DateModel getDate() { diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/datePicker/UDatePicker.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/datePicker/UDatePicker.java index c504993..10fb450 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/datePicker/UDatePicker.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/datePicker/UDatePicker.java @@ -21,11 +21,12 @@ import com.ali.uneversaldatetools.R; import com.ali.uneversaldatetools.date.Calendar; import com.ali.uneversaldatetools.date.DateSystem; +import com.ali.uneversaldatetools.date.TimeZoneHelper; import com.ali.uneversaldatetools.model.Month; import com.ali.uneversaldatetools.tools.Convert; -import com.ali.uneversaldatetools.tools.DateTools; import com.ali.uneversaldatetools.tools.ExpandAndCollapseAnimation; import com.ali.uneversaldatetools.tools.StringGenerator; +import com.ali.uneversaldatetools.tools.UnixTimeTools; import java.util.ArrayList; import java.util.List; @@ -88,7 +89,7 @@ public void ShowDatePicker(FragmentManager appCompatActivity, @NonNull DateSyste } public void ShowDatePicker(FragmentManager appCompatActivity, Calendar calendar) { - DateSystem defaultDate = new DateSystem(DateTools.getCurrentDate(), calendar); // now + DateSystem defaultDate = new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), calendar); // now ShowDatePicker(appCompatActivity, defaultDate); } @@ -378,12 +379,12 @@ public int selectedDayProvider() { @Override public int currentMonthProvider() { - return new DateSystem(DateTools.getCurrentDate(), mDateSystem.getCalendar()).getMonth(); + return new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), mDateSystem.getCalendar()).getMonth(); } @Override public int currentDayProvider() { - return new DateSystem(DateTools.getCurrentDate(), mDateSystem.getCalendar()).getDay(); + return new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), mDateSystem.getCalendar()).getDay(); } diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/DateTools.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/DateTools.java deleted file mode 100755 index 3bb410c..0000000 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/DateTools.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.ali.uneversaldatetools.tools; - -import com.ali.uneversaldatetools.model.DateModel; - -import java.util.Date; - -/** - * Created by ali on 9/11/18. - */ - -public class DateTools { - - public static DateModel getCurrentDate() { - Date date = new Date(); - date.setYear(date.getYear() + 1900); - date.setMonth(date.getMonth() + 1); // change index to 1 - return new DateModel(date.getYear(), date.getMonth(), date.getDate()); - } - - public static int getCurrentUnixTime() { - return (int) (System.currentTimeMillis() / 1000L); - } -} diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/UnixTimeTools.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/UnixTimeTools.java new file mode 100755 index 0000000..b2ba7a9 --- /dev/null +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/UnixTimeTools.java @@ -0,0 +1,12 @@ +package com.ali.uneversaldatetools.tools; + +/** + * Created by ali on 9/11/18. + */ + +public class UnixTimeTools { + + public static int getCurrentUnixTime() { + return (int) (System.currentTimeMillis() / 1000L); //toSec + } +} diff --git a/uneversaldatetools/src/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java b/uneversaldatetools/src/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java index 2523441..c441ab4 100755 --- a/uneversaldatetools/src/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java +++ b/uneversaldatetools/src/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java @@ -6,7 +6,7 @@ import com.ali.uneversaldatetools.date.JalaliDateTime; import com.ali.uneversaldatetools.date.TimeZoneHelper; import com.ali.uneversaldatetools.model.DateModel; -import com.ali.uneversaldatetools.tools.DateTools; +import com.ali.uneversaldatetools.tools.UnixTimeTools; import org.junit.Assert; import org.junit.Test; @@ -37,7 +37,7 @@ public void ToStringTest() { @Test public void UnixTest() { - int unixTime = DateTools.getCurrentUnixTime(); + int unixTime = UnixTimeTools.getCurrentUnixTime(); Log(unixTime); DateModel dateModel = DateConverter.UnixToJalali(unixTime);