Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
abduelrahman committed Jul 12, 2020
1 parent 2db732b commit 2f5f6fe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.abduelrahman.dateutils;

import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import am.dateutils.DateUtils;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toast.makeText(this, new DateUtils(this, "2020-07-12T17:00:49Z").setSpecificFormat("MMMM dd, yyyy"), Toast.LENGTH_LONG).show();

}

}
39 changes: 16 additions & 23 deletions dateUtils/src/main/java/am/dateutils/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DateUtils {
private Locale appLocale;
private Calendar calendar;
private String txtDate, txtTimeZone;
private DateFormat formatBackend, defaultDateFormat;
private DateFormat formatBackend, fullBackendFormat, defaultDateFormat;


//------------------------- Class Constructor ------------------------------
Expand Down Expand Up @@ -62,6 +62,7 @@ private void init(String appLocale) {
else
this.appLocale = Locale.US;

fullBackendFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", this.appLocale);
formatBackend = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", this.appLocale);
defaultDateFormat = new SimpleDateFormat("MMM dd yyyy", this.appLocale);

Expand Down Expand Up @@ -115,9 +116,13 @@ private Date getDateFormat(String txtDate, String timeZone) {
date = formatBackend.parse(txtDate);
} catch (ParseException e) {
try {
date = defaultDateFormat.parse(txtDate);
date = fullBackendFormat.parse(txtDate);
} catch (ParseException e1) {
e1.printStackTrace();
try {
date = defaultDateFormat.parse(txtDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
}
return date;
Expand Down Expand Up @@ -169,34 +174,22 @@ public int compareTwoDates(String firstDate, String firstDateTimeZone, String la


public boolean getComparedDate(String StartDate, String EndDate) {
boolean result = false;
Calendar c = Calendar.getInstance(Locale.ENGLISH);
try {
Date currentDate = formatBackend.parse(formatBackend.format(c.getTime()));
Date startDate = formatBackend.parse(StartDate);
Date endDate = formatBackend.parse(EndDate);
Date currentDate = getDateFormat(formatBackend.format(c.getTime()), null);
Date startDate = getDateFormat(StartDate, null);
Date endDate = getDateFormat(EndDate, null);

result = currentDate.after(startDate) && currentDate.before(endDate);
} catch (ParseException e) {
e.printStackTrace();
}
return result;
return currentDate.after(startDate) && currentDate.before(endDate);
}


public boolean getComparedDate(am.dateutils.Date startDate, am.dateutils.Date endDate) {
boolean result = false;
Calendar c = Calendar.getInstance(Locale.ENGLISH);
try {
Date currentDate = formatBackend.parse(formatBackend.format(c.getTime()));
Date start = getDateFormat(startDate.getDate(), startDate.getTimezone());
Date end = getDateFormat(endDate.getDate(), endDate.getTimezone());
Date currentDate = getDateFormat(formatBackend.format(c.getTime()), null);
Date start = getDateFormat(startDate.getDate(), startDate.getTimezone());
Date end = getDateFormat(endDate.getDate(), endDate.getTimezone());

result = currentDate.after(start) && currentDate.before(end);
} catch (ParseException e) {
e.printStackTrace();
}
return result;
return currentDate.after(start) && currentDate.before(end);
}


Expand Down

0 comments on commit 2f5f6fe

Please sign in to comment.