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 Date firstTimeOfWeek(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date.getTime());

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

    Date dateBegin = new Date();
    dateBegin.setTime(calendar.getTimeInMillis());
    return dateBegin;
}

From source file:Main.java

/**
 * Retrieve a calendar object set to the specified millisecond time
 * @param timeInMillis the time to set on the calendar
 * @return the calendar object with the specified time.
 *//*from  w  w  w. j a va 2s .c  o  m*/
public static Calendar getCalendar(long timeInMillis) {
    Calendar date = Calendar.getInstance();
    date.setTimeInMillis(timeInMillis);
    return date;
}

From source file:Main.java

public static boolean isTaday(Long time) {
    Calendar calendar_cur = Calendar.getInstance();
    Calendar calendar_old = Calendar.getInstance();
    calendar_old.setTimeInMillis(time);
    int day_cur = calendar_cur.get(Calendar.DAY_OF_YEAR);
    int day_old = calendar_old.get(Calendar.DAY_OF_YEAR);
    if (day_cur == day_old) {
        return true;
    } else {/*  www .  j a  va 2 s.com*/
        return false;
    }
}

From source file:Main.java

public static String getTimeStamp(long l) {
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(l);
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4)
            + " " + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
            + makeTimeString(cal.get(Calendar.MINUTE), 2) + ":" + makeTimeString(cal.get(Calendar.SECOND), 2);
}

From source file:Main.java

public static Calendar convertCalendar(String milliSeconds) {

    Long mMilliSeconds = 0L;/* ww w .j av a  2 s .  co m*/

    mMilliSeconds = Long.parseLong(milliSeconds);

    Calendar mCalendarEvent = Calendar.getInstance();
    mCalendarEvent.setTimeInMillis(mMilliSeconds);
    return mCalendarEvent;

}

From source file:Main.java

/**
 * Return date in specified format.//from w  ww .j  a v  a 2 s . co m
 *
 * @param milliSeconds Date in milliseconds
 * @param dateFormat   Date format
 * @return String representing date in specified format
 */
public static String getDate(long milliSeconds, String dateFormat) {
    // Create a DateFormatter object for displaying date in specified format.
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);

    // Create a calendar object that will convert the date and time value in milliseconds to date.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliSeconds);
    return formatter.format(calendar.getTime());
}

From source file:Main.java

/**
 * get month between minTime and maxTime, including head and tail
 *
 * @param minTime/*from ww  w .j  a va2s .co  m*/
 * @param maxTime
 * @return
 */
public static int getMonthNum(long minTime, long maxTime) {
    Calendar min = Calendar.getInstance();
    min.setTimeInMillis(minTime);
    Calendar max = Calendar.getInstance();
    max.setTimeInMillis(maxTime);
    if (max.before(min)) {
        throw new IllegalArgumentException("max date is before min date");
    }
    int minMonth = min.get(Calendar.MONTH) + 1;
    int maxMonth = max.get(Calendar.MONTH) + 1;
    return (max.get(Calendar.YEAR) - min.get(Calendar.YEAR)) * 12 + maxMonth - minMonth + 1;
}

From source file:Main.java

public static String createJcFileName() {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());

    int y = c.get(Calendar.YEAR);
    int m = c.get(Calendar.MONTH);
    int d = c.get(Calendar.DAY_OF_MONTH);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    int second = c.get(Calendar.SECOND);
    return y + "-" + m + "-" + d + "-" + hour + "-" + minute + "-" + second;
}

From source file:Main.java

/**
 * Encode a java date/time into a 16-bit encoded DOS date
 * /*from w  ww. ja  v a 2 s  .  c om*/
 * @param javaDateTime
 * @return long
 */
public static int encodeDate(long javaDateTime) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(javaDateTime);
    return 512 * (cal.get(Calendar.YEAR) - 1980) + 32 * (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);
}

From source file:Main.java

/**
 * Encode a java date/time into a 16-bit encoded DOS time
 * /* w w w . ja  va 2s.c o m*/
 * @param javaDateTime
 * @return long
 */
public static int encodeTime(long javaDateTime) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(javaDateTime);
    return 2048 * cal.get(Calendar.HOUR_OF_DAY) + 32 * cal.get(Calendar.MINUTE) + cal.get(Calendar.SECOND) / 2;
}