Example usage for java.util GregorianCalendar setTimeZone

List of usage examples for java.util GregorianCalendar setTimeZone

Introduction

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

Prototype

@Override
    public void setTimeZone(TimeZone zone) 

Source Link

Usage

From source file:ee.ria.xroad.confproxy.commandline.ConfProxyUtilGenerateAnchor.java

/**
 * Generates an achor xml file based on the provided proxy configuration
 * properties and writes it to the provided output stream.
 * @param conf configuration proxy properties instance
 * @param instanceIdentifier instance identifier of the resulting anchor
 * @param out the output stream for writing the generated xml
 * @throws Exception if xml generation fails
 *///from ww w .ja v a  2 s  .  c  o m
private void generateAnchorXml(final ConfProxyProperties conf, final String instanceIdentifier,
        final OutputStream out) throws Exception {
    JAXBContext jaxbCtx = JAXBContext.newInstance(ObjectFactory.class);
    Marshaller marshaller = jaxbCtx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    ObjectFactory factory = new ObjectFactory();
    ConfigurationSourceType sourceType = factory.createConfigurationSourceType();
    sourceType.setDownloadURL(conf.getConfigurationProxyURL() + "/" + OutputBuilder.SIGNED_DIRECTORY_NAME);
    for (byte[] cert : conf.getVerificationCerts()) {
        sourceType.getVerificationCert().add(cert);
    }
    ConfigurationAnchorType anchorType = factory.createConfigurationAnchorType();
    anchorType.setInstanceIdentifier(instanceIdentifier);
    GregorianCalendar gcal = new GregorianCalendar();
    gcal.setTimeZone(TimeZone.getTimeZone("UTC"));
    XMLGregorianCalendar xgcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
    anchorType.setGeneratedAt(xgcal);
    anchorType.getSource().add(sourceType);
    JAXBElement<ConfigurationAnchorType> root = factory.createConfigurationAnchor(anchorType);

    marshaller.marshal(root, out);
}

From source file:org.apache.eagle.jpm.mr.history.metrics.JobCountMetricsGenerator.java

public void flush(String date, int year, int month, int day) throws Exception {
    List<Pair<String, String>> jobs = JobHistoryZKStateManager.instance().getProcessedJobs(date);
    final int total = jobs.size();
    int succeeded = 0;
    int killed = 0;

    for (Pair<String, String> job : jobs) {
        if (job.getRight().equals(EagleJobStatus.KILLED.toString())) {
            ++killed;//from w w  w  . j  a va  2  s  .c o m
        }

        if (job.getRight().equals(EagleJobStatus.SUCCEEDED.toString())) {
            ++succeeded;
        }
    }
    int failed = total - killed - succeeded;

    EagleServiceConfig eagleServiceConfig = appConfig.getEagleServiceConfig();
    final IEagleServiceClient client = new EagleServiceClientImpl(eagleServiceConfig.eagleServiceHost,
            eagleServiceConfig.eagleServicePort, eagleServiceConfig.username, eagleServiceConfig.password);

    GregorianCalendar cal = new GregorianCalendar(year, month, day);
    cal.setTimeZone(timeZone);

    List<GenericMetricEntity> entities = new ArrayList<>();
    entities.add(generateEntity(cal, EagleJobStatus.FAILED.toString(), failed));
    entities.add(generateEntity(cal, EagleJobStatus.KILLED.toString(), killed));
    entities.add(generateEntity(cal, EagleJobStatus.SUCCEEDED.toString(), succeeded));

    LOG.info("start flushing entities of total number " + entities.size());
    client.create(entities);
    LOG.info("finish flushing entities of total number " + entities.size());
    client.getJerseyClient().destroy();
    client.close();
}

From source file:org.energyos.espi.datacustodian.web.api.ExportServiceTests.java

