Example usage for java.time ZonedDateTime getOffset

List of usage examples for java.time ZonedDateTime getOffset

Introduction

In this page you can find the example usage for java.time ZonedDateTime getOffset.

Prototype

@Override
public ZoneOffset getOffset() 

Source Link

Document

Gets the zone offset, such as '+01:00'.

Usage

From source file:Main.java

public static void main(String[] args) {
    ZonedDateTime dateTime = ZonedDateTime.now();

    System.out.println(dateTime.getOffset());
}

From source file:Main.java

public static void main(String[] args) {
    Set<String> allZones = ZoneId.getAvailableZoneIds();
    List<String> zoneList = new ArrayList<String>(allZones);
    Collections.sort(zoneList);/* w ww.  ja  v  a  2 s  .c  o  m*/

    LocalDateTime dt = LocalDateTime.now();
    for (String s : zoneList) {
        ZoneId zone = ZoneId.of(s);
        ZonedDateTime zdt = dt.atZone(zone);
        ZoneOffset offset = zdt.getOffset();
        String out = String.format("%35s %10s%n", zone, offset);
        System.out.println(out);
    }
}

From source file:Main.java

public static void main(String[] args) {
    ZoneId berlin = ZoneId.of("Europe/Berlin");
    ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
    // 2014-02-20 12:00
    LocalDateTime dateTime = LocalDateTime.of(2014, 02, 20, 12, 0);

    // 2014-02-20 12:00, Europe/Berlin (+01:00)
    ZonedDateTime berlinDateTime = ZonedDateTime.of(dateTime, berlin);

    // 2014-02-20 03:00, America/Los_Angeles (-08:00)
    ZonedDateTime losAngelesDateTime = berlinDateTime.withZoneSameInstant(losAngeles);

    int offsetInSeconds = losAngelesDateTime.getOffset().getTotalSeconds(); // -28800

    System.out.println(offsetInSeconds);
}

From source file:Main.java

/**
 * Checks if the type of the given date. Possible return values are standard
 * time, the date when to switch to daylight saving time (in Europe the last
 * Sunday in March), daylight saving time or the date when to switch back to
 * standard time (in Europe the last Sunday in October).
 * /*from w ww.ja v a 2 s  . co  m*/
 * @return DayType
 * @param cal
 *          Date to check, cannot be null
 */
public static DayType getDSTType(LocalDate cal) {
    DayType status = DayType.DAYLIGHT_SAVING_TIME;
    LocalDateTime testDate = cal.atStartOfDay();
    ZonedDateTime zdt = ZonedDateTime.of(testDate, ZoneId.systemDefault());
    // Find type of day
    if (zdt.getZone().getRules().isDaylightSavings(testDate.toInstant(zdt.getOffset())))
        status = DayType.DAYLIGHT_SAVING_TIME;
    else
        status = DayType.STANDARD_TIME;
    // Check the day after
    testDate = testDate.plusDays(1);
    zdt = ZonedDateTime.of(testDate, ZoneId.systemDefault());
    // Find type of day after
    if (zdt.getZone().getRules().isDaylightSavings(testDate.toInstant(zdt.getOffset()))) {
        if (status != DayType.DAYLIGHT_SAVING_TIME)
            status = DayType.TO_DAYLIGHT_SAVING_TIME;
    } else {
        if (status == DayType.DAYLIGHT_SAVING_TIME)
            status = DayType.TO_STANDARD_TIME;
    }
    return status;
}

From source file:org.openmhealth.shim.withings.mapper.WithingsDailyStepCountDataPointMapper.java

/**
 * Maps an individual list node from the array in the Withings activity measure endpoint response into a {@link
 * StepCount} data point.//w w  w  . java2  s . co  m
 *
 * @param node activity node from the array "activites" contained in the "body" of the endpoint response
 * @return a {@link DataPoint} object containing a {@link StepCount} measure with the appropriate values from
 * the JSON node parameter, wrapped as an {@link Optional}
 */
