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 String getDateByAddHour(String datetime, int minute) {
    String returnTime = null;/*from  w  w w  .  j a  va 2  s .c  o m*/
    Calendar cal = new GregorianCalendar();
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
        date = ft.parse(datetime);
        cal.setTime(date);
        cal.add(GregorianCalendar.MINUTE, minute);
        returnTime = getFormatDateTime(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return returnTime;

}

From source file:Main.java

/**
 * Convert the given Date object to a Calendar instance.
 *
 * @param date/*w w  w .  j  a va  2  s.  c  o m*/
 *            The Date object.
 * @return The Calendar instance.
 */
private static Calendar dateToCalendar(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal;
}

From source file:com.b5m.user.frame.util.DateUtils.java

public static String getBeforeOrAfterDay(Date date, int daycount) {
    if (daycount != 0) {
        Calendar calendar = Calendar.getInstance(); //
        calendar.setTime(date);//?
        calendar.add(Calendar.DAY_OF_MONTH, daycount); //  ? + dayCount
        date = calendar.getTime(); //? + dayCount 
    }/*from w  w w. j  a va 2 s  .  c o m*/
    return Date2String(date);
}

From source file:com.teamsun.framework.util.DateUtil.java

/**
 * This method returns the current date in the format: yyyy-MM-dd
 * //from  w w  w .  j a  va  2  s .c o m
 * @return the current date
 * @throws ParseException
 */
public static Calendar getToday() throws ParseException {
    Date today = new Date();
    SimpleDateFormat df = new SimpleDateFormat(datePattern);

    // This seems like quite a hack (date -> string -> date),
    // but it works ;-)
    String todayAsString = df.format(today);
    Calendar cal = new GregorianCalendar();
    cal.setTime(convertStringToDate(todayAsString));

    return cal;
}

From source file:com.b5m.user.frame.util.DateUtils.java

/**
 * ? ?  ? daycount /*  w w  w . j ava2s  . c  om*/
 * @param daycount
 * @return
 */
public static String getBeforeOrAfterDay(int daycount) {
    Date dayBefore = new Date();
    if (daycount != 0) {
        Calendar calendar = Calendar.getInstance(); //
        calendar.setTime(new Date());//?
        calendar.add(Calendar.DAY_OF_MONTH, daycount); //  ? + dayCount
        dayBefore = calendar.getTime(); //? + dayCount 
    }
    return Date2String(dayBefore);
}

From source file:org.musicrecital.util.DateUtil.java

/**
 * This method returns the current date in the format: MM/dd/yyyy
 *
 * @return the current date/*from www .  ja v  a 2  s . c  om*/
 * @throws ParseException when String doesn't match the expected format
 */
public static Calendar getToday() throws ParseException {
    Date today = new Date();
    SimpleDateFormat df = new SimpleDateFormat(getDatePattern());

    // This seems like quite a hack (date -> string -> date),
    // but it works ;-)
    String todayAsString = df.format(today);
    Calendar cal = new GregorianCalendar();
    cal.setTime(convertStringToDate(todayAsString));

    return cal;
}

From source file:DateUtil.java

public static String formatHTTPDate(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("GMT+0"));
    cal.setTime(date);
    return DAY_OF_WEEK[cal.get(Calendar.DAY_OF_WEEK) - 1] + ", " + cal.get(Calendar.DAY_OF_MONTH) + " "
            + MONTH[cal.get(Calendar.MONTH)] + " " + cal.get(Calendar.YEAR) + " "
            + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND)
            + " GMT";
    //Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
}

From source file:com.gatf.executor.core.GatfFunctionHandler.java

public static String handleFunction(String function) {
    if (function.equals(BOOLEAN)) {
        Random rand = new Random();
        return String.valueOf(rand.nextBoolean());
    } else if (function.matches(DT_FMT_REGEX)) {
        Matcher match = datePattern.matcher(function);
        match.matches();//  www .j  a v  a 2  s . co  m
        SimpleDateFormat format = new SimpleDateFormat(match.group(1));
        return format.format(new Date());
    } else if (function.matches(DT_FUNC_FMT_REGEX)) {
        Matcher match = specialDatePattern.matcher(function);
        match.matches();
        String formatStr = match.group(1);
        String operation = match.group(2);
        int value = Integer.valueOf(match.group(3));
        String unit = match.group(4);
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            Date dt = format.parse(format.format(new Date()));
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);

            value = (operation.equals(MINUS) ? -value : value);
            if (unit.equals(YEAR)) {
                cal.add(Calendar.YEAR, value);
            } else if (unit.equals(MONTH)) {
                cal.add(Calendar.MONTH, value);
            } else if (unit.equals(DAY)) {
                cal.add(Calendar.DAY_OF_YEAR, value);
            } else if (unit.equals(HOUR)) {
                cal.add(Calendar.HOUR_OF_DAY, value);
            } else if (unit.equals(MINUITE)) {
                cal.add(Calendar.MINUTE, value);
            } else if (unit.equals(SECOND)) {
                cal.add(Calendar.SECOND, value);
            } else if (unit.equals(MILLISECOND)) {
                cal.add(Calendar.MILLISECOND, value);
            }
            return format.format(cal.getTime());
        } catch (Exception e) {
            throw new AssertionError("Invalid date format specified - " + formatStr);
        }
    } else if (function.equals(FLOAT)) {
        Random rand = new Random(12345678L);
        return String.valueOf(rand.nextFloat());
    } else if (function.equals(ALPHA)) {
        return RandomStringUtils.randomAlphabetic(10);
    } else if (function.equals(ALPHANUM)) {
        return RandomStringUtils.randomAlphanumeric(10);
    } else if (function.equals(NUMBER_PLUS)) {
        Random rand = new Random();
        return String.valueOf(rand.nextInt(1234567));
    } else if (function.equals(NUMBER_MINUS)) {
        Random rand = new Random();
        return String.valueOf(-rand.nextInt(1234567));
    } else if (function.equals(NUMBER)) {
        Random rand = new Random();
        boolean bool = rand.nextBoolean();
        return bool ? String.valueOf(rand.nextInt(1234567)) : String.valueOf(-rand.nextInt(1234567));
    } else if (function.matches(RANDOM_RANGE_REGEX)) {
        Matcher match = randRangeNum.matcher(function);
        match.matches();
        String min = match.group(1);
        String max = match.group(2);
        try {
            int nmin = Integer.parseInt(min);
            int nmax = Integer.parseInt(max);
            return String.valueOf(nmin + (int) (Math.random() * ((nmax - nmin) + 1)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:jp.co.opentone.bsol.framework.core.util.DateUtil.java

/**
 * ?????????./*w w w . ja v a  2s.com*/
 * @param deadline ?
 * @return true:?
 */
public static boolean isExpire(Date deadline) {
    if (deadline == null) {
        return false;
    }
    // ?+1?
    Calendar cal = Calendar.getInstance();
    cal.setTime(deadline);
    cal.add(Calendar.DATE, 1);
    return cal.getTime().before(getNow());
}

From source file:com.qdum.llhb.common.utils.DateUtils.java

/**
 * ?//from  www . j  a  v  a  2  s  .c  o  m
 *
 * @param d
 * @param day
 * @return
 */
public static Date getDateBefore(Date d, int day) {
    Calendar now = Calendar.getInstance();
    now.setTime(d);
    now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
    return now.getTime();
}