Example usage for java.util Calendar after

List of usage examples for java.util Calendar after

Introduction

In this page you can find the example usage for java.util Calendar after.

Prototype

public boolean after(Object when) 

Source Link

Document

Returns whether this Calendar represents a time after the time represented by the specified Object.

Usage

From source file:Main.java

/**
 * Get the age of the user. Takes in their birthday and calculates it according to today's date
 * @param birthday Date Object//from  ww  w  .j a v a2s  .  c o  m
 * @return Returns an int of their age (IE 20, 55, 18). If the date is in the future, it will
 *         return -1 instead.
 */
public static int getAge(Date birthday) {

    Calendar now = Calendar.getInstance();
    Calendar dob = Calendar.getInstance();
    dob.setTime(birthday);

    //First check for in the future:
    if (dob.after(now)) {
        return -1;
    }

    int year1 = now.get(Calendar.YEAR);
    int year2 = dob.get(Calendar.YEAR);

    int age = year1 - year2;

    int month1 = now.get(Calendar.MONTH);
    int month2 = dob.get(Calendar.MONTH);

    if (month2 > month1) {
        age--;

    } else if (month1 == month2) {
        int day1 = now.get(Calendar.DAY_OF_MONTH);
        int day2 = dob.get(Calendar.DAY_OF_MONTH);
        if (day2 > day1) {
            age--;
        }
    }

    return age;
}

From source file:net.duckling.ddl.util.Utility.java

public static String getDateShort(Date date) {
    Calendar calToday = Calendar.getInstance();
    calToday.set(Calendar.HOUR_OF_DAY, 0);
    Calendar calYear = Calendar.getInstance();
    calYear.set(Calendar.DAY_OF_YEAR, 1);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//w w  w.ja  va2s  . c  o  m
    if (cal.after(calToday)) {
        return (new SimpleDateFormat("HH:mm")).format(cal.getTime());
    } else if (cal.after(calYear)) {
        return (new SimpleDateFormat("MMdd")).format(cal.getTime());
    } else {
        return (new SimpleDateFormat("yyyy")).format(cal.getTime());
    }
}

From source file:Main.java

public static boolean isDateSize(int[] oneDate, int[] twoDate) {
    Calendar oneCalendar = Calendar.getInstance();
    Calendar twoCalendar = Calendar.getInstance();
    oneCalendar.set(oneDate[0], oneDate[1] - 1, oneDate[2]);
    twoCalendar.set(twoDate[0], twoDate[1] - 1, twoDate[2]);
    return twoCalendar.after(oneCalendar);
}

From source file:Main.java

static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future) {
    int diff = 0;
    long savedDate = fromDate.getTimeInMillis();
    while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) {
        savedDate = fromDate.getTimeInMillis();
        fromDate.add(type, future ? 1 : -1);
        diff++;//  ww w . ja va 2  s. c  o  m
    }
    diff--;
    fromDate.setTimeInMillis(savedDate);
    return diff;
}

From source file:Main.java

public static String formatDateDiff(Calendar fromDate, Calendar toDate) {
    boolean future = false;
    if (toDate.equals(fromDate)) {
        return ("now");
    }/*from ww w  .  jav  a  2s.  c  om*/
    if (toDate.after(fromDate)) {
        future = true;
    }
    StringBuilder sb = new StringBuilder();
    int[] types = new int[] { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY,
            Calendar.MINUTE, Calendar.SECOND };
    String[] names = new String[] { ("year"), ("years"), ("month"), ("months"), ("day"), ("days"), ("hour"),
            ("hours"), ("minute"), ("minutes"), ("second"), ("seconds") };
    int accuracy = 0;
    for (int i = 0; i < types.length; i++) {
        if (accuracy > 2) {
            break;
        }
        int diff = dateDiff(types[i], fromDate, toDate, future);
        if (diff > 0) {
            accuracy++;
            sb.append(" ").append(diff).append(" ").append(names[i * 2 + (diff > 1 ? 1 : 0)]);
        }
    }
    if (sb.length() == 0) {
        return "now";
    }
    return sb.toString().trim();
}

From source file:Main.java

public static String getTimeFromLong(long l) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(l);//from  www .j av a 2 s .  c o m

    Calendar t = Calendar.getInstance();
    t.set(Calendar.HOUR_OF_DAY, 0);
    t.set(Calendar.MINUTE, 1);
    t.set(Calendar.SECOND, 0);

    if (c.after(t)) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumIntegerDigits(2);

        return nf.format(c.get(Calendar.HOUR_OF_DAY)) + ":" + nf.format(c.get(Calendar.MINUTE));
    } else {
        return TIME_NONE;
    }
}

