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:org.apache.ranger.audit.provider.MiscUtil.java

public static Date getUTCDateForLocalDate(Date date) {
    TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT+0");
    Calendar local = Calendar.getInstance();
    int offset = local.getTimeZone().getOffset(local.getTimeInMillis());
    GregorianCalendar utc = new GregorianCalendar(gmtTimeZone);
    utc.setTimeInMillis(date.getTime());
    utc.add(Calendar.MILLISECOND, -offset);
    return utc.getTime();
}

From source file:org.apache.ranger.audit.provider.MiscUtil.java

public static Date getUTCDate() {
    TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT+0");
    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();
}

From source file:com.google.sampling.experiential.server.EventServlet.java

public static DateTimeZone getTimeZoneForClient(HttpServletRequest req) {
    String tzStr = getParam(req, "tz");
    if (tzStr != null && !tzStr.isEmpty()) {
        DateTimeZone jodaTimeZone = DateTimeZone.forID(tzStr);
        return jodaTimeZone;
    } else {/*  www  .  j  a va  2  s . co m*/
        Locale clientLocale = req.getLocale();
        Calendar calendar = Calendar.getInstance(clientLocale);
        TimeZone clientTimeZone = calendar.getTimeZone();
        DateTimeZone jodaTimeZone = DateTimeZone.forTimeZone(clientTimeZone);
        return jodaTimeZone;
    }
}

From source file:org.floggy.synchronization.jme.core.impl.JSONSerializationManager.java

/**
* DOCUMENT ME!//from  w w  w .jav  a 2s .  c om
*
* @param value DOCUMENT ME!
* @param stringer DOCUMENT ME!
*
* @throws JSONException DOCUMENT ME!
*/
public static void toJSON(Calendar value, JSONStringer stringer) throws JSONException {
    if (value == null) {
        stringer.value(null);
    } else {
        stringer.object();
        stringer.key("timeZone");
        toJSON(value.getTimeZone(), stringer);
        stringer.key("time").value(value.getTime().getTime());
        stringer.endObject();
    }
}

From source file:com.sirma.itt.emf.time.ISO8601DateFormat.java

/**
 * Format date into ISO format.//from ww  w.  j  a va 2 s .co m
 * 
 * @param isoDate
 *            the date to format
 * @return the ISO formatted string
 */
public static String format(Date isoDate) {
    if (isoDate == null) {
        return null;
    }
    // Note: always serialise to Gregorian Calendar
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(isoDate);

    StringBuilder formatted = new StringBuilder(28);
    padInt(formatted, calendar.get(Calendar.YEAR), 4);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.MONTH) + 1, 2);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.DAY_OF_MONTH), 2);
    formatted.append('T');
    padInt(formatted, calendar.get(Calendar.HOUR_OF_DAY), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.MINUTE), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.SECOND), 2);
    formatted.append('.');
    padInt(formatted, calendar.get(Calendar.MILLISECOND), 3);

    TimeZone tz = calendar.getTimeZone();
    int offset = tz.getOffset(calendar.getTimeInMillis());
    formatted.append(getTimeZonePadding(offset));

    return formatted.toString();
}

From source file:com.sirma.itt.emf.time.ISO8601DateFormat.java

/**
 * Format calendar instance into ISO format.
 * /*  ww w.  j  av a  2 s  .  c  o  m*/
 * @param calendar
 *            the calendar instance to format
 * @return the ISO formatted string
 */
public static String format(Calendar calendar) {
    if (calendar == null) {
        return null;
    }

    StringBuilder formatted = new StringBuilder(28);
    padInt(formatted, calendar.get(Calendar.YEAR), 4);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.MONTH) + 1, 2);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.DAY_OF_MONTH), 2);
    formatted.append('T');
    padInt(formatted, calendar.get(Calendar.HOUR_OF_DAY), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.MINUTE), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.SECOND), 2);
    formatted.append('.');
    padInt(formatted, calendar.get(Calendar.MILLISECOND), 3);

    TimeZone tz = calendar.getTimeZone();
    int offset = tz.getOffset(calendar.getTimeInMillis());
    formatted.append(getTimeZonePadding(offset));

    return formatted.toString();
}