@Override
Optional<DataPoint<StepCount>> asDataPoint(JsonNode node) {

    long stepValue = asRequiredLong(node, "steps");
    StepCount.Builder stepCountBuilder = new StepCount.Builder(stepValue);
    Optional<String> dateString = asOptionalString(node, "date");
    Optional<String> timeZoneFullName = asOptionalString(node, "timezone");
    // We assume that timezone is the same for both the startdate and enddate timestamps, even though Withings only
    // provides the enddate timezone as the "timezone" property.
    // TODO: Revisit once Withings can provide start_timezone and end_timezone
    if (dateString.isPresent() && timeZoneFullName.isPresent()) {
        LocalDateTime localStartDateTime = LocalDate.parse(dateString.get()).atStartOfDay();
        ZoneId zoneId = ZoneId.of(timeZoneFullName.get());
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localStartDateTime, zoneId);
        ZoneOffset offset = zonedDateTime.getOffset();
        OffsetDateTime offsetStartDateTime = OffsetDateTime.of(localStartDateTime, offset);
        LocalDateTime localEndDateTime = LocalDate.parse(dateString.get()).atStartOfDay().plusDays(1);
        OffsetDateTime offsetEndDateTime = OffsetDateTime.of(localEndDateTime, offset);
        stepCountBuilder.setEffectiveTimeFrame(
                TimeInterval.ofStartDateTimeAndEndDateTime(offsetStartDateTime, offsetEndDateTime));
    }

    Optional<String> userComment = asOptionalString(node, "comment");
    if (userComment.isPresent()) {
        stepCountBuilder.setUserNotes(userComment.get());
    }

    StepCount stepCount = stepCountBuilder.build();
    DataPoint<StepCount> stepCountDataPoint = newDataPoint(stepCount, null, true, null);

    return Optional.of(stepCountDataPoint);
}

From source file:org.openmhealth.shim.withings.mapper.WithingsDailyCaloriesBurnedDataPointMapper.java

/**
 * Maps an individual list node from the array in the Withings activity measure endpoint response into a {@link
 * CaloriesBurned} data point.// ww  w  . j av a  2  s.  co m
 * <p>
 * <p>Note: the start datetime and end datetime values for the mapped {@link CaloriesBurned} {@link DataPoint}
 * assume that
 * the start timezone and end time zone are the same, both equal to the "timezone" property in the Withings
 * response
 * datapoints. However, according to Withings, the property value they provide is specifically the end datetime
 * timezone.</p>
 *
 * @param node activity node from the array "activites" contained in the "body" of the endpoint response
 * @return a {@link DataPoint} object containing a {@link CaloriesBurned} measure with the appropriate values from
 * the JSON node parameter, wrapped as an {@link Optional}
 */
@Override
Optional<DataPoint<CaloriesBurned>> asDataPoint(JsonNode node) {

    long caloriesBurnedValue = asRequiredLong(node, "calories");
    CaloriesBurned.Builder caloriesBurnedBuilder = new CaloriesBurned.Builder(
            new KcalUnitValue(KcalUnit.KILOCALORIE, caloriesBurnedValue));

    Optional<String> dateString = asOptionalString(node, "date");
    Optional<String> timeZoneFullName = asOptionalString(node, "timezone");
    // We assume that timezone is the same for both the startdate and enddate timestamps, even though Withings only
    // provides the enddate timezone as the "timezone" property.
    // TODO: Revisit once Withings can provide start_timezone and end_timezone
    if (dateString.isPresent() && timeZoneFullName.isPresent()) {
        LocalDateTime localStartDateTime = LocalDate.parse(dateString.get()).atStartOfDay();
        ZoneId zoneId = ZoneId.of(timeZoneFullName.get());
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localStartDateTime, zoneId);
        ZoneOffset offset = zonedDateTime.getOffset();
        OffsetDateTime offsetStartDateTime = OffsetDateTime.of(localStartDateTime, offset);
        LocalDateTime localEndDateTime = LocalDate.parse(dateString.get()).atStartOfDay().plusDays(1);

        OffsetDateTime offsetEndDateTime = OffsetDateTime.of(localEndDateTime, offset);
        caloriesBurnedBuilder.setEffectiveTimeFrame(
                TimeInterval.ofStartDateTimeAndEndDateTime(offsetStartDateTime, offsetEndDateTime));
    }

    Optional<String> userComment = asOptionalString(node, "comment");
    if (userComment.isPresent()) {
        caloriesBurnedBuilder.setUserNotes(userComment.get());
    }

    CaloriesBurned caloriesBurned = caloriesBurnedBuilder.build();
    DataPoint<CaloriesBurned> caloriesBurnedDataPoint = newDataPoint(caloriesBurned, null, true, null);

    return Optional.of(caloriesBurnedDataPoint);

}

From source file:com.buffalokiwi.api.APIDate.java

/**
 * Create a new APIDate instance from ZonedDateTime 
 * @param zdt //w w  w. j  a  v a  2  s. com
 */
public APIDate(final ZonedDateTime zdt) {
    if (zdt == null)
        throw new IllegalArgumentException("zdt can't be null");

    date = zdt;

    this.offset = zdt.getOffset();
}

From source file:org.cgiar.ccafs.marlo.action.center.summaries.CapdevSummaryAction.java

