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:com.android.projecte.townportal.WeatherInfo.java

public String convertDate(long unixTime) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(unixTime * 1000);
    sdf.setTimeZone(cal.getTimeZone());
    return sdf.format(cal.getTime());
}

From source file:com.esri.geoportal.harvester.api.base.DataReferenceSerializer.java

private String formatIsoDate(Date date) {
    Instant instant = date.toInstant();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*ww  w  . j a va  2s. c  om*/
    ZoneOffset zoneOffset = ZoneOffset.ofHours(cal.getTimeZone().getRawOffset() / (1000 * 60 * 60));
    OffsetDateTime ofInstant = OffsetDateTime.ofInstant(instant, zoneOffset);
    return FORMATTER.format(ofInstant);
}

From source file:org.dozer.converters.XMLGregorianCalendarConverter.java

/**
 * {@inheritDoc}/*  w w w.  j ava2s . c om*/
 */
public Object convert(Class destClass, Object srcObj) {
    Class sourceClass = srcObj.getClass();
    Calendar result = new GregorianCalendar();

    if (java.util.Date.class.isAssignableFrom(sourceClass)) {
        // Date --> XMLGregorianCalendar
        result.setTime((java.util.Date) srcObj);
    } else if (Calendar.class.isAssignableFrom(sourceClass)) {
        // Calendar --> XMLGregorianCalendar
        Calendar c = (Calendar) srcObj;
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(sourceClass)) {
        result = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
    } else if (dateFormat != null && String.class.isAssignableFrom(sourceClass)) {
        if ("".equals(srcObj)) {
            return null;
        }
        try {
            long time = dateFormat.parse((String) srcObj).getTime();
            result.setTime(new Date(time));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
    } else {
        try {
            long time = Long.parseLong(srcObj.toString());
            result.setTime(new Date(time));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    return dataTypeFactory().newXMLGregorianCalendar((GregorianCalendar) result);
}

From source file:org.opentides.dao.impl.UserDaoJpaImpl.java

public void updateLastLogin(String username) {
    Calendar now = Calendar.getInstance(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
    Date currentDate = DateUtil.getClientCurrentDate(now, now.getTimeZone());
    Query update = getEntityManager().createNativeQuery("update USER_PROFILE up, USERS u set LASTLOGIN='"
            + DateUtil.dateToString(currentDate, "yyyy-MM-dd HH:mm:ss") + "' where u.id=up.id and u.username='"
            + username + "'");
    update.executeUpdate();/*from  w  w  w .  j  a  va2 s  . c  om*/
}

From source file:com.synelixis.xifi.AuthWebClient.AToken.java

private Date getGMTime() {
    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 .  ja v  a2s .  co  m
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;
    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));
    return c.getTime();

}

From source file:com.github.dozermapper.core.converters.XMLGregorianCalendarConverter.java

/**
 * {@inheritDoc}/*from  w  w w  . j  av  a 2  s. c  o m*/
 */
public Object convert(Class destClass, Object srcObj) {
    Class sourceClass = srcObj.getClass();
    Calendar result = new GregorianCalendar();

    if (java.util.Date.class.isAssignableFrom(sourceClass)) {
        // Date --> XMLGregorianCalendar
        result.setTime(java.util.Date.class.cast(srcObj));
    } else if (Calendar.class.isAssignableFrom(sourceClass)) {
        // Calendar --> XMLGregorianCalendar
        Calendar c = Calendar.class.cast(srcObj);
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(sourceClass)) {
        result = XMLGregorianCalendar.class.cast(srcObj).toGregorianCalendar();
    } else if (dateFormat != null && String.class.isAssignableFrom(sourceClass)) {
        if ("".equals(String.class.cast(srcObj))) {
            return null;
        }

        try {
            long time = dateFormat.parse(String.class.cast(srcObj)).getTime();
            result.setTime(new Date(time));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
    } else {
        try {
            long time = Long.parseLong(srcObj.toString());
            result.setTime(new Date(time));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    if (dateFormat != null && String.class.isAssignableFrom(destClass)) {
        return dateFormat.format(result.getTime());
    }

    return dataTypeFactory().newXMLGregorianCalendar(GregorianCalendar.class.cast(result));
}

From source file:com.amediamanager.util.S3FormSigner.java

/**
 * The UploadPolicy method creates the S3 upload policy for the aMediaManager application.
 * Much of this is hard coded and would have to change with any changes to the fields in the S3
 * upload form./*  ww w.  ja v a2s  .com*/
 *
 * @param key            this is not currently used.
 * @param redirectUrl    this is the URL to which S3 will redirect the browser on successful upload.
 * @return                the upload policy string is returned.
 */
String generateUploadPolicy(String s3BucketName, String keyPrefix, AWSCredentialsProvider credsProvider,
        String redirectUrl) {

    Calendar dateTime = Calendar.getInstance();
    // add the offset from UTC
    dateTime.add(Calendar.MILLISECOND, -dateTime.getTimeZone().getOffset(dateTime.getTimeInMillis()));
    // add 15 minutes more for skew
    dateTime.add(Calendar.MINUTE, 15);
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

    String expirationDate = dateFormatter.format(dateTime.getTime());

    StringBuilder sb = new StringBuilder();
    sb.append("{ \"expiration\": \"" + expirationDate + "\",");
    sb.append("\"conditions\": [ { \"bucket\": \"" + s3BucketName + "\" }, ");
    sb.append("[\"starts-with\", \"$key\", \"" + keyPrefix + "/\"], ");
    sb.append("{ \"success_action_redirect\": \"" + redirectUrl + "\" },");
    sb.append("[\"eq\", \"$x-amz-meta-bucket\", \"" + s3BucketName + "\"], ");
    sb.append("[\"starts-with\", \"$x-amz-meta-owner\", \"\"], ");
    sb.append("[\"starts-with\", \"$x-amz-meta-uuid\", \"\"], ");
    sb.append("[\"starts-with\", \"$x-amz-meta-title\", \"\"], ");
    sb.append("[\"starts-with\", \"$x-amz-meta-tags\", \"\"], ");
    sb.append("[\"starts-with\", \"$x-amz-meta-createdDate\", \"\"], ");
    sb.append("[\"starts-with\", \"$x-amz-meta-description\", \"\"], ");
    sb.append("[\"starts-with\", \"$x-amz-meta-privacy\", \"\"], ");
    sb.append("[\"starts-with\", \"$Content-Type\", \"video/\"], ");

    if (credsProvider.getCredentials() instanceof BasicSessionCredentials) {
        sb.append("[\"starts-with\", \"$x-amz-security-token\", \"\"], ");
    }

    sb.append("[\"content-length-range\", 0, 1073741824] ] }");
    return sb.toString();
}

From source file:org.apache.synapse.mediators.deprecation.DeprecationMediator.java

private boolean isDeprecated(SynapseObject service) {

    try {/*from w  w  w  . j  a va 2 s  .co  m*/
        if (service.getBoolean("enabled").booleanValue()) {
            Calendar current = Calendar.getInstance();
            TimeZone tz = current.getTimeZone();
            int offset = tz.getRawOffset();
            Calendar calendar = new GregorianCalendar(tz);

            DateFormat df = new SimpleDateFormat("d/M/y:H:m");
            df.setTimeZone(tz);
            Date d1 = service.getDate(DeprecationConstants.CFG_DEPRECATION_FROM_DATE);
            Calendar fromCalendar = new GregorianCalendar(tz);
            d1.setTime(d1.getTime() + offset);
            fromCalendar.setTime(d1);
            String toDate = service.getDate(DeprecationConstants.CFG_DEPRECATION_TO_DATE).toString();
            if (toDate == null || (toDate.length() == 0)) {
                return calendar.before(fromCalendar);
            }

            Date d2 = service.getDate("toDate");
            Calendar toCalendar = new GregorianCalendar(tz);
            d2.setTime(d2.getTime() + offset);
            toCalendar.setTime(d2);
            return (calendar.after(fromCalendar) && calendar.before(toCalendar));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}

From source file:com.scoreminion.GameAdapter.java

/**
 * Converts UTC date string to localized date string.
 *
 * @param utcDateStr the date string returned from the Scores API
 * @return the localized date string/*w  w w . j  a v a  2s  .  c o  m*/
 */
private String convertDateString(String utcDateStr) {
    Calendar rightNow = Calendar.getInstance();
    TimeZone timeZone = rightNow.getTimeZone();
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
    Date date = dateFormat.parse(utcDateStr, new ParsePosition(DATE_PARSE_POSITION_OFFSET));
    Date adjustedDate = new Date(date.getTime() + timeZone.getOffset(rightNow.getTimeInMillis()));

    return dateFormat.format(adjustedDate);
}

From source file:org.psystems.dicom.browser.server.stat.StatClientRequestsChartServlet.java

/**
 * Returns a sample dataset.// w  w  w  .  j a  v  a 2s.c  o  m
 * 
 * @return The dataset.
 */
private CategoryDataset createDataset() {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    Calendar calendarEnd = Calendar.getInstance();
    int tzoffset = calendarEnd.getTimeZone().getOffset(calendarEnd.getTimeInMillis());

    long time = calendarEnd.getTimeInMillis();
    time = time - (time % (60 * 60 * 24 * 1000)) - tzoffset;
    calendarEnd.setTimeInMillis(time);

    try {
        Connection connection = Util.getConnection("main", getServletContext());

        Calendar calendarBegin = (Calendar) calendarEnd.clone();
        calendarBegin.add(Calendar.DAY_OF_MONTH, -7);

        getMetrics(connection, " ?", "CLIENT_CONNECTIONS",
                calendarBegin.getTimeInMillis(), calendarEnd.getTimeInMillis(), dataset);

    } catch (SQLException e) {
        logger.error(e);
        e.printStackTrace();
    }
    return dataset;
}