private GregorianCalendar getGregorianCalendar(int secondsFromEpoch) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    cal.setTimeInMillis(secondsFromEpoch * 1000);
    return cal;//from   ww  w .j a  v a 2 s .  co  m
}

From source file:be.fedict.eid.pkira.portal.util.TypeMapper.java

public XMLGregorianCalendar map(Date date) {
    if (date == null)
        return null;

    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);/* w  ww .j av  a 2 s  .co  m*/
    calendar.setTimeZone(TimeZone.getTimeZone("GMT"));

    return getDatatypeFactory().newXMLGregorianCalendar(calendar);
}

From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java

@Test
public void dateToJson() throws IOException {
    GregorianCalendar calendar = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.add(Calendar.DATE, 10000);
    java.util.Date date = calendar.getTime();

    JsonNode converted = converter.fromConnectData(Date.SCHEMA, date);
    assertTrue(converted.isTextual());//from w w w . j a  v a2s . c  o  m
    assertEquals(SimpleJsonConverter.ISO_DATE_FORMAT.format(date), converted.textValue());
}

From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java

@Test
public void timeToJson() throws IOException {
    GregorianCalendar calendar = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.add(Calendar.MILLISECOND, 14400000);
    java.util.Date date = calendar.getTime();

    JsonNode converted = converter.fromConnectData(Time.SCHEMA, date);
    assertTrue(converted.isTextual());// www .j  a  va2s. com
    assertEquals(SimpleJsonConverter.TIME_FORMAT.format(date), converted.textValue());
}

From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java

@Test
public void timestampToJson() throws IOException {
    GregorianCalendar calendar = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.add(Calendar.MILLISECOND, 2000000000);
    calendar.add(Calendar.MILLISECOND, 2000000000);
    java.util.Date date = calendar.getTime();

    JsonNode converted = converter.fromConnectData(Timestamp.SCHEMA, date);
    assertTrue(converted.isLong());/*from  ww  w.  j av a 2  s  . c  om*/
    assertEquals(4000000000L, converted.longValue());
}

From source file:org.getobjects.appserver.core.WOMessage.java

public static String httpFormatDate(final GregorianCalendar _cal) {
    /*/*from   w w  w  .j av  a 2  s .c o  m*/
     * Most likely some Java lib already provides this formatter ... It
     * basically always formats UTC in the English locale and therefore can be
     * hardcoded.
     *
     * Sample: Wed, 15 Nov 1995 04:58:08 GMT
     */
    if (_cal == null)
        return null;

    _cal.setTimeZone(gmt);

    final StringBuilder sb = new StringBuilder(32);
    sb.append(httpDayNames[_cal.get(Calendar.DAY_OF_WEEK) - 1]);
    sb.append(", ");
    int t = _cal.get(Calendar.DAY_OF_MONTH);
    sb.append(t < 10 ? "0" + t : t);
    sb.append(' ');
    sb.append(httpMonthNames[_cal.get(Calendar.MONTH)]);
    sb.append(' ');
    sb.append(_cal.get(Calendar.YEAR));
    sb.append(' ');
    t = _cal.get(Calendar.HOUR_OF_DAY /* 0..23 */);
    sb.append(t < 10 ? "0" + t : t);
    sb.append(':');
    t = _cal.get(Calendar.MINUTE);
    sb.append(t < 10 ? "0" + t : t);
    sb.append(':');
    t = _cal.get(Calendar.SECOND);
    sb.append(t < 10 ? "0" + t : t);
    sb.append(" GMT");

    return sb.toString();
}

From source file:net.acesinc.nifi.processors.json.BetterAttributesToJSON.java

/**
 * Builds the Map of attributes that should be included in the JSON that is
 * emitted from this process./*  w w w  .j  a  v a 2  s.c o m*/
 *
 * @param ff
 * @param atrListForStringValues
 * @param atrListForIntValues
 * @param atrListForDoubleValues
 * @param atrListForLongEpochToGoToDateValues
 * @return Map of values that are feed to a Jackson ObjectMapper
 * @throws java.io.IOException
 */
