Example usage for java.util Calendar getTimeZone

List of usage examples for java.util Calendar getTimeZone

Introduction

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

Prototype

public TimeZone getTimeZone() 

Source Link

Document

Gets the time zone.

Usage

From source file:Main.java

public static String getDateTimeTextFromTimestamp(Long timestamp) {
    Timestamp stamp = new Timestamp(timestamp);
    Date date = new Date(stamp.getTime());
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat s = new SimpleDateFormat("dd MMM yyyy HH:mm");
    s.setTimeZone(cal.getTimeZone());
    return s.format(date);
}

From source file:org.apache.ranger.common.DateUtil.java

public static Date getLocalDateForUTCDate(Date date) {
    Calendar local = Calendar.getInstance();
    int offset = local.getTimeZone().getOffset(local.getTimeInMillis());
    GregorianCalendar utc = new GregorianCalendar();
    utc.setTimeInMillis(date.getTime());
    utc.add(Calendar.MILLISECOND, offset);
    return utc.getTime();
}

From source file:org.apache.ranger.common.DateUtil.java

public static Date getUTCDate() {
    try {/*w w w  . j  a v  a2 s.  c om*/
        Calendar local = Calendar.getInstance();
        int offset = local.getTimeZone().getOffset(local.getTimeInMillis());
        GregorianCalendar utc = new GregorianCalendar(gmtTimeZone);
        utc.setTimeInMillis(local.getTimeInMillis());
        utc.add(Calendar.MILLISECOND, -offset);
        return utc.getTime();
    } catch (Exception ex) {
        return null;
    }
}

From source file:org.apache.ranger.common.DateUtil.java

public static Date getUTCDate(long epoh) {
    if (epoh == 0) {
        return null;
    }/*from   w  w  w.  ja  v a2s  . c  om*/
    try {
        Calendar local = Calendar.getInstance();
        int offset = local.getTimeZone().getOffset(epoh);
        GregorianCalendar utc = new GregorianCalendar(gmtTimeZone);
        utc.setTimeInMillis(epoh);
        utc.add(Calendar.MILLISECOND, -offset);
        return utc.getTime();
    } catch (Exception ex) {
        return null;
    }
}

From source file:fitnesse.components.Logger.java

private static String format(DateFormat format, Calendar calendar) {
    DateFormat tmpFormat = (DateFormat) format.clone();
    tmpFormat.setTimeZone(calendar.getTimeZone());
    return tmpFormat.format(calendar.getTime());
}

From source file:com.hp.hpl.jena.sparql.util.Utils.java

private static String calcTimezone(Calendar cal) {
    Date date = cal.getTime();//from   w w w  . ja va 2s .  co  m
    TimeZone z = cal.getTimeZone();
    int tz = z.getRawOffset();

    if (z.inDaylightTime(date)) {
        int tzDst = z.getDSTSavings();
        tz = tz + tzDst;
    }

    String sign = "+";
    if (tz < 0) {
        sign = "-";
        tz = -tz;
    }

    int tzH = tz / (60 * 60 * 1000); // Integer divide towards zero.
    int tzM = (tz - tzH * 60 * 60 * 1000) / (60 * 1000);

    String tzH_str = Integer.toString(tzH);
    String tzM_str = Integer.toString(tzM);

    if (tzH < 10)
        tzH_str = "0" + tzH_str;
    if (tzM < 10)
        tzM_str = "0" + tzM_str;
    return sign + tzH_str + ":" + tzM_str;
}

From source file:Main.java

/**
 * Takes a calendar object representing a date/time, ignores its current time zone (which should be the default time zone)
 * applies that date/time to the sourceTimeZone and returns the relative date/time in the current time zone. 
 * //from   w w  w  . ja  va 2  s .  co m
 * For example, given an input of 13:00 EST and source time zone PST, it will return 16:00 EST 
 * 13:00 EST = 18:00 GMT = 10:00 PST
 *  
 * @param calendar
 * @param sourceTimeZone
 * @return
 */
public static Calendar convertToTimeZone(Calendar calendar, TimeZone sourceTimeZone) {
    Calendar result = Calendar.getInstance();
    // i.e., 13:00 EST becomes 08:00 GMT
    long originalTimeInUtc = calendar.getTimeInMillis()
            + calendar.getTimeZone().getOffset(calendar.getTimeInMillis());
    // 08:00 GMT becomes 16:00 PST
    long sourceTime = originalTimeInUtc - sourceTimeZone.getOffset(originalTimeInUtc);
    result.setTimeZone(sourceTimeZone);
    result.setTimeInMillis(sourceTime);
    /*
    Log.d(TAG, "Converting "+DEBUG_CALENDAR_OUTPUT.format(new Date(calendar.getTimeInMillis()))
        +" in ["+sourceTimeZone.getDisplayName()+"] to ["+TimeZone.getDefault().getDisplayName()
        +"] resulting in "+DEBUG_CALENDAR_OUTPUT_WITH_TIMEZONE.format(new Date(result.getTimeInMillis())));
    Log.d(TAG, "Original time in UTC = "+DEBUG_CALENDAR_OUTPUT.format(new Date(originalTimeInUtc)));
    Log.d(TAG, "Original time in source time zone = "+DEBUG_CALENDAR_OUTPUT.format(new Date(sourceTime)));
     */
    return result;
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static Calendar getCalendarUTC() {
    Calendar c = Calendar.getInstance();
    System.out.println("current: " + c.getTime());

    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if (z.inDaylightTime(new Date())) {
        offset = offset + z.getDSTSavings();
    }/*from ww  w. java  2s . c o  m*/
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMins: " + offsetMins);
    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));

    System.out.println("GMT Time: " + c.getTime());

    return c;
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static long getTimeinMillis4TimeUTC(int hours, int minutes, int seconds) {
    Calendar c = Calendar.getInstance();
    System.out.println("current: " + c.getTime());

    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if (z.inDaylightTime(new Date())) {
        offset = offset + z.getDSTSavings();
    }//from  w w w .j  a  va  2  s.c  o m
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMins: " + offsetMins);

    c.set(Calendar.HOUR_OF_DAY, (hours - offsetHrs));
    c.set(Calendar.MINUTE, (minutes - offsetMins));
    c.set(Calendar.SECOND, seconds);

    System.out.println("GMT Time: " + c.getTime());

    long timeinmillis = c.getTimeInMillis();
    System.out.println(" system time in millis " + timeinmillis);

    return timeinmillis;
}

From source file:com.mycompany.craftdemo.utility.java

public static boolean isValidTime() {
    Calendar calendar = Calendar.getInstance();
    TimeZone fromTimeZone = calendar.getTimeZone();
    TimeZone toTimeZone = TimeZone.getTimeZone("EST");

    calendar.setTimeZone(fromTimeZone);//from  w  w w . j  a  v a  2 s.  c o  m
    calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
    if (fromTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
    }

    calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
    if (toTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
    }

    int hour = calendar.get(Calendar.HOUR_OF_DAY);

    return hour >= 9 && hour <= 16;
}