Example usage for java.util Calendar getTimeInMillis

List of usage examples for java.util Calendar getTimeInMillis

Introduction

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

Prototype

public long getTimeInMillis() 

Source Link

Document

Returns this Calendar's time value in milliseconds.

Usage

From source file:backtype.storm.scheduler.adaptive.DataManager.java

public static String formatDate(Calendar c) {
    return dateFormat.format(new Date(c.getTimeInMillis()));
}

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

public static Time newTime(java.util.Date date) {
    if (date == null) {
        return null;
    } else {/*  w  w w  .  jav  a2s. 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:com.greenline.guahao.biz.util.DateUtils.java

public static String getTimeDesc(Date date) {
    if (null == date) {
        return null;
    }/*from   w ww .j  a  v a 2s.com*/
    Calendar now = Calendar.getInstance();
    Calendar time = Calendar.getInstance();
    time.setTime(date);
    // ???
    long t = (now.getTimeInMillis() - time.getTimeInMillis()) / 1000;
    // ?2012-01-08?2012-01-08 09:20
    if ((t < 0) || (t > ((now.get(Calendar.HOUR_OF_DAY) + 24) * 3600 + now.get(Calendar.MINUTE) + 60
            + now.get(Calendar.SECOND))) || (t < 0)) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        return df.format(date);
    }

    // 1?" 09:30"
    if (t > 3600) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        String str = df.format(date);
        int day_time = time.get(Calendar.DAY_OF_YEAR);// 
        int now_time = now.get(Calendar.DAY_OF_YEAR);// ?

        // 1?"11:59"
        if (day_time < now_time) {
            str = " ".concat(str);
        } else {
            str = " ".concat(str);
        }
        return str;
    }
    // 11?"XX?"
    if (t >= 60) {
        return t / 60 + "?";
    }
    // ?1
    return "";
}

From source file:Main.java

public static Calendar getCalendarForTime(int repeatMode, int dayOfWeek, int hour, int minute, int second) {
    Calendar calendarNow = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, second);
    calendar.set(Calendar.MILLISECOND, 0);
    if (repeatMode == 2) {
        int dayOfWeekAndroid = 0; // 7 stands for sunday for interface, but for android, sunday stands for 1.
        dayOfWeekAndroid = dayOfWeek % 7 + 1;
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeekAndroid);
    }//from ww w  . j  av a 2s .c om

    // make sure the desire alarm time is in future.
    int tryCount = 0;
    int tryCountMax = 62;
    while (calendar.getTimeInMillis() < calendarNow.getTimeInMillis() && tryCount < tryCountMax) {
        if (repeatMode == 1) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        } else if (repeatMode == 2) {
            calendar.add(Calendar.DAY_OF_YEAR, 7);
        }
        tryCount++;
    }
    Log.v("cpeng", "getCalendearForTime target info: " + calendar.toString());
    return calendar;
}

From source file:com.clustercontrol.collect.util.CollectDataUtil.java

/**
 * ???? Queue ? put ?//from   ww  w.  j a v a2s  .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:com.krawler.common.timezone.Timezone.java

private static String getDSTSpecificTz(String tzid) {
    Calendar calTZ = Calendar.getInstance(TimeZone.getTimeZone(tzid));
    TimeZone tz = calTZ.getTimeZone();
    int miliseconds = tz.getOffset(calTZ.getTimeInMillis());
    String time = "";
    String signNum = "+";

    if (miliseconds < 0) {
        miliseconds = miliseconds * (-1);
        signNum = "-";
    }/*from  w w w . j  a  v a  2  s. c  o m*/

    int seconds = miliseconds / 1000;

    String min = ""; // Calculating minutes
    if (seconds > 60) {
        min = String.valueOf(seconds / 60 % 60);
        if ((seconds / 60 % 60) < 10) {
            min = "0" + String.valueOf(seconds / 60 % 60);
        }

        if (min.compareTo("0") != 0 && min.compareTo("30") != 0) { // Checking whether the minimum unit of minutes is 30 mins.
            if (min.compareTo("45") == 0) {
                min = "30";

            } else {
                min = "00";
            }
        }

    } else {
        min = "00";
    }

    String hours = ""; // Calculating hours

    if (seconds / 60 > 60) {
        hours = String.valueOf(seconds / 60 / 60);
        if ((seconds / 60 / 60) < 10) {
            hours = "0" + String.valueOf(seconds / 60 / 60);
        }

    } else {
        hours = "00";
    }

    time = signNum + hours + ":" + min;

    return time;
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

