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:com.clustercontrol.collect.util.CollectDataUtil.java

/**
 * ???? Queue ? put ?/*from  w w  w .j ava2  s .c  om*/
 *
 * @param sample
 *            
 * @throws HinemosUnknown
 * @throws Exception
 */
public static void put(List<Sample> sampleList) {
    m_log.debug("put() start");

    List<CollectData> collectdata_entities = new ArrayList<CollectData>();
    List<SummaryHour> summaryhour_entities = new ArrayList<SummaryHour>();
    List<SummaryDay> summaryday_entities = new ArrayList<SummaryDay>();
    List<SummaryMonth> summarymonth_entities = new ArrayList<SummaryMonth>();
    JpaTransactionManager jtm = new JpaTransactionManager();
    jtm.begin();

    for (Sample sample : sampleList) {
        // for debug
        if (m_log.isDebugEnabled()) {
            m_log.debug("put() dateTime = " + sample.getDateTime());
            ArrayList<PerfData> list = sample.getPerfDataList();
            for (PerfData data : list) {
                m_log.info("put() list facilityId = " + data.getFacilityId() + ", value = " + data.getValue());
            }
        }

        ArrayList<PerfData> list = sample.getPerfDataList();
        String monitorId = sample.getMonitorId();

        Long time = HinemosTime.currentTimeMillis();
        for (PerfData data : list) {
            m_log.debug("persist itemCode = " + data.getItemName());
            String itemName = data.getItemName();
            String facilityId = data.getFacilityId();
            String displayName = data.getDisplayName();
            CollectData collectData = null;
            SummaryHour summaryHour_c = null;
            SummaryDay summaryDay_c = null;
            SummaryMonth summaryMonth_c = null;

            Integer collectorid = getId(itemName, displayName, monitorId, facilityId, jtm);
            CollectDataPK pk = new CollectDataPK(collectorid, time);
            Float value = null;
            if (data.getValue() != null) {
                value = Float.parseFloat(data.getValue().toString());
            }
            collectData = new CollectData(pk, value);
            collectdata_entities.add(collectData);

            // Summary??
            // ??????
            Long hour = (time + TIMEZONE) / 1000 / 3600 * 3600 * 1000 - TIMEZONE;
            // ???????
            Long day = (time + TIMEZONE) / 1000 / 3600 / 24 * 24 * 3600 * 1000 - TIMEZONE;
            // ???
            Calendar calendar = HinemosTime.getCalendarInstance();
            calendar.setTimeInMillis(time);
            int y = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH);
            calendar.clear();
            calendar.set(y, m, 1);
            Long month = calendar.getTimeInMillis();

            // SummaryHour
            CollectDataPK pk_h = new CollectDataPK(collectorid, hour);
            try {
                SummaryHour summaryHour = QueryUtil.getSummaryHour(pk_h);
                summaryHour_c = summaryHour.clone();
                Integer h_count = summaryHour_c.getCount();
                if (value != null) {
                    summaryHour_c.setAvg((value + summaryHour_c.getAvg() * h_count) / (h_count + 1));
                    summaryHour_c.setMin(summaryHour_c.getMin() < value ? summaryHour_c.getMin() : value);
                    summaryHour_c.setMax(summaryHour_c.getMax() > value ? summaryHour_c.getMax() : value);
                } else {
                    summaryHour_c.setAvg(summaryHour_c.getAvg() * h_count / (h_count + 1));
                    summaryHour_c.setMin(summaryHour_c.getMin());
                    summaryHour_c.setMax(summaryHour_c.getMax());
                }
                summaryHour_c.setCount(h_count + 1);
            } catch (CollectKeyNotFound e) {
                summaryHour_c = new SummaryHour(pk_h, value, value, value, 1);
            }

            // SummaryDay
            CollectDataPK pk_d = new CollectDataPK(collectorid, day);
            try {
                SummaryDay summaryDay = QueryUtil.getSummaryDay(pk_d);
                summaryDay_c = summaryDay.clone();
                Integer d_count = summaryDay_c.getCount();
                if (value != null) {
                    summaryDay_c.setAvg((value + summaryDay_c.getAvg() * d_count) / (d_count + 1));
                    summaryDay_c.setMin(summaryDay_c.getMin() < value ? summaryDay_c.getMin() : value);
                    summaryDay_c.setMax(summaryDay_c.getMax() > value ? summaryDay_c.getMax() : value);
                } else {
                    summaryDay_c.setAvg(summaryDay_c.getAvg() * d_count / (d_count + 1));
                    summaryDay_c.setMin(summaryDay_c.getMin());
                    summaryDay_c.setMax(summaryDay_c.getMax());
                }
                summaryDay_c.setCount(d_count + 1);

            } catch (CollectKeyNotFound e) {
                summaryDay_c = new SummaryDay(pk_d, value, value, value, 1);
            }

            // SummaryMonth
            CollectDataPK pk_m = new CollectDataPK(collectorid, month);
            try {
                SummaryMonth summaryMonth = QueryUtil.getSummaryMonth(pk_m);
                summaryMonth_c = summaryMonth.clone();
                Integer m_count = summaryMonth_c.getCount();
                if (value != null) {
                    summaryMonth_c.setAvg((value + summaryMonth_c.getAvg() * m_count) / (m_count + 1));
                    summaryMonth_c.setMin(summaryMonth_c.getMin() < value ? summaryMonth_c.getMin() : value);
                    summaryMonth_c.setMax(summaryMonth_c.getMax() > value ? summaryMonth_c.getMax() : value);
                } else {
                    summaryMonth_c.setAvg(summaryMonth_c.getAvg() * m_count / (m_count + 1));
                    summaryMonth_c.setMin(summaryMonth_c.getMin());
                    summaryMonth_c.setMax(summaryMonth_c.getMax());
                }
                summaryMonth_c.setCount(m_count + 1);
            } catch (CollectKeyNotFound e) {
                summaryMonth_c = new SummaryMonth(pk_m, value, value, value, 1);
            }

            summaryhour_entities.add(summaryHour_c);
            summaryday_entities.add(summaryDay_c);
            summarymonth_entities.add(summaryMonth_c);
        }
        m_log.debug("insert() end : dateTime = " + sample.getDateTime());
    }
    jtm.commit();
    List<JdbcBatchQuery> query = new ArrayList<JdbcBatchQuery>();
    // ()?
    if (!collectdata_entities.isEmpty()) {
        query.add(new CollectDataJdbcBatchInsert(collectdata_entities));
    }
    if (!summaryhour_entities.isEmpty()) {
        query.add(new SummaryHourJdbcBatchUpsert(summaryhour_entities));
    }
    if (!summaryday_entities.isEmpty()) {
        query.add(new SummaryDayJdbcBatchUpsert(summaryday_entities));
    }
    if (!summarymonth_entities.isEmpty()) {
        query.add(new SummaryMonthJdbcBatchUpsert(summarymonth_entities));
    }
    JdbcBatchExecutor.execute(query);
    jtm.close();
    m_log.debug("put() end");
}