From source file:org.eclipse.smarthome.binding.astro.internal.util.DateTimeUtils.java

/**
 * Returns the next Calendar from today.
 *//*from ww  w.j av  a  2s .com*/
public static Calendar getNext(Calendar... calendars) {
    Calendar now = Calendar.getInstance();
    Calendar next = null;
    for (Calendar calendar : calendars) {
        if (calendar.after(now) && (next == null || calendar.before(next))) {
            next = calendar;
        }
    }
    return next;
}

From source file:Main.java

/**
 * Takes a date value as used in CPLC Date fields (represented by 2 bytes)
 * //w  w  w. j av a2  s  .  com
 * @param paramByte1
 * @param paramByte2
 * @throws IllegalArgumentException
 * @return
 */
public static Date calculateCplcDate(byte[] dateBytes) throws IllegalArgumentException {
    if (dateBytes == null || dateBytes.length != 2) {
        throw new IllegalArgumentException("Error! CLCP Date values consist always of exactly 2 bytes");
    }
    // current time
    Calendar now = Calendar.getInstance();

    int year = now.get(Calendar.YEAR);
    int startYearOfCurrentDecade = year - (year % 10);

    int days = 100 * (dateBytes[0] & 0xF) + 10 * (0xF & dateBytes[1] >>> 4) + (dateBytes[1] & 0xF);

    if (days > 366) {
        throw new IllegalArgumentException("Invalid date (or are we parsing it wrong??)");
    }

    Calendar calculatedDate = Calendar.getInstance();
    calculatedDate.clear();
    calculatedDate.set(Calendar.YEAR, startYearOfCurrentDecade + (0xF & dateBytes[0] >>> 4));
    calculatedDate.set(Calendar.DAY_OF_YEAR, days);
    while (calculatedDate.after(now)) {
        calculatedDate.add(Calendar.YEAR, -10);
    }
    return calculatedDate.getTime();
}

From source file:org.eyeseetea.malariacare.utils.Utils.java

public static boolean isDateOverSystemDate(Calendar closedDate) {
    if (closedDate != null) {
        Calendar sysDate = Calendar.getInstance();
        sysDate.setTime(new Date());
        if (sysDate.after(closedDate)) {
            return false;
        }//from w  w  w .java 2  s .c  om
    }
    return true;
}

From source file:erpsystem.chart.Charts.java

/** Grfico criado para mostrar o histrico de
 * faturamento periodicamente considerando o valor bruto
 * em R$ de lucro.//from w  w  w .  ja v a  2s .co  m
*/

public static BufferedImage create002(int w, int h, Calendar initialCalendar, int interval) {
    //Inicio
    DefaultCategoryDataset ds = new DefaultCategoryDataset();
    String LUCRO = "Lucro R$";

    //Configurando os Calendars.
    Calendar finalCalendar = getCurrentCalendar();
    Calendar finalValue = copyCalendar(initialCalendar);
    Calendar initialValue = copyCalendar(initialCalendar);

    while (finalCalendar.after(finalValue)) {

        //ajustando os Calendars ponteiros.
        initialValue = copyCalendar(finalValue);
        finalValue.add(Calendar.DAY_OF_MONTH, interval);

        //toString para depurao.
        String s1 = toString(initialValue);
        String s2 = toString(finalValue);

        //Obtendo as datas como raw para 
        //possibilitar comparao com o banco de dados.
        long value1 = initialValue.getTimeInMillis();
        long value2 = finalValue.getTimeInMillis();

        //Obtendo a nova abstrao de lucro com os intervalos especificados.
        double lucro = Chart001.getValorLucroEm(value1, value2);
        double venda = Chart001.getValorVendaEm(value1, value2);
        double compra = Chart001.getValorCompraEm(value1, value2);

        //Adicionando a nova abstrao na coleo de dados do grfico.
        int y = initialValue.get(Calendar.YEAR);
        int m = initialValue.get(Calendar.MONTH) + 1;
        int d = initialValue.get(Calendar.DAY_OF_MONTH);
        String identifier = d + "/" + m + "/" + y;
        ds.addValue(lucro, "Lucro", identifier);
        ds.addValue(venda, "Venda", identifier);
        ds.addValue(compra, "Compra", identifier);

    }

    //Criando o grfico abstrato em 3D.
    JFreeChart chart = ChartFactory.createBarChart3D("Faturamento", "Faturamento", "Valor R$", ds,
            PlotOrientation.VERTICAL, true, true, false);

    //Criando e retornando a imagem como 
    //mapa de pixels do grfico.
    return chart.createBufferedImage(w, h);
}