Code example for GregorianCalendar

Methods: set

0
     * @param calendar 
     * @return String representing the relative date 
     */ 
 
    public static String getRelativeDate(Calendar calendar) {
        Calendar today = new GregorianCalendar();
        today.set(GregorianCalendar.HOUR_OF_DAY, 0);
        today.set(GregorianCalendar.MINUTE, 0);
        today.set(GregorianCalendar.SECOND, 0);
        today.set(GregorianCalendar.MILLISECOND, 0);
 
        return getRelativeDate(today, calendar);
    } 
 
    public static String getRelativeDate(Calendar d1, Calendar d2) {
        long diff = d1.getTimeInMillis() - d2.getTimeInMillis();
 
        if (diff < 0 || diff >= YEAR) {
            // future or far in past, 
            // just return yyyy-mm-dd 
            return sdf.format(d2.getTime());