Example usage for java.util Calendar clear

List of usage examples for java.util Calendar clear

Introduction

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

Prototype

public final void clear() 

Source Link

Document

Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.

Usage

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static Date getFirstDayOfWeek(Date dateInWeek) {
    Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"), Locale.GERMANY);
    cal1.setTime(dateInWeek);/*from   www .  java  2 s.  c o m*/
    int week = cal1.get(Calendar.WEEK_OF_YEAR);
    int year = cal1.get(Calendar.YEAR);
    cal1.clear();
    cal1.set(Calendar.WEEK_OF_YEAR, week);
    cal1.set(Calendar.YEAR, year);
    //      cal1.add(Calendar.DATE, 1);//To make the week start on monday. Not needed when locale is set as above?

    return cal1.getTime();
}

From source file:org.apache.lens.cube.parse.DateUtil.java

public static CoveringInfo getWeeklyCoveringInfo(Date from, Date to) {
    int dayDiff = 0;
    Date tmpFrom = from;//from   ww  w.j a  v a  2s.c om
    while (tmpFrom.before(to)) {
        tmpFrom = DateUtils.addDays(tmpFrom, 1);
        dayDiff++;
    }

    if (dayDiff < 7) {
        return new CoveringInfo(0, false);
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(from);
    int fromWeek = cal.get(Calendar.WEEK_OF_YEAR);
    int fromDay = cal.get(Calendar.DAY_OF_WEEK);
    int fromYear = cal.get(YEAR);

    cal.clear();
    cal.set(YEAR, fromYear);
    cal.set(Calendar.WEEK_OF_YEAR, fromWeek);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    int maxDayInWeek = cal.getActualMaximum(Calendar.DAY_OF_WEEK);
    Date fromWeekStartDate = cal.getTime();
    boolean coverable = dayDiff % 7 == 0;
    if (fromWeekStartDate.before(from)) {
        // Count from the start of next week
        dayDiff -= (maxDayInWeek - (fromDay - Calendar.SUNDAY));
        coverable = false;
    }

    return new CoveringInfo(dayDiff / 7, coverable);
}

From source file:com.adito.activedirectory.ActiveDirectoryUserDatabaseConfiguration.java

/**
 * Converts an Active Directory long value into a
 * <code>java.util.Date</code>.
 * //from ww  w . ja  v a2  s  .c  om
 * @param timeStamp the time to convert
 * @return the <code>java.util.Date</code> representing the long
 */
public static Date adTimeToJavaDate(long timeStamp) {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(1601, 0, 1, 0, 0);
    timeStamp = timeStamp / 10000 + calendar.getTime().getTime();
    return new Date(timeStamp);
}

From source file:com.aurel.track.report.dashboard.AverageTimeToCloseItem.java

/**
 * Created a regular time period//from  w w  w  . j av a 2 s .c o m
 * @param period
 * @param year
 * @param timeInterval
 * @return
 */
public static Date createDate(int period, int year, int timeInterval) {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.setLenient(true);
    calendar.set(Calendar.YEAR, year);
    switch (timeInterval) {
    case TIME_INTERVAL.DAY:
        calendar.set(Calendar.DAY_OF_YEAR, period);
        break;
    case TIME_INTERVAL.WEEK:
        calendar.set(Calendar.WEEK_OF_YEAR, period);
        break;
    default:
        calendar.set(Calendar.MONTH, period);
    }
    return calendar.getTime();
}

From source file:org.teiid.resource.adapter.google.dataprotocol.GoogleDataProtocolAPI.java

static Object convertValue(Calendar cal, Object object, SpreadsheetColumnType type) {
    switch (type) {
    case DATE:/* w w  w.  jav  a  2 s. c om*/
    case DATETIME:
        if (object instanceof String) {
            String stringVal = (String) object;
            if (stringVal.startsWith("Date(") && stringVal.endsWith(")")) { //$NON-NLS-1$ //$NON-NLS-2$
                String[] parts = stringVal.substring(5, stringVal.length() - 1).split(","); //$NON-NLS-1$
                if (cal == null) {
                    cal = Calendar.getInstance();
                }
                cal.clear();
                if (type == SpreadsheetColumnType.DATETIME) {
                    cal.set(Integer.valueOf(parts[0]), Integer.valueOf(parts[1]), Integer.valueOf(parts[2]),
                            Integer.valueOf(parts[3]), Integer.valueOf(parts[4]), Integer.valueOf(parts[5]));
                    object = new Timestamp(cal.getTimeInMillis());
                } else {
                    cal.set(Integer.valueOf(parts[0]), Integer.valueOf(parts[1]), Integer.valueOf(parts[2]));
                    object = new Date(cal.getTimeInMillis());
                }
            }
        }
        break;
    case TIMEOFDAY:
        if (object instanceof List<?>) {
            List<Double> doubleVals = (List<Double>) object;
            if (cal == null) {
                cal = Calendar.getInstance();
            }
            cal.clear();
            cal.set(Calendar.YEAR, 1970);
            cal.set(Calendar.MONTH, Calendar.JANUARY);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.MILLISECOND, 0);
            cal.set(Calendar.HOUR, doubleVals.get(0).intValue());
            cal.set(Calendar.MINUTE, doubleVals.get(1).intValue());
            cal.set(Calendar.SECOND, doubleVals.get(2).intValue());
            //TODO: it's not proper to convey the millis on a time value
            cal.set(Calendar.MILLISECOND, doubleVals.get(3).intValue());
            object = new Time(cal.getTimeInMillis());
        }
        break;
    }
    return object;
}

