diff --git a/app/src/main/java/com/ali/universaldatetools/MainActivity.java b/app/src/main/java/com/ali/universaldatetools/MainActivity.java index 286f992..a7faff5 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.Jalali); + a.ShowDatePicker(getSupportFragmentManager(), Calendar.Hijri); 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/DateConverter.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateConverter.java index 6b4afc6..c6bc199 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateConverter.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateConverter.java @@ -487,9 +487,9 @@ public static DateModel HijriToGregorian(int year, int month, int day) { return DaysToGregorian(HijriToDays(year, month, day) + 227014); } + //unix time - public static DateModel UnixToHijri(int sec, int timeZone) { - sec += timeZone; + public static DateModel UnixToHijri(int sec) { DateModel date = DaysToHijri((sec / 86400) + 719163 - 227014); date.sec = sec % 86400; date.hour = date.sec / 3600; @@ -499,12 +499,7 @@ public static DateModel UnixToHijri(int sec, int timeZone) { return date; } - public static DateModel UnixToHijri(int sec) { - return UnixToHijri(sec, 0); - } - - public static DateModel UnixToJalali(int sec, int timeZone) { - sec += timeZone; + public static DateModel UnixToJalali(int sec) { DateModel date = DaysToJalali((sec / 86400) + 719163 - 226895); date.sec = sec % 86400; date.hour = date.sec / 3600; @@ -514,12 +509,7 @@ public static DateModel UnixToJalali(int sec, int timeZone) { return date; } - public static DateModel UnixToJalali(int sec) { - return UnixToJalali(sec, 0); - } - - public static DateModel UnixToGregorian(int sec, int timeZone) { - sec += timeZone; + public static DateModel UnixToGregorian(int sec) { DateModel date = DaysToGregorian((sec / 86400) + 719163); date.sec = sec % 86400; date.hour = date.sec / 3600; @@ -529,9 +519,48 @@ public static DateModel UnixToGregorian(int sec, int timeZone) { return date; } - public static DateModel UnixToGregorian(int sec) { - return UnixToGregorian(sec, 0); + + public static int HijriToUnix(int year, int month, int day, int hour, int min, int sec) { + + int from = 719163; // const : DateConverter.GregorianToDays(1970, 1, 1) + + from -= 227014; // hijri and gregorian deference in days + + int days = HijriToDays(year, month, day); + + int lastDays = ((days - from) * 86400); + + int today = sec + (min * 60) + (hour * 3600); + + return today + lastDays; + + } + + public static int JalaliToUnix(int year, int month, int day, int hour, int min, int sec) { + + int from = 719163; // const : DateConverter.GregorianToDays(1970, 1, 1) + + from -= 226895; // jalali and gregorian deference in days + + int days = JalaliToDays(year, month, day); + + int lastDays = ((days - from) * 86400); + + int today = sec + (min * 60) + (hour * 3600); + + return today + lastDays; } - //todo to unix methods + public static int GregorianToUnix(int year, int month, int day, int hour, int min, int sec) { + + int from = 719163; // const -> DateConverter.GregorianToDays(1970, 1, 1) + + int days = GregorianToDays(year, month, day); + + int lastDays = ((days - from) * 86400); + + int today = sec + (min * 60) + (hour * 3600); + + return today + lastDays; + } } 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 36b207a..fb59d91 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateSystem.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateSystem.java @@ -28,15 +28,15 @@ public DateSystem(DateModel date, Calendar calendar) { if (date.month == 0) throw new IllegalArgumentException("month cant be 0"); switch (Calendar) { case Jalali: { - Date_SD = new JalaliDateTime(date); + Date_SD = new JalaliDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone()); break; } case Gregorian: { - Date_SD = new GregorianDateTime(date); + Date_SD = new GregorianDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone()); break; } case Hijri: { - Date_SD = new HijriDateTime(date); + Date_SD = new HijriDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone()); break; } default: { @@ -274,8 +274,7 @@ public int hashCode() { return (Calendar.getValue() * 397) ^ (Date_SD.hashCode()); } - public long getUnixTime() { - long from = DateConverter.GregorianToDays(1970, 1, 1); - return (getGregorianDateTime().getDays() - from) * 86400; + public int toUnixTime() { + return Date_SD.toUnixTime(); } } diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateValidation.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateValidation.java new file mode 100644 index 0000000..cd2ccea --- /dev/null +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DateValidation.java @@ -0,0 +1,82 @@ +package com.ali.uneversaldatetools.date; + +/** + * Created by ali on 10/2/18. + */ + +public class DateValidation { + + + public static void GregorianValidate(int year, int month, int day, int hour, int min, int sec) { + BaseValidate(year,month,hour, min, sec); + GregorianDayValidate(year, month, day); + } + + public static void JalaliValidate(int year, int month, int day, int hour, int min, int sec) { + BaseValidate(year,month,hour, min, sec); + JalaliDayValidate(year, month, day); + } + + public static void HijriValidate(int year, int month, int day, int hour, int min, int sec) { + BaseValidate(year,month,hour, min, sec); + HijriDayValidate(year, month, day); + } + + + private static void GregorianDayValidate(int year, int month, int day) { + + //day + if (DateConverter.IsGregorianLeap(year) & month == 2) { + if (day < 1 | day > GregorianDateTime.DaysInMonth[month] + 1) + NotValid("day",day); + } else { + if (day < 1 | day > GregorianDateTime.DaysInMonth[month]) + NotValid("day",day); + } + + } + + private static void JalaliDayValidate(int year, int month, int day) { + + //day + if (DateConverter.IsGregorianLeap(year) & month == 12) { + if (day < 1 | day > JalaliDateTime.DaysInMonth[month] + 1) + NotValid("day",day); + } else { + if (day < 1 | day > JalaliDateTime.DaysInMonth[month]) + NotValid("day",day); + } + + } + + private static void HijriDayValidate(int year, int month, int day) { + //day + if (DateConverter.IsGregorianLeap(year) & month == 12) { + if (day < 1 | day > HijriDateTime.DaysInMonth[month] + 1) + NotValid("day",day); + } else { + if (day < 1 | day > HijriDateTime.DaysInMonth[month]) + NotValid("day",day); + } + } + + + private static void BaseValidate(int year, int month, int hour, int min, int sec) { + + //year + if (year < 0) NotValid("year", year); + + //month + if (month < 1 | month > 12) NotValid("month", month); + + //time + if (hour > 23 | hour < 0) NotValid("hour", hour); + if (min > 59 | min < 0) NotValid("minute", min); + if (sec > 59 | sec < 0) NotValid("second", sec); + } + + + private static void NotValid(String what, int value) { + throw new IllegalArgumentException("invalid " + what + " : " + value); + } +} diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DefaultCalendar.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DefaultCalendar.java index f5b4a8c..c08968e 100644 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DefaultCalendar.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/DefaultCalendar.java @@ -6,13 +6,13 @@ public class DefaultCalendar { - private Calendar defaultCalendar; + private static Calendar mDefaultCalendar; - public Calendar getDefaultCalendar() { - return defaultCalendar; + public static Calendar getDefaultCalendar() { + return mDefaultCalendar; } - public void setDefaultCalendar(Calendar defaultCalendar) { - this.defaultCalendar = defaultCalendar; + public static void setDefaultCalendar(Calendar DefaultCalendar) { + mDefaultCalendar = DefaultCalendar; } } 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 057f45d..714c6b2 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/GregorianDateTime.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/GregorianDateTime.java @@ -5,6 +5,8 @@ import com.ali.uneversaldatetools.model.DateModel; import com.ali.uneversaldatetools.tools.DateTools; +import java.util.TimeZone; + /** * Created by ali on 9/5/18. */ @@ -17,8 +19,9 @@ public class GregorianDateTime implements IDate, Comparable { private int Hour; private int Min; private int Sec; + private TimeZone TimeZone; - private static final int[] DaysInMonth = { + public static final int[] DaysInMonth = { 0, 31, 29, @@ -34,53 +37,54 @@ public class GregorianDateTime implements IDate, Comparable { 31 }; - public static GregorianDateTime FromUnixTime(int unixTime) { - DateModel dateModel = DateConverter.UnixToGregorian(unixTime); - Validate(dateModel); - return new GregorianDateTime(dateModel); - } - - public GregorianDateTime(DateModel date) { - - Validate(date); - - Year = date.year; - Month = date.month; - Day = date.day; - Hour = date.hour; - Min = date.min; - Sec = date.sec; - } - public GregorianDateTime(int year, int month, int day) { - - Validate(new DateModel(year, month, day)); - + DateValidation.GregorianValidate(year, month, day, 0, 0, 0); Year = year; Month = month; Day = day; + TimeZone = TimeZoneHelper.getSystemTimeZone(); } - public GregorianDateTime(int year, int month, int day, int hour, int min, int sec) { - - Validate(new DateModel(year, month, day, hour, min, sec)); - - Year = year; - Month = month; - Day = day; + public GregorianDateTime(int year, int month, int day, int hour, int min, int sec, TimeZone timeZone) { + this(year, month, day); + DateValidation.GregorianValidate(year, month, day, hour, min, sec); Hour = hour; Min = min; Sec = sec; + TimeZone = timeZone; } public GregorianDateTime(int days) { DateModel sd = DateConverter.DaysToGregorian(days); - Validate(sd); Year = sd.year; Month = sd.month; Day = sd.day; + TimeZone = TimeZoneHelper.getSystemTimeZone(); } + public GregorianDateTime(int days, int hour, int min, int sec, TimeZone timeZone) { + this(days); + Hour = hour; + Min = min; + Sec = sec; + TimeZone = timeZone; + } + + public GregorianDateTime(int unixTimeSeconds, TimeZone timeZone) { + + int offset = TimeZoneHelper.ToSeconds(timeZone); + + DateModel dateModel = DateConverter.UnixToGregorian(unixTimeSeconds + offset); + Year = dateModel.year; + Month = dateModel.month; + Day = dateModel.day; + Hour = dateModel.hour; + Min = dateModel.min; + Sec = dateModel.sec; + TimeZone = timeZone; + } + + public static GregorianDateTime Parse(String s) { int y = Integer.valueOf(s.substring(0, 4)); int m = Integer.valueOf(s.substring(5, 7)); @@ -101,9 +105,11 @@ public static GregorianDateTime ParseYearMonth(String s) { } public static GregorianDateTime Now() { - return new GregorianDateTime(DateTools.getCurrentDate()); + DateModel crnt = DateTools.getCurrentDate(); + return new GregorianDateTime(crnt.year, crnt.month, crnt.day, crnt.hour, crnt.min, crnt.sec, TimeZoneHelper.getSystemTimeZone()); } + public DateModel getDate() { return new DateModel(Year, Month, Day); } @@ -185,7 +191,8 @@ public DateModel AddDays(int days) { } public JalaliDateTime getJalaliDateTime() { - return new JalaliDateTime(getDate()); + DateModel d = getDate(); + return new JalaliDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZone); } public GregorianDateTime getGregorianDateTime() { @@ -193,7 +200,8 @@ public GregorianDateTime getGregorianDateTime() { } public HijriDateTime getHijriDateTime() { - return new HijriDateTime(getDate()); + DateModel d = getDate(); + return new HijriDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZone); } public String getLetters() { @@ -248,15 +256,15 @@ public int getDaysOfMonth() { } public String getToYearMonth() { - return String.format("%04d/%02d", Year, Month); + return String.format("%04d/%02d " + TimeZone.getDisplayName(), Year, Month); } public String toString() { - return String.format("%04d/%02d/%02d", Year, Month, Day); + return String.format("%04d/%02d/%02d " + TimeZone.getDisplayName(), Year, Month, Day); } public String toLongString() { - return String.format("%04d/%02d/%02d/%02d/%02d/%02d", Year, Month, Day, Hour, Min, Sec); + return String.format("%04d/%02d/%02d %02d:%02d:%02d " + TimeZone.getDisplayName(), Year, Month, Day, Hour, Min, Sec); } @Override @@ -292,35 +300,8 @@ public int hashCode() { return hashCode; } - private static void Validate(DateModel dateModel) { - //year - if (dateModel.year < 0) - throw new IllegalArgumentException("invalid date"); - - //month - if (dateModel.month < 1 | dateModel.month > 12) - throw new IllegalArgumentException("invalid date"); - - //day - if (DateConverter.IsGregorianLeap(dateModel.year) & dateModel.month == 2) { - if (dateModel.day < 1 | dateModel.day > DaysInMonth[dateModel.month] + 1) - throw new IllegalArgumentException("invalid date"); - } else { - if (dateModel.day < 1 | dateModel.day > DaysInMonth[dateModel.month]) - throw new IllegalArgumentException("invalid date"); - } - - //hour - if (dateModel.month < 0 | dateModel.month > 23) - throw new IllegalArgumentException("invalid date"); - - //min - if (dateModel.month < 0 | dateModel.month > 60) - throw new IllegalArgumentException("invalid date"); - - //sec - if (dateModel.month < 0 | dateModel.month > 60) - throw new IllegalArgumentException("invalid date"); - + @Override + public int toUnixTime() { + return DateConverter.GregorianToUnix(Year, Month, Day, Hour, Min, Sec) - TimeZoneHelper.ToSeconds(TimeZone); } } 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 12731a9..57f86d3 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/HijriDateTime.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/HijriDateTime.java @@ -5,6 +5,8 @@ import com.ali.uneversaldatetools.model.DateModel; import com.ali.uneversaldatetools.tools.DateTools; +import java.util.TimeZone; + /** * Created by ali on 9/5/18. */ @@ -17,8 +19,9 @@ public class HijriDateTime implements IDate, Comparable { private int Hour; private int Min; private int Sec; + private TimeZone TimeZone; - private static final int[] DaysInMonth = { + public static final int[] DaysInMonth = { 0, 30, 29, @@ -34,54 +37,54 @@ public class HijriDateTime implements IDate, Comparable { 30 }; - public static GregorianDateTime FromUnixTime(int unixTime) { - DateModel dateModel = DateConverter.UnixToHijri(unixTime); - Validate(dateModel); - return new GregorianDateTime(dateModel); - } - - public HijriDateTime(DateModel date) { - DateModel sd = DateConverter.GregorianToHijri(date.year, date.month, date.day); - - Validate(sd); - - Year = sd.year; - Month = sd.month; - Day = sd.day; - Hour = sd.hour; - Min = sd.min; - Sec = sd.sec; - } - public HijriDateTime(int year, int month, int day) { - - Validate(new DateModel(year, month, day)); - + DateValidation.HijriValidate(year, month, day, 0, 0, 0); Year = year; Month = month; Day = day; + TimeZone = TimeZoneHelper.getSystemTimeZone(); } - public HijriDateTime(int year, int month, int day, int hour, int min, int sec) { - - Validate(new DateModel(year, month, day, hour, min, sec)); - - Year = year; - Month = month; - Day = day; + public HijriDateTime(int year, int month, int day, int hour, int min, int sec, TimeZone timeZone) { + this(year, month, day); + DateValidation.HijriValidate(year, month, day, hour, min, sec); Hour = hour; Min = min; Sec = sec; + TimeZone = timeZone; } public HijriDateTime(int days) { DateModel sd = DateConverter.DaysToHijri(days); - Validate(sd); Year = sd.year; Month = sd.month; Day = sd.day; + TimeZone = TimeZoneHelper.getSystemTimeZone(); } + public HijriDateTime(int days, int hour, int min, int sec, TimeZone timeZone) { + this(days); + Hour = hour; + Min = min; + Sec = sec; + TimeZone = timeZone; + } + + public HijriDateTime(int unixTimeSeconds, TimeZone timeZone) { + + int offset = TimeZoneHelper.ToSeconds(timeZone); + + DateModel dateModel = DateConverter.UnixToHijri(unixTimeSeconds + offset); + Year = dateModel.year; + Month = dateModel.month; + Day = dateModel.day; + Hour = dateModel.hour; + Min = dateModel.min; + Sec = dateModel.sec; + TimeZone = timeZone; + } + + public static HijriDateTime Parse(String date) { int y = Integer.valueOf(date.substring(0, 4)); int m = Integer.valueOf(date.substring(5, 7)); @@ -102,7 +105,8 @@ public static HijriDateTime ParseYearMonth(String yearMonth) { } public static HijriDateTime Now() { - return new HijriDateTime(DateTools.getCurrentDate()); + DateModel d = DateTools.getCurrentDate(); + return new HijriDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone()); } public DateModel getDate() { @@ -131,8 +135,9 @@ public DateModel getLastDayOfMonth() { } public DateModel FirstDayOfSeason(Season season) { - DateModel mFirstDayOfSeason = new GregorianDateTime(getDate()).FirstDayOfSeason(season); - HijriDateTime firstDayOfSeason = new HijriDateTime(mFirstDayOfSeason); + DateModel d = getDate(); + DateModel mFirstDayOfSeason = new GregorianDateTime(d.year, d.month, d.day).FirstDayOfSeason(season); + HijriDateTime firstDayOfSeason = new HijriDateTime(mFirstDayOfSeason.year, mFirstDayOfSeason.month, mFirstDayOfSeason.day); if (firstDayOfSeason.Year == Year) { if (firstDayOfSeason.Month < 11 || firstDayOfSeason.Month == 11 && firstDayOfSeason.Day <= 15) { @@ -140,7 +145,7 @@ public DateModel FirstDayOfSeason(Season season) { } getDate().month = getDate().month - 6; - return new HijriDateTime(getDate()).FirstDayOfSeason(season); + return new HijriDateTime(d.year, d.month, d.day).FirstDayOfSeason(season); } @@ -151,12 +156,13 @@ public DateModel FirstDayOfSeason(Season season) { } getDate().month = getDate().month - (firstDayOfSeason.Year < Year ? 6 : -6); - return new HijriDateTime(getDate()).FirstDayOfSeason(season); + return new HijriDateTime(d.year, d.month, d.day).FirstDayOfSeason(season); } public DateModel LastDayOfSeason(Season season) { - return new GregorianDateTime(FirstDayOfSeason(season)).LastDayOfSeason(season); + DateModel d = FirstDayOfSeason(season); + return new GregorianDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZone).LastDayOfSeason(season); } @@ -210,12 +216,14 @@ public DateModel AddDays(int days) { public JalaliDateTime getJalaliDateTime() { - return new JalaliDateTime(getDate()); + DateModel d = getDate(); + return new JalaliDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZone); } public GregorianDateTime getGregorianDateTime() { - return new GregorianDateTime(getDate()); + DateModel d = getDate(); + return new GregorianDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZone); } @@ -283,15 +291,15 @@ public DayOfWeek getDayOfWeek() { public String getToYearMonth() { - return String.format("%04d/%02d", Year, Month); //tested + return String.format("%04d/%02d " + TimeZone.getDisplayName(), Year, Month); //tested } public String toString() { - return String.format("%04d/%02d/%02d", Year, Month, Day); //tested + return String.format("%04d/%02d/%02d " + TimeZone.getDisplayName(), Year, Month, Day); //tested } public String toLongString() { - return String.format("%04d/%02d/%02d/%02d/%02d/%02d", Year, Month, Day, Hour, Min, Sec); + return String.format("%04d/%02d/%02d %02d:%02d:%02d " + TimeZone.getDisplayName(), Year, Month, Day, Hour, Min, Sec); } @Override @@ -328,35 +336,8 @@ public int hashCode() { return hashCode; } - private static void Validate(DateModel dateModel) { - //year - if (dateModel.year < 0) - throw new IllegalArgumentException("invalid date"); - - //month - if (dateModel.month < 1 | dateModel.month > 12) - throw new IllegalArgumentException("invalid date"); - - //day - if (DateConverter.IsGregorianLeap(dateModel.year) & dateModel.month == 12) { - if (dateModel.day < 1 | dateModel.day > DaysInMonth[dateModel.month] + 1) - throw new IllegalArgumentException("invalid date"); - } else { - if (dateModel.day < 1 | dateModel.day > DaysInMonth[dateModel.month]) - throw new IllegalArgumentException("invalid date"); - } - - //hour - if (dateModel.month < 0 | dateModel.month > 23) - throw new IllegalArgumentException("invalid date"); - - //min - if (dateModel.month < 0 | dateModel.month > 60) - throw new IllegalArgumentException("invalid date"); - - //sec - if (dateModel.month < 0 | dateModel.month > 60) - throw new IllegalArgumentException("invalid date"); - + @Override + public int toUnixTime() { + return DateConverter.HijriToUnix(Year, Month, Day, Hour, Min, Sec) - TimeZoneHelper.ToSeconds(TimeZone); } } diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/IDate.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/IDate.java index b23e192..3cc4387 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/IDate.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/IDate.java @@ -61,4 +61,6 @@ public interface IDate { String getYearMonthLetters(); String getToYearMonth(); + + int toUnixTime(); } 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 6eec62b..a719ea7 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/JalaliDateTime.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/JalaliDateTime.java @@ -5,6 +5,8 @@ import com.ali.uneversaldatetools.model.DateModel; import com.ali.uneversaldatetools.tools.DateTools; +import java.util.TimeZone; + /** * Created by ali on 9/5/18. */ @@ -17,9 +19,9 @@ public class JalaliDateTime implements IDate, Comparable { private int Hour; private int Min; private int Sec; + private TimeZone TimeZone; - - private static final int[] DaysInMonth = { + public static final int[] DaysInMonth = { 0, 31, 31, @@ -35,44 +37,25 @@ public class JalaliDateTime implements IDate, Comparable { 30 }; - public static GregorianDateTime FromUnixTime(int unixTime) { - DateModel dateModel = DateConverter.UnixToJalali(unixTime); - Validate(dateModel); - return new GregorianDateTime(dateModel); - } - - public JalaliDateTime(DateModel dateTime) { - DateModel sd = DateConverter.GregorianToJalali(dateTime.year, dateTime.month, dateTime.day); - - Validate(sd); - - Year = sd.year; - Month = sd.month; - Day = sd.day; - Hour = sd.hour; - Min = sd.min; - Sec = sd.sec; - } - public JalaliDateTime(int year, int month, int day) { - Validate(new DateModel(year, month, day)); + DateValidation.JalaliValidate(year, month, day, 0, 0, 0); Year = year; Month = month; Day = day; - } - public JalaliDateTime(int year, int month, int day, int hour, int min, int sec) { + TimeZone = TimeZoneHelper.getSystemTimeZone(); + } - Validate(new DateModel(year, month, day,hour,min,sec)); + public JalaliDateTime(int year, int month, int day, int hour, int min, int sec, TimeZone timeZone) { - Year = year; - Month = month; - Day = day; + this(year, month, day); + DateValidation.JalaliValidate(year, month, day, hour, min, sec); Hour = hour; Min = min; Sec = sec; + TimeZone = timeZone; } public JalaliDateTime(int days) { @@ -81,8 +64,32 @@ public JalaliDateTime(int days) { Year = sd.year; Month = sd.month; Day = sd.day; + TimeZone = TimeZoneHelper.getSystemTimeZone(); } + public JalaliDateTime(int days, int hour, int min, int sec, TimeZone timeZone) { + this(days); + Hour = hour; + Min = min; + Sec = sec; + TimeZone = timeZone; + } + + public JalaliDateTime(int unixTimeSeconds, TimeZone timeZone) { + + int offset = TimeZoneHelper.ToSeconds(timeZone); + + DateModel dateModel = DateConverter.UnixToJalali(unixTimeSeconds + offset); + Year = dateModel.year; + Month = dateModel.month; + Day = dateModel.day; + Hour = dateModel.hour; + Min = dateModel.min; + Sec = dateModel.sec; + TimeZone = timeZone; + } + + public static JalaliDateTime Parse(String s) { int y = Integer.valueOf(s.substring(0, 4)); int m = Integer.valueOf(s.substring(5, 7)); @@ -103,7 +110,9 @@ public static JalaliDateTime ParseYearMonth(String s) { } public static JalaliDateTime Now() { - return new JalaliDateTime(DateTools.getCurrentDate()); + 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()); } public DateModel getDate() { @@ -194,11 +203,13 @@ public JalaliDateTime getJalaliDateTime() { } public GregorianDateTime getGregorianDateTime() { - return new GregorianDateTime(getDate()); + DateModel dateModel = getDate(); + return new GregorianDateTime(dateModel.year, dateModel.month, dateModel.day, dateModel.hour, dateModel.min, dateModel.sec, TimeZoneHelper.getSystemTimeZone()); } public HijriDateTime getHijriDateTime() { - return new HijriDateTime(getDate()); + DateModel d = getDate(); + return new HijriDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZone); } public String getLetters() { @@ -255,15 +266,15 @@ public int getDaysOfMonth() { } public String getToYearMonth() { - return String.format("%04d/%02d", Year, Month); + return String.format("%04d/%02d " + TimeZone.getDisplayName(), Year, Month); } public String toString() { - return String.format("%04d/%02d/%02d", Year, Month, Day); + return String.format("%04d/%02d/%02d " + TimeZone.getDisplayName(), Year, Month, Day); } public String toLongString() { - return String.format("%04d/%02d/%02d/%02d/%02d/%02d", Year, Month, Day, Hour, Min, Sec); + return String.format("%04d/%02d/%02d %02d:%02d:%02d " + TimeZone.getDisplayName(), Year, Month, Day, Hour, Min, Sec); } @Override @@ -299,36 +310,8 @@ public int hashCode() { return hashCode; } - private static void Validate(DateModel dateModel) { - //year - if (dateModel.year < 0) - throw new IllegalArgumentException("invalid date"); - - //month - if (dateModel.month < 1 | dateModel.month > 12) - throw new IllegalArgumentException("invalid date"); - - //day - if (DateConverter.IsGregorianLeap(dateModel.year) & dateModel.month == 12) { - if (dateModel.day < 1 | dateModel.day > DaysInMonth[dateModel.month] + 1) - throw new IllegalArgumentException("invalid date"); - } else { - if (dateModel.day < 1 | dateModel.day > DaysInMonth[dateModel.month]) - throw new IllegalArgumentException("invalid date"); - } - - //hour - if (dateModel.month < 0 | dateModel.month > 23) - throw new IllegalArgumentException("invalid date"); - - //min - if (dateModel.month < 0 | dateModel.month > 60) - throw new IllegalArgumentException("invalid date"); - - //sec - if (dateModel.month < 0 | dateModel.month > 60) - throw new IllegalArgumentException("invalid date"); - + @Override + public int toUnixTime() { + return DateConverter.JalaliToUnix(Year, Month, Day, Hour, Min, Sec) - TimeZoneHelper.ToSeconds(TimeZone); } - } diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/TimeZoneHelper.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/TimeZoneHelper.java index 0deed88..e740e07 100644 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/TimeZoneHelper.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/date/TimeZoneHelper.java @@ -9,21 +9,15 @@ public class TimeZoneHelper { - public static TimeZone getCurrentTimeZone() { + public static TimeZone getSystemTimeZone() { return TimeZone.getDefault(); } - /** - * @return in minute - */ - public static int getCurrentTimeZoneOffset() { - return getCurrentTimeZone().getOffset(new Date().getTime()) / 1000 / 60; //210 minute for iran + public static int getSystemOffsetSeconds() { + return (getSystemTimeZone().getOffset(new Date().getTime()) / 1000 / 60) * 60; } - /** - * @return in second - */ - public static int getCurrentTimeZoneOffsetSec() { - return (getCurrentTimeZoneOffset()) * 60; //to second + public static int ToSeconds(TimeZone timeZone){ + return (timeZone.getOffset(new Date().getTime()) / 1000 / 60) * 60; } } 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 b13a8eb..c504993 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/datePicker/UDatePicker.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/datePicker/UDatePicker.java @@ -280,10 +280,10 @@ public DateSystem getSelectedDate() { * you can show message like "plz select day" */ @Nullable - public Long getSelectedUnixTime() { + public Integer getSelectedUnixTime() { DateSystem dateSystem = getSelectedDate(); if (dateSystem == null) return null; - else return dateSystem.getUnixTime(); + else return dateSystem.toUnixTime(); } private void SetupDayOfWeek() { @@ -367,7 +367,7 @@ public void onDaySelectListener(int day, int monthPos) { if (listener != null) { DateSystem dateSystem = new DateSystem(mDateSystem.getYear(), monthPos, day, mDateSystem.getCalendar()); - listener.onDateChange(dateSystem, dateSystem.getUnixTime()); + listener.onDateChange(dateSystem, dateSystem.toUnixTime()); } } diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/model/DateModel.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/model/DateModel.java index b7c8595..7e0fbf3 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/model/DateModel.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/model/DateModel.java @@ -27,5 +27,4 @@ public DateModel(int year, int month, int day, int hour, int min, int sec) { this.min = min; this.sec = sec; } - } \ No newline at end of file diff --git a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/DateTools.java b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/DateTools.java index 75cdbe4..3bb410c 100755 --- a/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/DateTools.java +++ b/uneversaldatetools/src/main/java/com/ali/uneversaldatetools/tools/DateTools.java @@ -16,4 +16,8 @@ public static DateModel getCurrentDate() { 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/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java b/uneversaldatetools/src/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java index 93759bf..2523441 100755 --- a/uneversaldatetools/src/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java +++ b/uneversaldatetools/src/test/java/com/ali/uneversaldatetools/ExampleUnitTest.java @@ -1,15 +1,18 @@ package com.ali.uneversaldatetools; -import com.ali.uneversaldatetools.date.Calendar; import com.ali.uneversaldatetools.date.DateConverter; -import com.ali.uneversaldatetools.date.DateSystem; +import com.ali.uneversaldatetools.date.GregorianDateTime; import com.ali.uneversaldatetools.date.HijriDateTime; 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 org.junit.Assert; import org.junit.Test; +import java.util.Date; + /** * Example local unit test, which will execute on the development machine (host). * @@ -18,33 +21,26 @@ public class ExampleUnitTest { @Test - public void CurrentTimeTest() { - DateModel date = DateTools.getCurrentDate(); - Log(date.year); - Log(date.month); - Log(date.day); - } - - - @Test - public void Test() { - DateModel date = DateTools.getCurrentDate(); - DateSystem dateSystem = new DateSystem(date, Calendar.Jalali); - Log(dateSystem.getYear() + "/" + dateSystem.getMonth() + "/" + dateSystem.getDate()); - } - - @Test - public void ToStringTest(){ - JalaliDateTime dateTime = new JalaliDateTime(1377,1,27); - Log(dateTime); - Log(dateTime.toLongString()); + public void ToStringTest() { + JalaliDateTime jTime = new JalaliDateTime(1377, 1, 27); + Log(jTime.toString()); + Log(jTime.toLongString()); + Log("---"); + HijriDateTime hTime = new HijriDateTime(1440, 1, 27); + Log(hTime.toString()); + Log(hTime.toLongString()); + Log("---"); + GregorianDateTime gTime = new GregorianDateTime(2002, 1, 27); + Log(gTime.toString()); + Log(gTime.toLongString()); } @Test public void UnixTest() { - int unixTime = 1538316150; - // int max 2147483647; - DateModel dateModel = DateConverter.UnixToGregorian(unixTime); + int unixTime = DateTools.getCurrentUnixTime(); + + Log(unixTime); + DateModel dateModel = DateConverter.UnixToJalali(unixTime); Log(dateModel.year); Log(dateModel.month); Log(dateModel.day); @@ -52,35 +48,28 @@ public void UnixTest() { Log(dateModel.min); Log(dateModel.sec); Log("-------"); - long days = DateConverter.GregorianToDays(1969, 12, 31); - Log(days); - long sec = days * 86400; - Log(sec); - dateModel = DateConverter.UnixToGregorian(unixTime - 2006141056); - Log(dateModel.year); - Log(dateModel.month); - Log(dateModel.day); - - Log("--"); - Log(Integer.MAX_VALUE); + int unixTime2 = DateConverter.JalaliToUnix( + dateModel.year, + dateModel.month, + dateModel.day, + dateModel.hour, + dateModel.min, + dateModel.sec); + Log(unixTime2); + Assert.assertTrue(unixTime == unixTime2); } @Test public void JalaliTest() { - HijriDateTime mDateTime = new HijriDateTime(DateTools.getCurrentDate()); + JalaliDateTime mDateTime = new JalaliDateTime((int) new Date().getTime(), TimeZoneHelper.getSystemTimeZone()); + Log(mDateTime.toLongString()); } @Test public void HijriTest() { - JalaliDateTime mDateTime = new JalaliDateTime(DateTools.getCurrentDate()); Log(DateConverter.IsHijriLeap(1439)); } - @Test - public void SystemDateTest() { - DateSystem mDateTime = new DateSystem(DateTools.getCurrentDate(), Calendar.Hijri); - } - private void Log(Object s) { System.out.println(s); }