From source file:Main.java

public static String getTimeMillisToFullDate(long milliSeconds) {
    Calendar cal = Calendar.getInstance();
    java.util.Date currentTime = cal.getTime();
    // Create a DateFormatter object for displaying date in specified format.
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // String ndate = formatter.format(currentTime);
    // Create a calendar object that will convert the date and time value in milliseconds to date. 
    cal.setTimeInMillis(milliSeconds);
    return formatter.format(cal.getTime());
}

From source file:Main.java

/**
 * Takes a calendar object representing a date/time, ignores its current time zone (which should be the default time zone)
 * applies that date/time to the sourceTimeZone and returns the relative date/time in the current time zone. 
 * //  w  w  w .jav  a2 s .c  om
 * For example, given an input of 13:00 EST and source time zone PST, it will return 16:00 EST 
 * 13:00 EST = 18:00 GMT = 10:00 PST
 *  
 * @param calendar
 * @param sourceTimeZone
 * @return
 */
public static Calendar convertToTimeZone(Calendar calendar, TimeZone sourceTimeZone) {
    Calendar result = Calendar.getInstance();
    // i.e., 13:00 EST becomes 08:00 GMT
    long originalTimeInUtc = calendar.getTimeInMillis()
            + calendar.getTimeZone().getOffset(calendar.getTimeInMillis());
    // 08:00 GMT becomes 16:00 PST
    long sourceTime = originalTimeInUtc - sourceTimeZone.getOffset(originalTimeInUtc);
    result.setTimeZone(sourceTimeZone);
    result.setTimeInMillis(sourceTime);
    /*
    Log.d(TAG, "Converting "+DEBUG_CALENDAR_OUTPUT.format(new Date(calendar.getTimeInMillis()))
        +" in ["+sourceTimeZone.getDisplayName()+"] to ["+TimeZone.getDefault().getDisplayName()
        +"] resulting in "+DEBUG_CALENDAR_OUTPUT_WITH_TIMEZONE.format(new Date(result.getTimeInMillis())));
    Log.d(TAG, "Original time in UTC = "+DEBUG_CALENDAR_OUTPUT.format(new Date(originalTimeInUtc)));
    Log.d(TAG, "Original time in source time zone = "+DEBUG_CALENDAR_OUTPUT.format(new Date(sourceTime)));
     */
    return result;
}