From source file:Dates.java

/**
 * Given a date, a proper TimeZone, return the beginning date of the month of
 * the specified date and TimeZone. If TimeZone is null, meaning use default
 * TimeZone of the JVM.//  www . j  a v  a  2  s . c om
 */
final public static Date beginOfYear(Date when, TimeZone tz) {
    if (tz == null)
        tz = TimeZones.getCurrent();
    final Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(when.getTime()); // don't call cal.setTime(Date) which
                                         // will reset the TimeZone.

    final int year = cal.get(Calendar.YEAR);
    cal.clear();
    cal.set(year, Calendar.JANUARY, 1);
    return cal.getTime();
}

From source file:Dates.java

/**
 * Given a date, a proper TimeZone, return the ending date of the month of the
 * specified date and TimeZone. If TimeZone is null, meaning use default
 * TimeZone of the JVM.//ww w  .j  a  va 2s . c  om
 */
final public static Date endOfYear(Date when, TimeZone tz) {
    if (tz == null)
        tz = TimeZones.getCurrent();
    final Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(when.getTime()); // don't call cal.setTime(Date) which
                                         // will reset the TimeZone.

    final int year = cal.get(Calendar.YEAR);
    cal.clear();
    cal.set(year + 1, Calendar.JANUARY, 1);
    cal.setTimeInMillis(cal.getTimeInMillis() - 1);
    return cal.getTime();
}

From source file:com.sammyun.util.DateUtil.java

/**
 * ??_?yyyy-MM-dd/*  w  w w  .j  ava 2  s .c o m*/
 * 
 * @param year 
 * @return String
 */
public static String getCurrYearFirstDay(int year) {
    SimpleDateFormat format = new SimpleDateFormat(default_format);
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.YEAR, year);
    Date currYearFirst = calendar.getTime();
    return format.format(currYearFirst);
}

From source file:Dates.java

/**
 * Given a date, a proper TimeZone, return the beginning date of the month of
 * the specified date and TimeZone. If TimeZone is null, meaning use default
 * TimeZone of the JVM./*from  www .j ava 2  s.  com*/
 */
final public static Date beginOfMonth(Date when, TimeZone tz) {
    if (tz == null)
        tz = TimeZones.getCurrent();
    final Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(when.getTime()); // don't call cal.setTime(Date) which
                                         // will reset the TimeZone.

    final int year = cal.get(Calendar.YEAR);
    final int month = cal.get(Calendar.MONTH);
    cal.clear();
    cal.set(year, month, 1);
    return cal.getTime();
}

From source file:Dates.java

/**
 * Given a date, a proper TimeZone, return the beginning date of the specified
 * date and TimeZone. If TimeZone is null, meaning use Defult TimeZone of the
 * JVM.// ww w .  j ava  2s .  c  o m
 */
final public static Date beginOfDate(Date when, TimeZone tz) {
    if (tz == null)
        tz = TimeZones.getCurrent();

    final Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(when.getTime());// don't call cal.setTime(Date) which
                                        // will reset the TimeZone.
    final int day = cal.get(Calendar.DAY_OF_MONTH);
    final int year = cal.get(Calendar.YEAR);
    final int month = cal.get(Calendar.MONTH);
    cal.clear();
    cal.set(year, month, day);

    return cal.getTime();
}