protected Map<String, Object> buildAttributesMapAndBringInFlowAttrs(FlowFile ff, String atrListForStringValues,
        String atrListForIntValues, String atrListForDoubleValues, String atrListForLongEpochToGoToDateValues)
        throws IOException {
    Map<String, Object> atsToWrite = new HashMap<>();

    //handle all the string values
    //If list of attributes specified get only those attributes. Otherwise write them all
    if (StringUtils.isNotBlank(atrListForStringValues)) {
        String[] ats = StringUtils.split(atrListForStringValues, AT_LIST_SEPARATOR);
        if (ats != null) {
            for (String str : ats) {
                String cleanStr = str.trim();
                String val = ff.getAttribute(cleanStr);
                if (val != null) {
                    atsToWrite.put(cleanStr, val);
                } else {
                    atsToWrite.put(cleanStr, "");
                }
            }
        }

    } else {
        atsToWrite.putAll(ff.getAttributes());
    }
    //handle all int values
    if (StringUtils.isNotBlank(atrListForIntValues)) {
        String[] ats = StringUtils.split(atrListForIntValues, AT_LIST_SEPARATOR);
        if (ats != null) {
            for (String str : ats) {
                String cleanStr = str.trim();
                String val = ff.getAttribute(cleanStr);
                if (val != null) {
                    atsToWrite.put(cleanStr, Integer.parseInt(val));
                } else {
                    atsToWrite.put(cleanStr, null);
                }
            }
        }
    }
    //handle all double values
    if (StringUtils.isNotBlank(atrListForDoubleValues)) {
        String[] ats = StringUtils.split(atrListForDoubleValues, AT_LIST_SEPARATOR);
        if (ats != null) {
            for (String str : ats) {
                String cleanStr = str.trim();
                String val = ff.getAttribute(cleanStr);
                if (val != null) {
                    atsToWrite.put(cleanStr, Double.parseDouble(val));
                } else {
                    atsToWrite.put(cleanStr, null);
                }
            }
        }
    }
    //handle all date values
    if (StringUtils.isNotBlank(atrListForLongEpochToGoToDateValues)) {
        String[] ats = StringUtils.split(atrListForLongEpochToGoToDateValues, AT_LIST_SEPARATOR);
        if (ats != null) {
            for (String str : ats) {
                String cleanStr = str.trim();
                String val = ff.getAttribute(cleanStr);
                if (val != null) {
                    long epochTime = Long.parseLong(val);
                    GregorianCalendar gcal = new GregorianCalendar();
                    gcal.setTimeZone(TimeZone.getTimeZone(MONGO_TIME_ZONE));
                    gcal.setTimeInMillis(epochTime);
                    SimpleDateFormat sdf = new SimpleDateFormat(MONGO_DATE_TEMPLATE);
                    String mongoDate = sdf.format(gcal.getTime());
                    //to Date
                    Map<String, String> isoDate = new HashMap<>();
                    isoDate.put("$date", mongoDate);
                    atsToWrite.put(cleanStr, isoDate);
                } else {
                    atsToWrite.put(cleanStr, null);
                }
            }
        }
    }

    return atsToWrite;
}

From source file:org.jfree.data.time.SecondTest.java

/**
 * Some checks for the getFirstMillisecond(TimeZone) method.
 *//*from   w w  w .j  a  v  a  2s .  c o m*/
@Test
public void testGetFirstMillisecondWithCalendar() {
    Second s = new Second(55, 40, 2, 15, 4, 2000);
    GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
    calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
    assertEquals(955766455000L, s.getFirstMillisecond(calendar));

    // try null calendar
    boolean pass = false;
    try {
        s.getFirstMillisecond((Calendar) null);
    } catch (NullPointerException e) {
        pass = true;
    }
    assertTrue(pass);
}