Example usage for java.util Calendar setTimeZone

List of usage examples for java.util Calendar setTimeZone

Introduction

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

Prototype

public void setTimeZone(TimeZone value) 

Source Link

Document

Sets the time zone with the given time zone value.

Usage

From source file:bobs.is.compress.sevenzip.SevenZArchiveEntry.java

/**
 * Converts Java time to NTFS time.//from w  w w. jav a2 s.  c  om
 * @param date the Java time
 * @return the NTFS time
 */
public static long javaTimeToNtfsTime(final Date date) {
    final Calendar ntfsEpoch = Calendar.getInstance();
    ntfsEpoch.setTimeZone(TimeZone.getTimeZone("GMT+0"));
    ntfsEpoch.set(1601, 0, 1, 0, 0, 0);
    ntfsEpoch.set(Calendar.MILLISECOND, 0);
    return ((date.getTime() - ntfsEpoch.getTimeInMillis()) * 1000 * 10);
}

From source file:bobs.is.compress.sevenzip.SevenZArchiveEntry.java

/**
 * Converts NTFS time (100 nanosecond units since 1 January 1601)
 * to Java time./* w  w w . j  av  a  2s .co  m*/
 * @param ntfsTime the NTFS time in 100 nanosecond units
 * @return the Java time
 */
public static Date ntfsTimeToJavaTime(final long ntfsTime) {
    final Calendar ntfsEpoch = Calendar.getInstance();
    ntfsEpoch.setTimeZone(TimeZone.getTimeZone("GMT+0"));
    ntfsEpoch.set(1601, 0, 1, 0, 0, 0);
    ntfsEpoch.set(Calendar.MILLISECOND, 0);
    final long realTime = ntfsEpoch.getTimeInMillis() + (ntfsTime / (10 * 1000));
    return new Date(realTime);
}

From source file:org.istsms.util.Javabean2JSON.java

