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.alkacon.opencms.documentcenter.NewDocumentsTree.java

/**
 * Builds the HTML code for a select box of days.<p>
 * /*w  w w. ja v  a2  s. c  o m*/
 * @param timeMillis the time in milliseconds which should be preselected
 * @param fieldName the prefix of the name attribute
 * @param attributes optional additional attributes of the select tag
 * @param localeString the current locale String
 * @return the HTML code for a select box of days
 */
public static String buildSelectDay(long timeMillis, String fieldName, String attributes, String localeString) {

    StringBuffer retValue = new StringBuffer(512);
    Locale locale = new Locale(localeString);
    Calendar cal = new GregorianCalendar(locale);
    cal.setTimeInMillis(timeMillis);

    retValue.append("<select name=\"" + fieldName + "day\"");
    if (attributes != null) {
        retValue.append(" " + attributes);
    }
    retValue.append(">\n");
    for (int i = 1; i < 32; i++) {
        retValue.append("\t<option value=\"" + i + "\"");
        if (cal.get(Calendar.DAY_OF_MONTH) == i) {
            retValue.append(" selected=\"selected\"");
        }
        retValue.append(">" + i + "</option>\n");
    }
    retValue.append("</select>\n");

    return retValue.toString();
}

From source file:com.appassit.common.Utils.java

public static String formatDate(long time) {
    if (calendar == null || calendar.get() == null) {
        calendar = new WeakReference<Calendar>(Calendar.getInstance());
    }// www .  ja  v a2s .com
    Calendar target = calendar.get();
    target.setTimeInMillis(time);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(target.getTime());
}

From source file:com.rsegismont.androlife.core.utils.AndrolifeUtils.java

public static long getTonightIndex(Cursor mCursor) {
    if (mCursor == null)
        return -1;

    if (mCursor.getCount() <= 0)
        return -1;

    long dateUtc = -1;

    final int initialPosition = mCursor.getPosition();

    mCursor.moveToPosition(0);//  w  w  w. j a va 2  s .  c o  m

    final long initialValue = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));

    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(initialValue);
    calendar.set(Calendar.HOUR_OF_DAY, 19);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    for (int i = 0; i < mCursor.getCount(); i++) {
        mCursor.moveToPosition(i);
        final long value = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));

        if (calendar.getTime().getTime() - value < 0) {

            mCursor.moveToPosition(Math.max(0, i - 1));
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }
        if ((i == 0) && (calendar.getTime().getTime() - value < 0)) {

            mCursor.moveToPosition(0);
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }

        if (i == (mCursor.getCount() - 1)) {

            mCursor.moveToPosition(Math.max(0, mCursor.getCount() - 1));
            dateUtc = mCursor.getLong(mCursor.getColumnIndex(DatabaseColumn.DATE_UTC.stringValue));
            break;
        }
    }

    if (initialPosition >= 0)
        mCursor.moveToPosition(initialPosition);

    return dateUtc;
}

From source file:com.alkacon.opencms.documentcenter.NewDocumentsTree.java

/**
 * Builds the HTML code for a select box of years.<p>
 * //from  w w  w .  j  a  va2  s. c  om
 * @param timeMillis the time in milliseconds which should be preselected
 * @param fieldName the prefix of the name attribute
 * @param attributes optional additional attributes of the select tag
 * @param localeString the current locale String
 * @param startyear the year to start with
 * @param endyear the last year to display in the selection
 * @return the HTML code for a select box of years
 */
public static String buildSelectYear(long timeMillis, String fieldName, String attributes, String localeString,
        int startyear, int endyear) {

    StringBuffer retValue = new StringBuffer(512);
    Locale locale = new Locale(localeString);
    Calendar cal = new GregorianCalendar(locale);
    cal.setTimeInMillis(timeMillis);

    if (startyear > endyear) {
        startyear = endyear;
    }

    retValue.append("<select name=\"" + fieldName + "year\"");
    if (attributes != null) {
        retValue.append(" " + attributes);
    }
    retValue.append(">\n");
    for (int i = startyear; i <= endyear; i++) {
        retValue.append("\t<option value=\"" + i + "\"");
        if (cal.get(Calendar.YEAR) == i) {
            retValue.append(" selected=\"selected\"");
        }
        retValue.append(">" + i + "</option>\n");
    }
    retValue.append("</select>\n");

    return retValue.toString();
}

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;//  w w  w .j  a v  a 2 s .c  o m
}

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

 public static Calendar getCalendar(long timeInMillis) {
   Calendar dest = Calendar.getInstance();
   dest.setTimeInMillis(timeInMillis);
   return dest;/*from ww  w  .j a va2 s. c o  m*/
}

