June 19, 2018

Simple Date functions

On this blog I am going to explain some date functionality's which we are commonly used in our android application. 


Current Date Time

public static String getCurrentDate() {
    String DATE_FORMAT_NOW = "dd/MM/yyyy";
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
    return sdf.format(new Date());
}
      
      This function will return the current system date and time on  dd/MM/yyyy HH:mm this format. There is so many other different Date time formats are available,  You can replace the current format (dd/MM/yyyy HH:mm) with your date format if you need.





Compare two Date Time


public static boolean checkDateBeforeFirstDate(String first, String second) { 
    try {        
        String DATE_FORMAT_NOW = "dd/MM/yyyy HH:mm";        
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);        
        Date firstDate = sdf.parse(first);        
        Date secondDate = sdf.parse(second);        
        if(secondDate.before(firstDate))            
            return true;        
        else            
            return false;    
    }catch (Exception e){        
        return false;    
    }
}


             This function will return true or false by comparing the given 2 dates, Here I am used date time format as  dd/MM/yyyy HH:mm this. There is so many other different Date time formats are available,  You can replace the current format (dd/MM/yyyy HH:mm) with your date format if you need. 










Change Date Time Format
public static String changeToServerFormat(String time) {
    String inputPattern = "dd/MM/yyyy HH:mm";    
    String outputPattern = "yyyy-MM-dd HH:mm";    
    SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);    
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);     
    Date date = null;    String str = null;     
    try {        
        date = inputFormat.parse(time);        
        str = outputFormat.format(date);    
    } catch (ParseException e) {        
        e.printStackTrace();    
    }   
    return str;
}
                   This function will return the converted format date string. On the above function will convert dd/MM/yyyy HH:mm format date time string to yyyy-MM-dd HH:mm. There is so many other different Date time formats are available, You can replace the input or output date time format with your date format if you need. 




Current Week Days

public static ArrayList<String> currentWeekDaysOfMonth() {
    ArrayList<String> weekDays = new ArrayList<>();
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    SimpleDateFormat date = new SimpleDateFormat("EEE dd MMMM yyyy", Locale.ENGLISH);
    for (int i = 0; i < 7; i++) {
        String weekDay =  date.format(cal.getTime()); 
        Log.v("WeekDays", "Date: " + weekDay);
        weekDays.add(weekDay);
        cal.add(Calendar.DAY_OF_WEEK, 1);
    }
    return weekDays;
}



                  This function will return the current weeks days as a String ArrayList. On the above function will return the date on EEE dd MMMM yyyy format. On the above function it will return the current weekdays starting from Sunday (We can specify it using .setFirstDayOfWeek(Calendar.SUNDAY) code). 


---------------
Thanks For Reading, Wish you a Happy Coding....