From source file:mvc.TestController.java

@RequestMapping("/testTimeZone")
public String testTimeZone(Map<String, Object> model) {
    Calendar cl = Calendar.getInstance();
    model.put("timezone", cl.getTimeZone());
    model.put("date", cl.getTime());
    return "Test_timezone";
}

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

public Object convert(Class destClass, Object srcObj) {
    Calendar result = new GregorianCalendar();
    Class srcFieldClass = srcObj.getClass();
    // Convert from Date to Calendar
    if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
        result.setTime((java.util.Date) srcObj);
    }/* ww w.  jav  a  2s .c o m*/
    //  Convert from Calendar to Calendar
    else if (Calendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar c = (Calendar) srcObj;
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar c = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    }
    // String to Calendar
    else if (dateFormat != null && String.class.isAssignableFrom(srcFieldClass)) {
        try {
            result.setTime(new Date(dateFormat.parse((String) srcObj).getTime()));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
        // Default conversion
    } else {
        try {
            result.setTime(new Date(Long.parseLong(srcObj.toString())));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }
    return result;
}

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

public Object convert(Class destClass, Object srcObj) {
    Calendar result = new GregorianCalendar();
    Class srcFieldClass = srcObj.getClass();
    // Convert from Date to Calendar
    if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
        result.setTime((java.util.Date) srcObj);
    } else if (Calendar.class.isAssignableFrom(srcFieldClass)) {
        //  Convert from Calendar to Calendar
        Calendar c = (Calendar) srcObj;
        result.setTime(c.getTime());/* w ww .  java 2  s. c  o m*/
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar c = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (dateFormat != null && String.class.isAssignableFrom(srcFieldClass)) {
        // String to Calendar
        try {
            result.setTime(new Date(dateFormat.parse((String) srcObj).getTime()));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
    } else {
        // Default conversion
        try {
            result.setTime(new Date(Long.parseLong(srcObj.toString())));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

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

    return result;
}

From source file:org.seedstack.monitoring.batch.internal.rest.jobinstance.JobInstanceResource.java

/**
 * Retrieves the details of a job instance by id.
 *
 * @param jobName       the job name/*from  www  .jav a 2 s. co m*/
 * @param jobInstanceId the job instance id
 * @return the response
 */
@GET
@Path("/{jobInstanceId}")
@Produces(MediaType.APPLICATION_JSON)
public Response details(@PathParam("jobName") String jobName, @PathParam("jobInstanceId") long jobInstanceId) {

    JobInstance jobInstance;
    Calendar calendar = new GregorianCalendar();
    TimeZone timeZone = calendar.getTimeZone();
    try {
        jobInstance = jobService.getJobInstance(jobInstanceId);
        if (!jobInstance.getJobName().equals(jobName)) {

            String error = "wrong.job.name " + jobName + " The JobInstance with id =" + jobInstanceId
                    + " has the wrong name " + jobInstance.getJobName() + " not " + jobName;

            return Response.status(Response.Status.BAD_REQUEST).entity(error).type(MediaType.TEXT_PLAIN)
                    .build();
        }
    } catch (NoSuchJobInstanceException e) {
        return Response.status(Response.Status.BAD_REQUEST)
                .entity("There is no such job (" + jobName + ") with JobInstanceID = " + jobInstanceId)
                .type(MediaType.TEXT_PLAIN).build();
    }
    Collection<JobExecutionInfo> jobInstancesRepresentations = new ArrayList<JobExecutionInfo>();
    try {
        Collection<JobExecution> jobExecutions = jobService.getJobExecutionsForJobInstance(jobName,
                jobInstanceId);
        for (JobExecution jobExecution : jobExecutions) {
            jobInstancesRepresentations.add(new JobExecutionInfo(jobExecution, timeZone));
        }
    } catch (NoSuchJobException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity("There is no such job (" + jobName + ")")
                .type(MediaType.TEXT_PLAIN).build();
    }

    return Response.ok(jobInstancesRepresentations).build();

}