Example usage for java.util Calendar setTime

List of usage examples for java.util Calendar setTime

Introduction

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

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:Main.java

public static Date add(Date date, int amount, int field) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(field, amount);/*  w ww  .  j  a  v a 2s.co m*/
    return calendar.getTime();
}

From source file:Main.java

public static String dateGraphLabelParser(long epoch) {
    Date date = new Date(epoch);
    Calendar cal = Calendar.getInstance();

    cal.setTime(date);

    return String.format("%d/%d/%d", cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH),
            cal.get(Calendar.YEAR));
}

From source file:Main.java

public static long getDiffOfMillis(Date starDate, Date endDate) {
    Calendar star = Calendar.getInstance();
    star.setTime(starDate);
    Calendar end = Calendar.getInstance();
    end.setTime(endDate);//w w  w  .j av  a2  s.c om
    long diff = star.getTimeInMillis() - end.getTimeInMillis();
    long diffMillis = (Math.abs(diff));
    return diffMillis;
}

From source file:Main.java

public static String getBeforeTime(int months, SimpleDateFormat format, Date now) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(now);
    calendar.add(calendar.MONTH, -months);
    return format.format(calendar.getTime());
}

From source file:Main.java

public static Date getPreNDay(Date presentDate, int n) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(presentDate);
    calendar.add(Calendar.DAY_OF_MONTH, -n);
    presentDate = calendar.getTime();//  w ww  .j a  v a2  s  .  c  om
    return presentDate;
}

From source file:Main.java

public static int getWeekOfDateDecimal(Date dt) {
    int[] weekDays = { 6, 0, 1, 2, 3, 4, 5 };
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    if (w < 0)
        w = 0;/*from   w ww  .j a  va  2  s.  c  o m*/
    return weekDays[w];
}

From source file:Main.java

public static int getWeek(int year, int month, int day) {
    Date date = new Date(year - 1900, month - 1, day);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
    return dayOfWeek;
}

From source file:Main.java

public static Calendar getCalendar(Date date) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar;
}

From source file:Main.java

public static long getYesterTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    return calendar.getTimeInMillis();
}

From source file:Main.java

public static Date getNextDay(Date presentDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(presentDate);
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    presentDate = calendar.getTime();/*from ww  w . j  av a2s .  c om*/
    return presentDate;
}