@Override
public String execute() throws Exception {

    final ByteArrayOutputStream os = new ByteArrayOutputStream();

    final Resource reportResource = resourceManager
            .createDirectly(this.getClass().getResource("/pentaho/center/Capdev.prpt"), MasterReport.class);

    final MasterReport masterReport = (MasterReport) reportResource.getResource();
    String center = this.getCurrentCrp().getAcronym();

    // Get datetime
    ZonedDateTime timezone = ZonedDateTime.now();
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-d 'at' HH:mm ");
    String zone = timezone.getOffset() + "";
    if (zone.equals("Z")) {
        zone = "+0";
    }//from  w  ww.  j ava2 s  . co m
    String currentDate = timezone.format(format) + "(GMT" + zone + ")";

    // Set Main_Query
    CompoundDataFactory cdf = CompoundDataFactory.normalize(masterReport.getDataFactory());
    String masterQueryName = "main";
    TableDataFactory sdf = (TableDataFactory) cdf.getDataFactoryForQuery(masterQueryName);
    TypedTableModel model = this.getMasterTableModel(center, currentDate);
    sdf.addTable(masterQueryName, model);
    masterReport.setDataFactory(cdf);

    // Get details band
    final ItemBand masteritemBand = masterReport.getItemBand();
    // Create new empty subreport hash map
    final HashMap<String, Element> hm = new HashMap<String, Element>();
    // method to get all the subreports in the prpt and store in the HashMap
    this.getAllSubreports(hm, masteritemBand);
    // Uncomment to see which Subreports are detecting the method getAllSubreports

    // Subreport list of capdev
    this.fillSubreport((SubReport) hm.get("capdev_interventions"), "capdev_interventions");
    this.fillSubreport((SubReport) hm.get("capdev_participants"), "capdev_participants");
    this.fillSubreport((SubReport) hm.get("individual_types"), "individual_types");
    this.fillSubreport((SubReport) hm.get("groupTypes"), "groupTypes");
    this.fillSubreport((SubReport) hm.get("disciplines"), "disciplines");
    this.fillSubreport((SubReport) hm.get("target_groups"), "target_groups");
    this.fillSubreport((SubReport) hm.get("programs"), "programs");
    this.fillSubreport((SubReport) hm.get("crp"), "crp");
    this.fillSubreport((SubReport) hm.get("capdevSummary"), "capdevSummary");
    this.fillSubreport((SubReport) hm.get("citizenship"), "citizenship");
    this.fillSubreport((SubReport) hm.get("funding_type"), "funding_type");
    this.fillSubreport((SubReport) hm.get("institution"), "institution");
    this.fillSubreport((SubReport) hm.get("institutions_type"), "institutions_type");

    ExcelReportUtil.createXLSX(masterReport, os);
    bytesXLS = os.toByteArray();
    os.close();

    return SUCCESS;
}

From source file:org.cgiar.ccafs.marlo.action.summaries.ExpectedDeliverablesSummaryAction.java

private TypedTableModel getMasterTableModel() {
    // Initialization of Model
    TypedTableModel model = new TypedTableModel(
            new String[] { "center", "date", "year", "regionalAvalaible", "showDescription", "cycle" },
            new Class[] { String.class, String.class, String.class, Boolean.class, Boolean.class,
                    String.class });
    String center = this.getLoggedCrp().getAcronym();

    ZonedDateTime timezone = ZonedDateTime.now();
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-d 'at' HH:mm ");
    String zone = timezone.getOffset() + "";
    if (zone.equals("Z")) {
        zone = "+0";
    }/* w  w  w .  j a v a2 s  .  co  m*/
    String date = timezone.format(format) + "(GMT" + zone + ")";
    String year = this.getSelectedYear() + "";
    model.addRow(new Object[] { center, date, year, this.hasProgramnsRegions(),
            this.hasSpecificities(APConstants.CRP_REPORTS_DESCRIPTION), this.getSelectedCycle() });
    return model;
}

From source file:org.cgiar.ccafs.marlo.action.summaries.InstitutionsSummaryAction.java

private TypedTableModel getMasterTableModel() {
    // Initialization of Model
    TypedTableModel model = new TypedTableModel(new String[] { "date", "center", "showDescription" },
            new Class[] { String.class, String.class, Boolean.class });
    String center = this.getLoggedCrp().getAcronym();

    ZonedDateTime timezone = ZonedDateTime.now();
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-d 'at' HH:mm ");
    String zone = timezone.getOffset() + "";
    if (zone.equals("Z")) {
        zone = "+0";
    }//  w  w w.j a  v a  2 s. com
    String date = timezone.format(format) + "(GMT" + zone + ")";
    model.addRow(new Object[] { date, center, this.hasSpecificities(APConstants.CRP_REPORTS_DESCRIPTION) });
    return model;
}