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:Main.java

public static String getTime(long time) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(time);
    int minute = c.get(Calendar.MINUTE);
    int second = c.get(Calendar.SECOND);
    String times = "";
    if (minute < 10) {
        times = "0" + minute;
    } else {/*from   w ww .ja v  a2s. c  o  m*/
        times = "" + minute;
    }
    if (second < 10) {
        times += ":0" + second;
    } else {
        times += ":" + second;
    }
    return times;
}

From source file:Main.java

public static String getDate(long time) {
    try {/*from  w  w  w  .  j a  v  a  2s .c  o m*/
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        return format.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:lu.lippmann.cdb.common.gui.dataset.InstanceFormatter.java

public static String htmlFormat(final Instance inst, final boolean withHTMLHeader) {
    final StringBuilder sb = new StringBuilder();
    if (withHTMLHeader)
        sb.append("<html><body>");
    for (int i = 0; i < inst.numAttributes(); i++) {
        sb.append(StringEscapeUtils.escapeHtml(inst.attribute(i).name())).append(" = ");
        sb.append("<b>");
        if (inst.attribute(i).isNominal() || inst.attribute(i).isString()) {
            sb.append(StringEscapeUtils.escapeHtml(inst.stringValue(i)));
        } else if (inst.attribute(i).isDate()) {
            final Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis((long) inst.value(i));
            sb.append(FormatterUtil.DATE_FORMAT.format(cal.getTime()));
        } else if (inst.attribute(i).isNumeric()) {
            sb.append(inst.value(i));/*  w w  w.  j av a 2 s. c  o  m*/
        }
        sb.append("</b>");
        sb.append("<br/>");
    }
    if (withHTMLHeader)
        sb.append("</body></html>");
    return sb.toString();
}

From source file:Main.java

public static String getTimeAndDate(long j) {
    try {//  w  ww.  ja va2s  . com
        Calendar instance = Calendar.getInstance();
        TimeZone timeZone = TimeZone.getDefault();
        instance.setTimeInMillis(j);
        instance.add(14, timeZone.getOffset(instance.getTimeInMillis()));
        String format = new SimpleDateFormat(" dd, MMM yyyy").format(instance.getTime());
        Time time = new Time();
        time.set(j);
        return time.format("%H:%M") + format;
    } catch (Exception e) {
        return "";
    }
}

From source file:com.pureinfo.force.util.TimerUtil.java

/**
 * @param _sTime//from   w ww  . j  ava  2  s  .c  o  m
 *            must be "HH:mm" format
 */
public static Timer scheduleFrom(String _sTime, long _lPeriod, TimerTask _task) {
    Calendar nowTime = new GregorianCalendar();
    Calendar firstTime = getTimeToday(nowTime, _sTime);
    firstTime.setTimeInMillis(getNext(firstTime.getTimeInMillis(), nowTime.getTimeInMillis(), _lPeriod));
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(_task, firstTime.getTime(), _lPeriod);
    return timer;
}

From source file:Main.java

/**
 * Format calendar to string/*w ww  .  j  a  v a  2  s .  c  o  m*/
 * @param c Calendar to convert
 * @param format String format of SimpleDateFormat
 * @return String converted
 */
public static String formatDate(Calendar c, String format) {
    if (c == null) {
        c = Calendar.getInstance();
        c.setTimeInMillis(0);
        c.set(Calendar.YEAR, 1990);
    }

    return formatDate(c.getTime(), format);
}

From source file:Main.java

public static long getPeriodStart(int periodType, long date) {
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(date);

    switch (periodType) {
    case TYPE_DAY: {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;//www . j  a va 2  s . c  om
    }

    case TYPE_WEEK: {
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        final int currentDayOfWeek = (cal.get(Calendar.DAY_OF_WEEK) + 7 - cal.getFirstDayOfWeek()) % 7;
        cal.add(Calendar.DAY_OF_YEAR, -currentDayOfWeek);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_MONTH: {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_YEAR: {
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }
    }
    return cal.getTimeInMillis();
}

From source file:info.magnolia.cms.util.DateUtil.java

/**
 * Convert a local date time to a UTC calendar
 *///from  w  w w  . ja  v a2s .c  om
public static Calendar getUTCCalendarFromLocalDate(Date date) {
    Calendar instance = Calendar.getInstance(UTC_TIME_ZONE);
    instance.setTimeInMillis(date.getTime());
    return instance;
}

From source file:Main.java

public static long getPeriodEnd(int periodType, long date) {
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(getPeriodStart(periodType, date));

    switch (periodType) {
    case TYPE_DAY: {
        cal.add(Calendar.DAY_OF_YEAR, 1);
        break;//from w  w w . ja  v  a2s  . c  o  m
    }

    case TYPE_WEEK: {
        cal.add(Calendar.WEEK_OF_YEAR, 1);
        break;
    }

    case TYPE_MONTH: {
        cal.add(Calendar.MONTH, 1);
        break;
    }

    case TYPE_YEAR: {
        cal.add(Calendar.YEAR, 1);
        break;
    }
    }

    cal.add(Calendar.MILLISECOND, -1);
    return cal.getTimeInMillis();
}

From source file:DateUtils.java

/**
 * Creates an instance of {@link java.util.Calendar} from the given time in milliseconds.
 * <p>/*from  w w w  .j  a  v  a2  s.c om*/
 * The input milliseconds value represents the specified number of milliseconds since the standard base time known
 * as "the epoch", namely January 1, 1970, 00:00:00 GMT.
 *
 * @param millis
 *            A given time corresponding to the number of milliseconds since January 1, 1970, 00:00:00 GMT
 * @return Converted <code>Calendar</code> object.
 */
public static Calendar toCalendar(final long millis) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millis);
    return calendar;
}