From source file:com.alkacon.opencms.documentcenter.NewDocumentsTree.java

/**
 * Builds the HTML code for a select box of months.<p>
 * /*from   www .j  a  v a2 s  .c om*/
 * @param timeMillis the time in milliseconds which should be preselected
 * @param fieldName the prefix of the name attribute
 * @param attributes optional additional attributes of the select tag
 * @param localeString the current locale String
 * @return the HTML code for a select box of months
 */
public static String buildSelectMonth(long timeMillis, String fieldName, String attributes,
        String localeString) {

    StringBuffer retValue = new StringBuffer(512);
    Locale locale = new Locale(localeString);
    Calendar cal = new GregorianCalendar(locale);
    cal.setTimeInMillis(timeMillis);
    Calendar calTemp = new GregorianCalendar(locale);
    calTemp.setTimeInMillis(timeMillis);
    // set day to 2 to avoid display errors for days 29, 30 and 31
    calTemp.set(Calendar.DAY_OF_MONTH, 2);
    DateFormat df = new SimpleDateFormat("MMMM", locale);

    retValue.append("<select name=\"" + fieldName + "month\"");
    if (attributes != null) {
        retValue.append(" " + attributes);
    }
    retValue.append(">\n");
    for (int i = 0; i < 12; i++) {
        calTemp.set(Calendar.MONTH, i);
        retValue.append("\t<option value=\"" + (i + 1) + "\"");
        if (cal.get(Calendar.MONTH) == i) {
            retValue.append(" selected=\"selected\"");
        }
        retValue.append(">" + df.format(calTemp.getTime()) + "</option>\n");
    }
    retValue.append("</select>\n");

    return retValue.toString();
}

From source file:com.alkacon.opencms.documentcenter.NewDocumentsTree.java

/**
 * Creates a nice localized date String from the given String.<p>
 * //w w  w  . ja v a  2 s .  c  om
 * @param dateLongString the date as String representation of a long value
 * @param localeString the current locale String
 * @return nice formatted date string in long mode (e.g. 15. April 2003)
 */
public static String getNiceDate(String dateLongString, String localeString) {

    Locale locale = new Locale(localeString.toLowerCase());
    DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, locale);
    Calendar cal = new GregorianCalendar(locale);
    try {
        cal.setTimeInMillis(Long.parseLong(dateLongString));
    } catch (Exception e) {
        //noop
    }
    return df.format(cal.getTime());
}

From source file:com.streamsets.pipeline.stage.origin.jdbc.table.AllTypesIT.java