From source file:Main.java

public static String getTimeMillisToFullDate2(long milliSeconds) {
    Calendar cal = Calendar.getInstance();
    java.util.Date currentTime = cal.getTime();
    // Create a DateFormatter object for displaying date in specified format.
    SimpleDateFormat formatter = new SimpleDateFormat(FORMATE_YYYY_MM_DDHHMMSS_FULL);
    // String ndate = formatter.format(currentTime);
    // Create a calendar object that will convert the date and time value in milliseconds to date. 
    cal.setTimeInMillis(milliSeconds);
    return formatter.format(cal.getTime());
}

From source file:de.fahrgemeinschaft.FahrgemeinschaftConnector.java

public static long getNextDayMorning(long dep) {
    if (dep != 0) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(dep + 25 * 3600000); // plus one(!) day
        c.set(Calendar.HOUR_OF_DAY, 0); // reset
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis();
    } else/*from ww w . j  a v  a  2s  . c  o  m*/
        return 0;
}

From source file:ips1ap101.lib.base.util.TimeUtils.java

public static Time newTime(java.util.Date date) {
    if (date == null) {
        return null;
    } else {/*from  w  w w  . j  av  a 2 s .c o m*/
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date.getTime());
        c.set(Calendar.YEAR, 1970);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return new Time(c.getTimeInMillis());
    }
}

From source file:ips1ap101.lib.base.util.TimeUtils.java

public static Date newDate(java.util.Date date) {
    if (date == null) {
        return null;
    } else {//from   w  ww .jav  a  2s  . com
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date.getTime());
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return new Date(c.getTimeInMillis());
    }
}

From source file:com.sccl.attech.common.utils.DateUtils.java

/**
 * ???/*w  ww .j a v  a  2  s .  c  o  m*/
 * @param time
 * @return
 */
public static Date getDateByTime(Long time) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(time);
    Date date = c.getTime();
    return date;
}

From source file:com.sccl.attech.common.utils.DateUtils.java

/**
 * ??? 00//from  w w  w  .  j a va  2s. c  om
 * @param time
 * @return
 */
public static Date getDateByMinLow(Long time) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(time);
    Date date = c.getTime();
    date.setSeconds(0);
    return date;
}

From source file:com.sccl.attech.common.utils.DateUtils.java

/**
 * ??? 59//from w  ww  .  jav  a2 s .c o  m
 * @param time
 * @return
 */
public static Date getDateByMinUp(Long time) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(time);
    Date date = c.getTime();
    date.setSeconds(59);
    return date;
}