Example usage for java.util Calendar set

List of usage examples for java.util Calendar set

Introduction

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

Prototype

public final void set(int year, int month, int date, int hourOfDay, int minute, int second) 

Source Link

Document

Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, and SECOND.

Usage

From source file:Main.java

/** Converts a Macintosh-style timestamp (seconds since
 *  January 1, 1904) into a Java date.  The timestamp is
 *  treated as a time in the default localization.
 *  Depending on that localization,//  w w w .  j  a v  a2 s  .  c o  m
 *  there may be some variation in the exact hour of the date 
 *  returned, e.g., due to daylight savings time.
 * 
 */
public static Date timestampToDate(long timestamp) {
    Calendar cal = Calendar.getInstance();
    cal.set(1904, 0, 1, 0, 0, 0);

    // If we add the seconds directly, we'll truncate the long
    // value when converting to int.  So convert to hours plus
    // residual seconds.
    int hours = (int) (timestamp / 3600);
    int seconds = (int) (timestamp - (long) hours * 3600L);
    cal.add(Calendar.HOUR_OF_DAY, hours);
    cal.add(Calendar.SECOND, seconds);
    Date dat = cal.getTime();
    return dat;
}

From source file:Main.java

@NonNull
public static Calendar getCalendar(String date) {
    //Date should be in the format YYYY/MM/DD if not return
    if (date != null && !date.isEmpty() && date.length() == 10) {
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = Integer.parseInt(date.substring(8, 10));
        Calendar startingTime = Calendar.getInstance();
        startingTime.set(year, month - 1, day, 0, 0, 0);
        return startingTime;
    }/*w  w w. j av  a 2s  . com*/
    return null;
}

From source file:Main.java

public static Date toTimeZone(Date date, TimeZone source, TimeZone target) {
    Calendar cal1 = Calendar.getInstance(source);
    cal1.setTime(date);//from  ww w  .  j a v  a 2s.  c o m

    int[] dateSplit = splitDate(date);

    Calendar cal2 = Calendar.getInstance(target);
    cal2.set(dateSplit[0], dateSplit[1], dateSplit[2], dateSplit[3], dateSplit[4], dateSplit[5]);
    return cal2.getTime();
}

From source file:Main.java

/**
 * Converts time in dos format to Java format
 * @param dosTime//from  w  w w. ja v  a  2s.c  om
 * @return time in java format
 */
public static long dosToJavaTme(int dosTime) {
    int sec = 2 * (dosTime & 0x1f);
    int min = (dosTime >> 5) & 0x3f;
    int hrs = (dosTime >> 11) & 0x1f;
    int day = (dosTime >> 16) & 0x1f;
    int mon = ((dosTime >> 21) & 0xf) - 1;
    int year = ((dosTime >> 25) & 0x7f) + 1980;

    Calendar cal = Calendar.getInstance();
    cal.set(year, mon, day, hrs, min, sec);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime().getTime();
}

From source file:Main.java

/**
 * Sleeps the current thread until the specified future date. If the date is before the current time,
 * the thread will resume operation immediately.
 * /*from   w  w w. ja va  2s.  c o m*/
 * @param date
 */
public static void sleepUntil(int year, int month, int day, int hour, int min, int sec) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, day, hour, min, sec);

    long msFuture = cal.getTime().getTime();
    long msNow = System.currentTimeMillis();
    long msSleep = msFuture - msNow;

    if (msSleep <= 0) {
        return;
    }

    try {
        Thread.sleep(msFuture - msNow);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:Util.java

/**
 * Unpacks date stored in kdb format./*  www . ja  va2 s .  co m*/
 * 
 * @param d
 * @return
 */
public static Date unpackDate(byte[] d) {
    // Byte bits: 00000000 11111111 22222222 33333333 44444444
    // Contents : 00YYYYYY YYYYYYMM MMDDDDDH HHHHMMMM MMSSSSSS
    int year = (d[0] << 6) | ((d[1] >> 2) & 0x0000003F);
    int month = ((d[1] & 0x00000003) << 2) | ((d[2] >> 6) & 0x00000003);
    int day = (d[2] >> 1) & 0x0000001F;
    int hour = ((d[2] & 0x00000001) << 4) | ((d[3] >> 4) & 0x0000000F);
    int minute = ((d[3] & 0x0000000F) << 2) | ((d[4] >> 6) & 0x00000003);
    int second = d[4] & 0x0000003F;
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, day, hour, minute, second);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}

From source file:Main.java

/**
 * @param dateString// ww w . j av a2  s  .c om
 * @return a java Date object that correspond to the dateString
 * Convert the dateString to a Date object
 */
public static Date getDateFromString(String dateString) {
    // The Irish Time: "Mon, 15 Apr 2013 06:55:14 +0000"
    // Guardian / BBC: Wed, 17 Apr 2013 12:57:50 GMT

    String[] elements = dateString.split("[:\\s+]");
    int year = Integer.parseInt(elements[3]);
    int month = getMonth(elements[2].toLowerCase());
    int date = Integer.parseInt(elements[1]);
    int hour = Integer.parseInt(elements[4]);
    int minute = Integer.parseInt(elements[5]);
    int second = Integer.parseInt(elements[6]);

    Calendar cal = Calendar.getInstance();
    cal.set(year, month, date, hour, minute, second);

    return cal.getTime();
}

From source file:Main.java

/**
 * Creates a Calendar instance for the given year, month (1-based), day
 * (1-based), hour, minute and second in the default time zone
 *///from   w ww.jav  a 2s  . co m
public static Calendar createDate(int year, int month, int day, int hour, int minute, int second) {
    Calendar date = Calendar.getInstance();
    date.set(year, month - 1, day, hour, minute, second); // Months start at 0 for Calendar!
    date.set(Calendar.MILLISECOND, 0);
    return date;
}

From source file:Main.java

/**
 * Creates a Calendar instance for the given year, month (1-based), day
 * (1-based), hour, minute and second in the given time zone
 *///from  w ww . jav a 2s  .  c o m
public static Calendar createDate(TimeZone timeZone, int year, int month, int day, int hour, int minute,
        int second) {
    Calendar date = Calendar.getInstance(timeZone);
    date.set(year, month - 1, day, hour, minute, second); // Months start at 0 for Calendar!
    date.set(Calendar.MILLISECOND, 0);
    return date;
}

From source file:Main.java

/**
 * Convert the given Julian Day to Gregorian Date (in UT time zone).
 * Based on the formula given in the Explanitory Supplement to the
 * Astronomical Almanac, pg 604.//  w  w w  . j a  va2 s  .  c om
 */
public static Date calculateGregorianDate(double jd) {
    int l = (int) jd + 68569;
    int n = (4 * l) / 146097;
    l = l - (146097 * n + 3) / 4;
    int i = (4000 * (l + 1)) / 1461001;
    l = l - (1461 * i) / 4 + 31;
    int j = (80 * l) / 2447;
    int d = l - (2447 * j) / 80;
    l = j / 11;
    int m = j + 2 - 12 * l;
    int y = 100 * (n - 49) + i + l;

    double fraction = jd - Math.floor(jd);
    double dHours = fraction * 24.0;
    int hours = (int) dHours;
    double dMinutes = (dHours - hours) * 60.0;
    int minutes = (int) dMinutes;
    int seconds = (int) ((dMinutes - minutes) * 60.0);

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UT"));
    cal.set(y, m - 1, d, hours + 12, minutes, seconds);
    return cal.getTime();
}