private static Object toJavaCalendar(JSONObject jo) {
    if (jo == null)
        return null;
    try {//w ww  . ja  v  a  2 s . c  o  m
        if (jo.getString("__class").equals(Calendar.class.getCanonicalName())) {
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(new java.util.Date(Long.parseLong(jo.getString("__value"))));
            c.setTimeZone(java.util.TimeZone.getTimeZone(jo.getString("__timezn")));
            return c;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (RuntimeException r) {
        r.printStackTrace();
    }
    return null;
}

From source file:org.ariadne.oai.utils.HarvesterUtils.java

public static void addReposContributor(Record record, String name) throws JDOMException {

    final MimeDir.ContentLine fn = new MimeDirImpl.ContentLine(null, "FN", null,
            new MimeDirImpl.TextValueType(new String[] { name }));
    final MimeDir.ContentLine n = new MimeDirImpl.ContentLine(null, "N", null,
            new MimeDirImpl.TextValueType(new String[] { name }));
    final MimeDir.ContentLine org = new MimeDirImpl.ContentLine(null, "ORG", null,
            new MimeDirImpl.TextValueType(new String[] { name }));
    final MimeDir.ContentLine version = new MimeDirImpl.ContentLine(null, "VERSION", null,
            new MimeDirImpl.TextValueType(new String[] { "3.0" }));

    Element metaMetadata = null;//from   w  ww . jav a  2  s .c  o m
    //      try {
    metaMetadata = JDomUtils.getXpathNode("//lom:lom/lom:metaMetadata", OaiUtils.LOMLOMNS,
            record.getMetadata());
    if (metaMetadata != null) {
        // contribute
        Element contribute = new Element("contribute", OaiUtils.LOMNS);
        metaMetadata.addContent(contribute);
        // contribute.entity
        Element entity = new Element("entity", OaiUtils.LOMNS);
        VCardImpl vcard = new VCardImpl(new MimeDir.ContentLine[] { fn, n, org, version });
        CDATA cdata = new CDATA(MimeDirUtil.toString(vcard));
        entity.addContent(cdata);
        contribute.addContent(entity);
        // contribute.role
        Element role = new Element("role", OaiUtils.LOMNS);
        contribute.addContent(role);
        Element value = new Element("value", OaiUtils.LOMNS);
        String valueString = PropertiesManager.getInstance().getProperty("Harvest.metadataProvider.value");
        value.setText(valueString);
        role.addContent(value);
        Element source = new Element("source", OaiUtils.LOMNS);
        String sourceString = PropertiesManager.getInstance().getProperty("Harvest.metadataProvider.source");
        source.setText(sourceString);
        role.addContent(source);
        // contribute.date
        Element date = new Element("date", OaiUtils.LOMNS);
        contribute.addContent(date);
        Element dateTime = new Element("dateTime", OaiUtils.LOMNS);
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        // String time = Calendar.getInstance().getTime().toString();
        dateTime.setText(DateParser.getIsoDate(dateCalendar));
        date.addContent(dateTime);
    }
}

From source file:com.lm.lic.manager.util.GenUtil.java

public static Date resetClientTimezone(HttpServletRequest request, Date date) {
    final TimeZone timeZone = TimeZone.getDefault();
    final boolean daylight = timeZone.inDaylightTime(date);
    final Locale locale = request.getLocale();
    String tzName = timeZone.getDisplayName(daylight, TimeZone.LONG, locale);
    // String countryCode = locale.getCountry();
    // TimeZone ltz = TimeZone.getTimeZone(countryCode);
    // TimeZoneNameUtility.getZoneStrings(locale);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);// w  ww  . ja v  a2s . com
    cal.setTimeZone(TimeZone.getTimeZone(tzName));
    return cal.getTime();
}

From source file:ac.elements.parser.ExtendedFunctions.java

/**
 * Formats a number given as the number of milliseconds String or Long and
 * converts it to a format "H'h' m'm' s's'". Periods longer then a day do
 * not work in this method. If conversion doesn't work it returns the Object
 * casted as a string. (SimpleDateFormat syntax).
 * // w  w w. j  av a 2s.  c  om
 * @returns null if myObject is null, otherwise a formatted time period as a
 *          string.
 * @see java.text.SimpleDateFormat
 */
public static String timePeriod(Object myObject) {

    long timePeriodInMilliseceonds = 0;

    if (myObject == null)
        return null;

    try {

        if (myObject instanceof String) {
            timePeriodInMilliseceonds = Long.parseLong(myObject.toString());
        } else if (myObject instanceof Number) {
            timePeriodInMilliseceonds = ((Number) myObject).longValue();
        } else {
            return "Not supported: ".concat(myObject.getClass().getName());
        }

        String format = "D' days' H'h' mm'm' ss's'";
        if (timePeriodInMilliseceonds < 60 * 1000l)
            format = "ss's'";
        else if (timePeriodInMilliseceonds < 3600 * 1000l)
            format = "mm'm' ss's'";
        else if (timePeriodInMilliseceonds < 3600 * 24 * 1000l)
            format = "H'h' mm'm' ss's'";
        else if (timePeriodInMilliseceonds < 3600 * 48 * 1000l) {
            format = "D' day' H'h' mm'm' ss's'";
        }

        // Get Greenwich time zone.
        TimeZone theTz = TimeZone.getTimeZone("GMT");

        SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
        mySimpleDateFormat.setTimeZone(theTz);

        // create a date in the locale's calendar,
        // set its timezone and hour.
        Calendar day = Calendar.getInstance();
        day.setTimeZone(theTz);
        day.setTime(new Date(timePeriodInMilliseceonds));
        day.roll(Calendar.DAY_OF_MONTH, false);
        day.set(Calendar.HOUR, day.get(Calendar.HOUR));

        return mySimpleDateFormat.format(day.getTime());

    } catch (NullPointerException npe) {
        // npe.printStackTrace();
        return null;
    } catch (NumberFormatException nfe) {
        // nfe.printStackTrace();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:org.richfaces.renderkit.CalendarRendererBase.java

public static Object formatSelectedDate(TimeZone timeZone, Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(timeZone);
    calendar.setTime(date);/*from w  w w .  j  a  v  a2  s .  c  o m*/
    JSFunction result = new JSFunction("new Date");
    result.addParameter(Integer.valueOf(calendar.get(Calendar.YEAR)));
    result.addParameter(Integer.valueOf(calendar.get(Calendar.MONTH)));
    result.addParameter(Integer.valueOf(calendar.get(Calendar.DATE)));
    result.addParameter(Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY)));
    result.addParameter(Integer.valueOf(calendar.get(Calendar.MINUTE)));
    result.addParameter(new Integer(0));
    return result;
}

From source file:org.openmrs.module.atomfeed.AtomFeedUtil.java

/**
 * Format dates as specified in rfc3339 (required for Atom dates)
 * //w  w  w.  j av a 2  s . c om
 * @param d the Date to be formatted
 * @return the formatted date
 * @should not fail given a null date
 * @should convert date to rfc
 */
public static String dateToRFC3339(Date d) {
    if (d == null)
        return null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(RFC_3339_DATE_FORMAT);
    Calendar cal = new GregorianCalendar();
    cal.setTime(d);
    cal.setTimeZone(TimeZone.getDefault());
    simpleDateFormat.setCalendar(cal);
    StringBuilder result = new StringBuilder(simpleDateFormat.format(d));
    int offset_millis = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
    int offset_hours = Math.abs(offset_millis / (1000 * 60 * 60));
    int offset_minutes = Math.abs((offset_millis / (1000 * 60)) % 60);

    if (offset_millis == 0) {
        result.append("Z");
    } else {
        result.append((offset_millis > 0) ? "+" : "-").append(doubleDigit.format(offset_hours)).append(":")
                .append(doubleDigit.format(offset_minutes));
    }

    return result.toString();
}

From source file:org.codice.alliance.nsili.source.NsiliFilterDelegateTest.java

private static Date getDate() {
    Calendar calendar = new GregorianCalendar(YEAR, MONTH, DAY);
    calendar.setTimeZone(TimeZone.getTimeZone(NsiliFilterDelegate.UTC));
    return calendar.getTime();
}

From source file:Currently.java

/**
 * Parses one of the ISO 8601 that it produces. Note, it will not
 * parse the full range of ISO timestamps.
 *
 * @param stamp is the textual timestamp representation.
 * @return a time or <code>null</code>, if unparsable.
 *///from   www.  j a  v  a 2 s.c  om
public static Date parse(String stamp) {
    // initialize the compiled expressions once
    if (c_pattern == null) {
        c_pattern = new Pattern[c_expression.length];
        for (int i = 0; i < c_expression.length; ++i) {
            c_pattern[i] = Pattern.compile(c_expression[i]);
        }
    }

    // match against pattern
    for (int i = 0; i < c_expression.length; ++i) {
        Matcher m = c_pattern[i].matcher(stamp);
        if (m.matches()) {
            Calendar c = Calendar.getInstance();
            TimeZone z = TimeZone.getDefault();
            if (m.group(9) != null && m.group(9).length() > 0) {
                boolean utc = (Character.toUpperCase(m.group(9).charAt(0)) == 'Z');
                if (utc) {
                    z = TimeZone.getTimeZone("GMT+0");
                } else {
                    z = TimeZone.getTimeZone("GMT" + m.group(9));
                }
            }

            c.setTimeZone(z);
            c.set(Calendar.YEAR, Integer.parseInt(m.group(1)));
            c.set(Calendar.MONTH, Integer.parseInt(m.group(2)) + (Calendar.JANUARY - 1));
            c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(m.group(3)));

            if (m.group(4).length() > 0) {
                c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(5)));
                c.set(Calendar.MINUTE, Integer.parseInt(m.group(6)));
                if (m.group(7) != null && m.group(7).length() > 0) {
                    c.set(Calendar.SECOND, Integer.parseInt(m.group(7)));
                }
                if (m.group(8) != null && m.group(8).length() > 1) {
                    String millis = m.group(8).substring(1);
                    while (millis.length() < 3) {
                        millis += "0";
                    }
                    millis = millis.substring(0, 3);
                    c.set(Calendar.MILLISECOND, Integer.parseInt(millis));
                }
            }

            return c.getTime();
        }
    }

    // not found
    return null;
}