private static void populateRecords() {
    Record record = RecordCreator.create();
    LinkedHashMap<String, Field> fields;
    AtomicInteger id_field = new AtomicInteger(0);

    //CHAR_AND_BINARY
    fields = new LinkedHashMap<>();
    createIdField(fields, id_field);//ww w. j  a v  a 2 s  .c o  m
    fields.put("char1", Field.create("abcdefghij"));
    fields.put("varchar1", Field.create(UUID.randomUUID().toString()));
    fields.put("clob1", Field.create(UUID.randomUUID().toString()));
    fields.put("varbinary1", Field.create(UUID.randomUUID().toString().getBytes()));
    fields.put("blob1", Field.create(UUID.randomUUID().toString().getBytes()));
    record.set(Field.createListMap(fields));

    TABLE_TO_TEMPLATE_AND_RECORDS_MAP.get("CHAR_AND_BINARY").getRight().add(record);

    //Date and time
    record = RecordCreator.create();
    fields = new LinkedHashMap<>();
    createIdField(fields, id_field);
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    fields.put("date1", Field.create(Field.Type.DATE, calendar.getTime()));
    calendar.setTimeInMillis(System.currentTimeMillis());

    calendar.set(Calendar.MILLISECOND, 0);
    fields.put("timestamp1", Field.create(Field.Type.DATETIME, calendar.getTime()));
    fields.put("datetime1", Field.create(Field.Type.DATETIME, calendar.getTime()));
    calendar.setTimeInMillis(System.currentTimeMillis());

    calendar.set(Calendar.YEAR, 1970);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.MILLISECOND, 0);
    fields.put("time1", Field.create(Field.Type.TIME, calendar.getTime()));
    calendar.setTimeInMillis(System.currentTimeMillis());

    record.set(Field.createListMap(fields));
    TABLE_TO_TEMPLATE_AND_RECORDS_MAP.get("DATE_AND_TIME").getRight().add(record);

    //DIFFERENT_INTS
    record = RecordCreator.create();
    fields = new LinkedHashMap<>();
    createIdField(fields, id_field);
    fields.put("int1", Field.create(Field.Type.INTEGER, Integer.MIN_VALUE));
    fields.put("int2", Field.create(Field.Type.INTEGER, Integer.MIN_VALUE));
    fields.put("mediumint1", Field.create(Field.Type.INTEGER, Integer.MIN_VALUE));
    fields.put("tinyint1", Field.create(Field.Type.SHORT, -128));
    fields.put("smallint1", Field.create(Field.Type.SHORT, Short.MIN_VALUE));
    fields.put("bigint1", Field.create(Field.Type.LONG, Long.MIN_VALUE));
    record.set(Field.createListMap(fields));
    TABLE_TO_TEMPLATE_AND_RECORDS_MAP.get("DIFFERENT_INTS").getRight().add(record);

    record = RecordCreator.create();
    fields = new LinkedHashMap<>();
    createIdField(fields, id_field);
    fields.put("int1", Field.create(Field.Type.INTEGER, Integer.MAX_VALUE));
    fields.put("int2", Field.create(Field.Type.INTEGER, Integer.MAX_VALUE));
    fields.put("mediumint1", Field.create(Field.Type.INTEGER, Integer.MAX_VALUE));
    fields.put("tinyint1", Field.create(Field.Type.SHORT, 127));
    fields.put("smallint1", Field.create(Field.Type.SHORT, Short.MAX_VALUE));
    fields.put("bigint1", Field.create(Field.Type.LONG, Long.MAX_VALUE));
    record.set(Field.createListMap(fields));
    TABLE_TO_TEMPLATE_AND_RECORDS_MAP.get("DIFFERENT_INTS").getRight().add(record);

    //FLOATING_PT_INTS
    record = RecordCreator.create();
    fields = new LinkedHashMap<>();
    createIdField(fields, id_field);
    fields.put("decimal1", Field.create(Field.Type.DECIMAL, new BigDecimal("12.345")));
    fields.put("number1", Field.create(Field.Type.DECIMAL, new BigDecimal("0.12345")));
    fields.put("double1", Field.create(Field.Type.DOUBLE, 123.456));
    fields.put("real1", Field.create(Field.Type.FLOAT, 12.34));
    fields.put("floatdouble1", Field.create(Field.Type.DOUBLE, Double.MAX_VALUE));
    record.set(Field.createListMap(fields));
    TABLE_TO_TEMPLATE_AND_RECORDS_MAP.get("FLOATING_PT_INTS").getRight().add(record);

    record = RecordCreator.create();
    fields = new LinkedHashMap<>();
    createIdField(fields, id_field);
    fields.put("decimal1", Field.create(Field.Type.DECIMAL, new BigDecimal("-12.345")));
    fields.put("number1", Field.create(Field.Type.DECIMAL, new BigDecimal("-0.12345")));
    fields.put("double1", Field.create(Field.Type.DOUBLE, -123.456));
    fields.put("real1", Field.create(Field.Type.FLOAT, -12.34));
    fields.put("floatdouble1", Field.create(Field.Type.DOUBLE, Double.MIN_VALUE));
    record.set(Field.createListMap(fields));
    TABLE_TO_TEMPLATE_AND_RECORDS_MAP.get("FLOATING_PT_INTS").getRight().add(record);

    //OTHER_TYPES
    record = RecordCreator.create();
    fields = new LinkedHashMap<>();
    createIdField(fields, id_field);
    fields.put("boolean1", Field.create(Field.Type.BOOLEAN, true));
    record.set(Field.createListMap(fields));
    TABLE_TO_TEMPLATE_AND_RECORDS_MAP.get("OTHER_TYPES").getRight().add(record);

    record = RecordCreator.create();
    fields = new LinkedHashMap<>();
    createIdField(fields, id_field);
    fields.put("boolean1", Field.create(Field.Type.BOOLEAN, false));
    record.set(Field.createListMap(fields));
    TABLE_TO_TEMPLATE_AND_RECORDS_MAP.get("OTHER_TYPES").getRight().add(record);
}

From source file:com.projity.pm.calendar.CalendarDefinition.java

public static final int getDayOfWeek(long date) {
    Calendar scratchDate = DateTime.calendarInstance();
    scratchDate.setTimeInMillis(date);
    return scratchDate.get(Calendar.DAY_OF_WEEK) - 1;
}