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 Calendar parseTimestamp(String timestamp) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.US);
    Date d = sdf.parse(timestamp);
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal;/*from w  w w . j  a  v a 2 s  .co  m*/
}

From source file:Main.java

public static Date getTheEndOfDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 999);

    return calendar.getTime();
}

From source file:Main.java

public static Date getTheBeginingOfDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    return calendar.getTime();
}

From source file:Main.java

public static LinkedHashMap<String, Integer> getDateMap(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    LinkedHashMap<String, Integer> datemap = new LinkedHashMap<String, Integer>();
    datemap.put("year", cal.get(Calendar.YEAR));
    datemap.put("month", cal.get(Calendar.MONTH));
    datemap.put("day", cal.get(Calendar.DAY_OF_MONTH));
    datemap.put("hour", cal.get(Calendar.HOUR_OF_DAY));
    datemap.put("minute", cal.get(Calendar.MINUTE));
    datemap.put("second", cal.get(Calendar.SECOND));
    datemap.put("millisecond", cal.get(Calendar.MILLISECOND));

    return datemap;

}

From source file:Main.java

public static Date trim(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(HOUR_OF_DAY, 0);/*w w w .  jav a2 s.  com*/
    cal.set(MINUTE, 0);
    cal.set(SECOND, 0);
    cal.set(MILLISECOND, 0);
    return cal.getTime();
}

From source file:Main.java

public static String previousDateString(String dateString) throws ParseException {
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    Date myDate = dateFormat.parse(dateString);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(myDate);
    calendar.add(Calendar.DAY_OF_YEAR, -1);

    Date previousDate = calendar.getTime();
    String result = dateFormat.format(previousDate);

    return result;
}

From source file:Main.java

public static long setTimeToMidnightAndGet(Calendar cal, long epoch) {
    cal.setTime(new Date(epoch));
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);/*w  ww.  j ava 2 s. co  m*/
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}

From source file:Main.java

public static int getCurrentYear() {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    return c.get(Calendar.YEAR);
}

From source file:Main.java

public static int getMonthFromDate(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    return c.get(Calendar.MONTH);
}

From source file:Main.java

public static int getYearFromDate(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    return c.get(Calendar.YEAR);
}