public static Timestamp readTimestamp(ByteBuf in) {
    byte length = in.readByte();
    int year = readInt(in);
    byte month = readByte(in);
    byte date = readByte(in);
    int hour = readByte(in);
    int minute = readByte(in);
    int second = readByte(in);
    if (length == 11) {
        long nanos = readUB4(in) * 1000;
        Calendar cal = getLocalCalendar();
        cal.set(year, --month, date, hour, minute, second);
        Timestamp time = new Timestamp(cal.getTimeInMillis());
        time.setNanos((int) nanos);
        return time;
    } else {//from w  ww  .  j  a v a2  s  . co  m
        Calendar cal = getLocalCalendar();
        cal.set(year, --month, date, hour, minute, second);
        return new Timestamp(cal.getTimeInMillis());
    }
}

From source file:net.seratch.taskun.util.CalendarUtil.java

 public static Calendar deepCopy(Calendar src) {
   Calendar dest = Calendar.getInstance();
   dest.setTimeInMillis(src.getTimeInMillis());
   return dest;//from w  ww  .  jav a 2  s  .  c  o m
}

From source file:com.hurence.logisland.utils.DateUtils.java

public static String outputDateInfo(Date d) {
    String output = "";
    Calendar c = GregorianCalendar.getInstance(tz);
    c.setTimeInMillis(d.getTime());//from  w w  w  . j a v  a 2s .c om
    TimeZone tzCal = c.getTimeZone();

    output += "Date:                         " + d + "\n"; // toString uses current system TimeZone
    output += "Date Millis:                  " + d.getTime() + "\n";
    output += "Cal Millis:                   " + c.getTimeInMillis() + "\n";
    output += "Cal To Date Millis:           " + c.getTime().getTime() + "\n";
    output += "Cal TimeZone Name:            " + tzCal.getDisplayName() + "\n";
    output += "Cal TimeZone ID:              " + tzCal.getID() + "\n";
    output += "Cal TimeZone DST Name:        " + tzCal.getDisplayName(true, TimeZone.SHORT) + "\n";
    output += "Cal TimeZone Standard Name:   " + tzCal.getDisplayName(false, TimeZone.SHORT) + "\n";
    output += "In DayLight:                  " + tzCal.inDaylightTime(d) + "\n";

    output += "" + "\n";
    output += "Day Of Month:                 " + c.get(Calendar.DAY_OF_MONTH) + "\n";
    output += "Month Of Year:                " + c.get(Calendar.MONTH) + "\n";
    output += "Year:                         " + c.get(Calendar.YEAR) + "\n";

    output += "Hour Of Day:                  " + c.get(Calendar.HOUR_OF_DAY) + "\n";
    output += "Minute:                       " + c.get(Calendar.MINUTE) + "\n";
    output += "Second:                       " + c.get(Calendar.SECOND) + "\n";

    return output;
}

From source file:Main.java

public static void setupNotificationMessage(AlarmManager am, Calendar calendar, int interval,
        PendingIntent sender) {//  ww  w.j a va  2s  .c o  m
    // official docs:
    // Note: as of API 19, all repeating alarms are inexact. If your application needs precise 
    // delivery times then it must use one-time exact alarms, rescheduling each time as described above. 
    // Legacy applications whosetargetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.
    if (Build.VERSION.SDK_INT >= FAKE_KITKAT_WATCH) {
        //am.setWindow(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20000, sender);
        am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    } else {
        // use this on sdk level 18 and smaller than 18. later sdk won`t guarantee time to be precise.
        //am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, sender);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    }
}