Example usage for java.util Calendar setTimeInMillis

List of usage examples for java.util Calendar setTimeInMillis

Introduction

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

Prototype

public void setTimeInMillis(long millis) 

Source Link

Document

Sets this Calendar's current time from the given long value.

Usage

From source file:lu.lippmann.cdb.common.gui.ts.TimeSeriesChartUtil.java

private static void fillWithMultipleAxis(final Instances dataSet, final int dateIdx,
        final TimeSeriesCollection tsDataset, final JFreeChart tsChart) {
    final int numInstances = dataSet.numInstances();

    int axisNumber = 0;
    final Calendar cal = Calendar.getInstance();
    for (final Integer i : WekaDataStatsUtil.getNumericAttributesIndexes(dataSet)) {
        final TimeSeries ts = new TimeSeries(dataSet.attribute(i).name());
        for (int k = 0; k < numInstances; k++) {
            final long timeInMilliSec = (long) dataSet.instance(k).value(dateIdx);
            cal.setTimeInMillis(timeInMilliSec);
            if (dataSet.instance(k).isMissing(i)) {
                ts.addOrUpdate(new Millisecond(cal.getTime()), null);
            } else {
                ts.addOrUpdate(new Millisecond(cal.getTime()), dataSet.instance(k).value(i));
            }/*from w w  w .  j av a  2s .  c om*/
        }
        if (!ts.isEmpty()) {
            if (axisNumber == 0) {
                tsDataset.addSeries(ts);
            } else {
                final XYPlot plot = tsChart.getXYPlot();
                final NumberAxis axisToAdd = new NumberAxis(dataSet.attribute(i).name());
                axisToAdd.setAutoRangeIncludesZero(false);
                plot.setRangeAxis(axisNumber, axisToAdd);
                final TimeSeriesCollection t = new TimeSeriesCollection();
                t.addSeries(ts);
                plot.setDataset(axisNumber, t);
                plot.mapDatasetToRangeAxis(axisNumber, axisNumber);
                final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
                renderer2.setSeriesPaint(0, ColorHelper.getColorForAString(dataSet.attribute(i).name()));
                plot.setRenderer(axisNumber, renderer2);
            }
            axisNumber++;
        }
    }
}

From source file:Dates.java

/**
 * Get the year of a date.//from  ww w.  jav a  2  s.  com
 * 
 * @param when
 *          The date.
 * @param tz
 *          The time zone; if null, the current time zone is assumed.
 * @see #localizedYearOfDate
 */
public static final int yearOfDate(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.

    return cal.get(Calendar.YEAR);
}

From source file:Dates.java

/**
 * Get the month of a date. The first month of the year is JANUARY which is 0.
 * //from ww  w . j  av a  2  s.  com
 * @param when
 *          The date.
 * @param tz
 *          The time zone; if null, the current time zone is assumed.
 */
public static final int monthOfDate(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.

    return cal.get(Calendar.MONTH);
}

From source file:Dates.java

/**
 * Get the month of a date. The first month of the year is JANUARY which is 1.
 * // w w  w.ja  v a2  s  .co  m
 * @param when
 *          The date.
 * @param tz
 *          The time zone; if null, the current time zone is assumed.
 */
public static final int monthOfDatePlus1(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.

    return cal.get(Calendar.MONTH) + 1;
}

From source file:Dates.java

/**
 * Get the day of month of a date. The first day of the month has value 1.
 * // w w  w  . j a  v a 2  s. com
 * @param when
 *          The date.
 * @param tz
 *          The time zone; if null, the current time zone is assumed.
 */
public static final int dayMonthOfDate(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.

    return cal.get(Calendar.DAY_OF_MONTH);
}

From source file:Dates.java

/**
 * Whether the given date in the specified TimeZone is the first day of that
 * month. If TimeZone is null, meaning use default TimeZone of the JVM.
 */// w  ww .  j  a  v  a2  s .c  o  m
final public static boolean isBeginOfMonth(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);
    return day == 1;
}

From source file:Dates.java

/**
 * Given a date, a proper TimeZone, return the two month int. eg. 1, 3, 5, 7,
 * 9, 11. If TimeZone is null, meaning use default TimeZone of the JVM.
 *//*from  w  ww.  j  a  v  a  2s .  com*/
final public static short twoMonthShort(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 month = (cal.get(Calendar.MONTH) / 2) * 2 + 1;
    return (short) month;
}

From source file:Dates.java

/**
 * Whether the given date in the specified TimeZone is the last day of that
 * month. If TimeZone is null, meaning use default TimeZone of the JVM.
 *//*from   w ww  . ja  v a2 s  .  c  om*/
final public static boolean isEndOfMonth(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 maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    return day == maxDay;
}

From source file:Dates.java

/**
 * Date Arithmetic function. Adds the specified (signed) amount of time to the
 * given date, based on the calendar's rules.
 * /* w w  w . jav  a 2 s .  c o m*/
 * @param when
 *          The based date.
 * @param tz
 *          The time zone; if null, the current time zone is assumed.
 * @param field
 *          The time field.
 * @param amount
 *          The amount of date or time to be added to the field.
 */
public static final Date add(Date when, TimeZone tz, int field, int amount) {
    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.

    cal.add(field, amount);
    return cal.getTime();
}

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  w  ww .jav  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();
}