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: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.// w  w  w . j a  v a2 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 ending date of the month of the
 * specified date and TimeZone. If TimeZone is null, meaning use default
 * TimeZone of the JVM.//from w w  w.java 2 s  . c  o m
 */
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: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.//from  w  ww . ja  v  a2 s  .c  o  m
 */
final public static Date endOfMonth(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);
    final int monthDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    cal.clear();
    cal.set(year, month, monthDays + 1);
    cal.setTimeInMillis(cal.getTimeInMillis() - 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.//from   ww  w.  j av  a 2  s .  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();
}

From source file:Dates.java

/**
 * Given a date, a proper TimeZone, return the last millisecond date of the
 * specified date and TimeZone. If TimeZone is null, meaning use Defult
 * TimeZone of the JVM.//from   www .  j ava  2  s.  c o  m
 */
final public static Date endOfDate(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 + 1);
    cal.setTimeInMillis(cal.getTimeInMillis() - 1);

    return cal.getTime();
}

From source file:Dates.java

/**
 * merge the date part and time part of two specified dates into a date.
 * /*from  ww w.ja v  a 2  s  .c o m*/
 * @param datePart
 *          The date part date.
 * @param timePart
 *          The time part date.
 * @param tz
 *          The time zone.
 */
public static final Date merge(Date datePart, Date timePart, TimeZone tz) {
    if (tz == null)
        tz = TimeZones.getCurrent();

    final Calendar dateCal = Calendar.getInstance(tz);
    dateCal.setTimeInMillis(datePart.getTime());// don't call cal.setTime(Date)
                                                // which will reset the
                                                // TimeZone.

    final Calendar timeCal = Calendar.getInstance(tz);
    timeCal.setTimeInMillis(timePart.getTime());// don't call cal.setTime(Date)
                                                // which will reset the
                                                // TimeZone.

    final int hour = timeCal.get(Calendar.HOUR);
    final int minute = timeCal.get(Calendar.MINUTE);
    final int second = timeCal.get(Calendar.SECOND);
    final int msillisecond = timeCal.get(Calendar.MILLISECOND);

    dateCal.set(Calendar.HOUR, hour);
    dateCal.set(Calendar.MINUTE, minute);
    dateCal.set(Calendar.SECOND, second);
    dateCal.set(Calendar.MILLISECOND, msillisecond);

    return dateCal.getTime();
}

From source file:adalid.commons.util.TimeUtils.java

public static Calendar newCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    }// w  w w  .jav a2 s. co  m
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(date.getTime());
    return c;
}

From source file:bizlogic.Records.java

public static void writeCSV(Connection DBcon, String record_id) throws SQLException {

    Statement st;//ww w . j  a  va2s.c om
    ResultSet rs = null;

    System.out.println("WriteCSV started");

    try {
        st = DBcon.createStatement();
        rs = st.executeQuery("SELECT * FROM PUBLIC.t" + record_id);
        System.out.println("Result set read finished");
    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Records.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    }

    try {

        String DELIMITER = ",";
        String NEW_LINE = "\n";
        String FILE_HEADER = "Time,Series";

        System.out.println("Delete old file");
        FileWriter csvFile = new FileWriter("/var/lib/tomcat8/webapps/ROOT/Records/Data/" + record_id + ".csv");

        //BufferedWriter csvFile = new BufferedWriter(
        //        new OutputStreamWriter(new FileOutputStream(new File(
        //                "/var/lib/tomcat8/webapps/ROOT/Records/Data/" + 
        //                        record_id + ".csv"))));

        csvFile.write("");

        csvFile.append(FILE_HEADER);
        csvFile.append(NEW_LINE);

        Calendar calendar = new GregorianCalendar();

        System.out.println("Writing file...");
        while (rs.next()) {

            long time_stamp = rs.getLong("time");
            double value = rs.getDouble("value");
            String _year;
            String _month;
            String _day;
            String _hour;
            String _min;
            String _sec;

            calendar.setTimeInMillis(time_stamp);

            _year = Integer.toString(calendar.get(Calendar.YEAR));
            _month = Integer.toString(calendar.get(Calendar.MONTH) + 1);
            _day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
            _hour = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY));
            _min = Integer.toString(calendar.get(Calendar.MINUTE));
            _sec = Integer.toString(calendar.get(Calendar.SECOND));

            csvFile.append(_year + "/" + _month + "/" + _day + " " + _hour + ":" + _min + ":" + _sec + DELIMITER
                    + Double.toString(value) + NEW_LINE); //new Date("2009/07/19 12:34:56")

        }
        System.out.print("File written");
        rs.close();
        //csvFile.flush();
        csvFile.close();
    }

    catch (IOException ex) {
        Logger.getLogger(Records.class.getName()).log(Level.WARNING, null, ex);
    }
}

From source file:adalid.commons.util.TimeUtils.java

public static synchronized Time getTime(java.util.Date date) {
    if (date == null) {
        return currentTime();
    }/* w  ww .  j av a2s  . co 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:adalid.commons.util.TimeUtils.java

public static Calendar newTimeCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    }/*  w ww  .j  av  a 2  s .  co  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 c;
}