Created
November 14, 2019 07:11
-
-
Save shmehdi01/abf94e48a85bda3f349f46f1cb341348 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Utils { | |
public static void changeFragment( | |
FragmentActivity activity, | |
Fragment fragment, @IdRes int id, | |
boolean addToStack, | |
String tag, | |
boolean shouldAnimate, | |
boolean replace | |
) { | |
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction(); | |
if (shouldAnimate) ft.setCustomAnimations( | |
R.anim.anim_next_slide_in, | |
R.anim.anim_next_slide_out, | |
R.anim.anim_prev_slide_in, | |
R.anim.anim_prev_slide_out | |
); | |
if (replace) ft.replace(id, fragment, tag); | |
else ft.add(id, fragment, tag); | |
if (addToStack) ft.addToBackStack(tag); | |
ft.commit(); | |
if (fragment instanceof BaseFragment) { | |
SSApp.getInstance().activeFragment = (BaseFragment) fragment; | |
} | |
} | |
public static String getCurrentDateCode(String format) { | |
return new SimpleDateFormat(format, Locale.getDefault()).format(new Date()); | |
} | |
public static long getDaysDiff(String fromDateStr, String toDateStr, String dateFormat) { | |
try { | |
Date date1 =new SimpleDateFormat(dateFormat).parse(fromDateStr); | |
Date date2 =new SimpleDateFormat(dateFormat).parse(toDateStr); | |
long diff = date2.getTime() - date1.getTime(); | |
long days = TimeUnit.MILLISECONDS.toDays(diff); | |
System.out.println("Days " + days); | |
return days; | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return -1; | |
} | |
private static int getPreviousYear() { | |
Calendar prevYear = Calendar.getInstance(); | |
prevYear.add(Calendar.YEAR, -1); | |
return prevYear.get(Calendar.YEAR); | |
} | |
private static int getPreviousMonth() { | |
Calendar prevMonth = Calendar.getInstance(); | |
prevMonth.add(Calendar.MONTH, -1); | |
return prevMonth.get(Calendar.MONTH); | |
} | |
public static String yesterday() { | |
Calendar cal = Calendar.getInstance(); | |
cal.add(Calendar.DATE, -1); | |
DateFormat dateFormat = new SimpleDateFormat(GlobalEnum.DATE_FORMAT); | |
return dateFormat.format(cal.getTime()); | |
} | |
public static String getFirstDayOfWeek() { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.add(Calendar.DAY_OF_MONTH, Calendar.MONDAY - calendar.get(Calendar.DAY_OF_WEEK)); | |
int year1 = calendar.get(Calendar.YEAR); | |
int month1 = calendar.get(Calendar.MONTH) + 1; | |
int day1 = calendar.get(Calendar.DAY_OF_MONTH); | |
return year1 + "-" + CommonUtils.getMonthDate("" + (month1)) + "-" + CommonUtils.getMonthDate("" + day1); | |
} | |
public static String getLastDayOfWeek() { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.add(Calendar.DAY_OF_MONTH, Calendar.SUNDAY); | |
int year1 = calendar.get(Calendar.YEAR); | |
int month1 = calendar.get(Calendar.MONTH) + 1; | |
int day1 = calendar.get(Calendar.DAY_OF_MONTH); | |
return year1 + "-" + CommonUtils.getMonthDate("" + (month1)) + "-" + CommonUtils.getMonthDate("" + day1); | |
} | |
public static String getFirstDayOfMonth() { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.add(Calendar.DAY_OF_MONTH, Calendar.MONDAY - calendar.get(Calendar.DAY_OF_MONTH)); | |
int year1 = calendar.get(Calendar.YEAR); | |
int month1 = calendar.get(Calendar.MONTH) + 1; | |
int day1 = calendar.get(Calendar.DAY_OF_MONTH); | |
return year1 + "-" + CommonUtils.getMonthDate("" + (month1)) + "-" + CommonUtils.getMonthDate("" + 1); | |
} | |
public static String getLastDayOfMonth() { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.getActualMaximum(Calendar.DAY_OF_MONTH); | |
calendar.add(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH); | |
int year1 = calendar.get(Calendar.YEAR); | |
int month1 = calendar.get(Calendar.MONTH) + 1; | |
int day1 = calendar.get(Calendar.DAY_OF_MONTH); | |
return year1 + "-" + CommonUtils.getMonthDate("" + (month1)) + "-" + CommonUtils.getMonthDate("" + day1); | |
} | |
public static String getFirstDayOfYear() { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.add(Calendar.DAY_OF_MONTH, Calendar.MONDAY - calendar.get(Calendar.DAY_OF_MONTH)); | |
int year1 = calendar.get(Calendar.YEAR); | |
return year1 + "-" + CommonUtils.getMonthDate("" + (1)) + "-" + CommonUtils.getMonthDate("" + 1); | |
} | |
public static String getLastDayOfYear() { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.getActualMaximum(Calendar.DAY_OF_YEAR); | |
int year1 = calendar.get(Calendar.YEAR); | |
int month1 = calendar.get(Calendar.MONTH) + 1; | |
int day1 = calendar.get(Calendar.DAY_OF_MONTH); | |
return year1 + "-" + CommonUtils.getMonthDate("" + (month1)) + "-" + CommonUtils.getMonthDate("" + day1); | |
} | |
public static int getDateDifferenceInMonth(String strStartDate, String strEndDate) { | |
try { | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | |
Calendar startCalendar = new GregorianCalendar(); | |
Date date1 = sdf.parse(sdf.format(startCalendar.getTime())); | |
startCalendar.setTime(date1); | |
Calendar endCalendar = new GregorianCalendar(); | |
Date endDate = sdf.parse(sdf.format(endCalendar.getTime())); | |
endCalendar.setTime(endDate); | |
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR); | |
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH); | |
return diffMonth; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return 0; | |
} | |
public static String getManufacturerAndModelName() { | |
String manufacturer = Build.MANUFACTURER; | |
String model = Build.MODEL; | |
if (model.startsWith(manufacturer)) { | |
return (model); | |
} | |
return manufacturer + " " + model; | |
} | |
public static String getDeviceManufacturer() { | |
String manufacturer = Build.MANUFACTURER; | |
String model = Build.MODEL; | |
if (model.startsWith(manufacturer)) { | |
return (model); | |
} | |
return (manufacturer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment