Date Formatting is frequently used in programming and it is like headache for developers to format date to store into database and to display for users. Sometimes, app is crashed due to wrong date formatting. Here, I have give you best example of some different Date formats and get different Date Range.
First of all, Make new Java Class, name it Utils.java and copy below code into it.
DB_DATE_FORMAT and DISPLAY_DATE_FORMAT are different date formats and we can convert date from one format to another. You can use your own date format as well. To do this you just need to change 2 strings of date formats.
1. Display Date Format(): This method is convert date from DB_DATE_FORMAT to DISPLAY_DATE_FORMAT. Make sure your input in dtOldFormat is exact same as DB_DATE_FORMAT. Otherwise, it will throw an exception.
2. getLast7Days(): It will give you last 7 days list including todays date. Pair<String,String> function stores first and last date of range.
3. getPreviousMonth(): same as above, it will return Previous month's first and last date.
4. getSameMonthLastYear(): As name suggested it will give you same month date for previous month.
5. getCurrentMonthDateRange(): will give you current month date range
Other Private methods are used to set current calendar date and time for use of this class only.
public class Utils {
public String DB_DATE_FORMAT = "yyyy-MM-dd";
public String DISPLAY_DATE_FORMAT = "dd MMM yyyy";
public String displayDateFormat(String dtOldFormat) {
try {
DateFormat inputFormat = new SimpleDateFormat(DB_DATE_FORMAT);
DateFormat outputFormat = new SimpleDateFormat(DISPLAY_DATE_FORMAT);
Date date = inputFormat.parse(dtOldFormat);
String outputDateStr = outputFormat.format(date);
return outputDateStr;
} catch (Exception e) {
e.printStackTrace();
return dtOldFormat;
}
}
public Pair<String,String> getLast7Days(){
String begining,ending;
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DB_DATE_FORMAT);
ending = sdf.format(cal.getTime());
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, -6);
begining = sdf.format(cal.getTime());
return Pair.create(begining,ending);
}
public Pair<String, String> getPreviousMonth(){
Date begining, end;
Calendar calendar = GregorianCalendar.getInstance();
calendar.add(Calendar.MONTH,-1);
{
calendar.set(Calendar.DAY_OF_MONTH,
calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
setTimeToBeginningOfDay(calendar);
begining = calendar.getTime();
}
{
calendar.set(Calendar.DAY_OF_MONTH,
calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setTimeToEndofDay(calendar);
end = calendar.getTime();
}
DateFormat inputFormat = new SimpleDateFormat(DB_DATE_FORMAT);
return android.util.Pair.create(inputFormat.format(begining),inputFormat.format(end));
}
public Pair<String, String> getSameMonthLastYear(){
Date begining, end;
Calendar calendar = GregorianCalendar.getInstance();
calendar.add(Calendar.YEAR,-1);
{
calendar.set(Calendar.DAY_OF_MONTH,
calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
setTimeToBeginningOfDay(calendar);
begining = calendar.getTime();
}
{
calendar.set(Calendar.DAY_OF_MONTH,
calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setTimeToEndofDay(calendar);
end = calendar.getTime();
}
DateFormat inputFormat = new SimpleDateFormat(DB_DATE_FORMAT);
return android.util.Pair.create(inputFormat.format(begining),inputFormat.format(end));
}
public android.util.Pair<String, String> getCurrentMonthDateRange() {
Date begining, end;
{
Calendar calendar = getCalendarForNow();
calendar.set(Calendar.DAY_OF_MONTH,
calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
setTimeToBeginningOfDay(calendar);
begining = calendar.getTime();
}
{
Calendar calendar = getCalendarForNow();
calendar.set(Calendar.DAY_OF_MONTH,
calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setTimeToEndofDay(calendar);
end = calendar.getTime();
}
DateFormat inputFormat = new SimpleDateFormat(DB_DATE_FORMAT);
return android.util.Pair.create(inputFormat.format(begining),inputFormat.format(end));
}
private static Calendar getCalendarForNow() {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(new Date());
return calendar;
}
private static void setTimeToBeginningOfDay(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
private static void setTimeToEndofDay(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
}
}