Example usage for java.time LocalDateTime toInstant

List of usage examples for java.time LocalDateTime toInstant

Introduction

In this page you can find the example usage for java.time LocalDateTime toInstant.

Prototype

default Instant toInstant(ZoneOffset offset) 

Source Link

Document

Converts this date-time to an Instant .

Usage

From source file:Main.java

public static Timestamp timeZoneAdjustedDate(LocalDateTime due) {
    return Timestamp.from(due.toInstant(ZoneOffset.ofHours(0)));
}

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 .j av  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:net.consulion.jeotag.PhotoLoader.java

private static Instant getTimeTaken(final TiffImageMetadata exif, final File file) {
    Instant instant = null;//from w ww  . j a  v  a 2  s  . c  o  m
    if (exif != null) {
        try {
            final String[] dateTimeOriginal = exif.getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
            if (dateTimeOriginal.length == 1
                    && dateTimeOriginal[0].matches("\\d{4}\\:\\d{2}:\\d{2}\\s\\d{2}:\\d{2}:\\d{2}")) {
                final String[] split = dateTimeOriginal[0].split("\\s");
                final String dateString = split[0];
                final String timeString = split[1];
                final String[] dateSplit = dateString.split("\\:");
                final int year = Integer.parseInt(dateSplit[0]);
                final int month = Integer.parseInt(dateSplit[1]);
                final int day = Integer.parseInt(dateSplit[2]);
                final String[] timeSplit = timeString.split("\\:");
                final int hour = Integer.parseInt(timeSplit[0]);
                final int minute = Integer.parseInt(timeSplit[1]);
                final int second = Integer.parseInt(timeSplit[2]);
                final LocalDateTime ldt = LocalDateTime.of(year, month, day, hour, minute, second);
                instant = ldt.toInstant(ZoneOffset.ofHoursMinutes(0, 0));
            } else {
                log(Level.WARNING, String.format("unknown date format in file %s", file.getPath()));
            }
        } catch (ImageReadException ex) {
            Logger.getLogger(PhotoLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return instant;
}

From source file:edu.usu.sdl.openstorefront.common.util.TimeUtil.java

/**
 * Get the end of the day passed in/*from w  w w  .j ava 2s  .  c om*/
 *
 * @param date
 * @return end of the day or null if date was null
 */
public static Date endOfDay(Date date) {
    if (date != null) {
        Instant instant = Instant.ofEpochMilli(date.getTime());
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        localDateTime = localDateTime.withHour(23).withMinute(59).withSecond(59)
                .with(ChronoField.MILLI_OF_SECOND, 999);
        return new Date(localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli());
    }
    return date;

}

From source file:com.thinkbiganalytics.nifi.v2.ingest.GetTableData.java

public static Date toDate(LocalDateTime dateTime) {
    return dateTime == null ? new Date(0L) : Date.from(dateTime.toInstant(ZoneOffset.UTC));
}

From source file:com.tascape.reactor.report.MySqlBaseBean.java

public static long getMillis(String time) {
    if (time == null || time.trim().isEmpty()) {
        return System.currentTimeMillis();
    } else {//from   w  ww . j  a v a  2 s  . c om
        LocalDateTime ldt = LocalDateTime.parse(time, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        LOG.trace("ldt {}", ldt);
        ZoneId zone = ZoneId.of("America/Los_Angeles");
        ldt.atZone(zone);
        LOG.trace("ldt {}", ldt);
        return ldt.toInstant(ZoneOffset.ofHours(-8)).toEpochMilli();
    }
}

From source file:net.bis5.slack.command.gcal.SlashCommandApi.java

private Date toDate(LocalDateTime dateTime) {
    return Date.from(dateTime.toInstant(ZoneOffset.ofHours(+9)));
}

From source file:com.algodefu.yeti.web.rest.LocalDateTimeSerializer.java

@Override
public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    //        jgen.writeString(value.format(dtf));
    jgen.writeNumber(value.toInstant(ZoneOffset.UTC).toEpochMilli());
}

From source file:de.ks.flatadocdb.defaults.ReflectionLuceneDocumentExtractor.java

private DocField createLocalDateTimeDocField(Field f, MethodHandle getter) {
    return new DocField(f, getter, (id, value) -> {
        LocalDateTime localDateTime = (LocalDateTime) value;
        long utcTime = localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
        return new LongField(id, utcTime, org.apache.lucene.document.Field.Store.YES);
    });//from  ww  w .j  a v a2  s.c om
}

From source file:com.streamsets.pipeline.stage.processor.parser.TestDataParserProcessor.java

@Test
public void testSyslogParsing() throws Exception {
    int priority = 17;
    int facility = priority / 8;
    int severity = priority % 8;
    LocalDateTime date = LocalDateTime.now().withNano(0);
    String host = "1.2.3.4";
    String rest = "Nothing interesting happened.";

    String inputFieldPath = "input";
    String outputFieldPath = "/output";

    String syslogMsg = String.format("<%d>%s %s %s", priority,
            DateTimeFormatter.ofPattern("MMM dd HH:mm:ss").format(date), host, rest);

    DataParserConfig configs = new DataParserConfig();
    configs.dataFormat = DataFormat.SYSLOG;
    final DataParserFormatConfig dataParserFormatConfig = new DataParserFormatConfig();
    configs.dataFormatConfig = dataParserFormatConfig;
    configs.fieldPathToParse = "/" + inputFieldPath;
    configs.parsedFieldPath = outputFieldPath;

    DataParserProcessor processor = new DataParserProcessor(configs);

    final String outputLane = "out";

    ProcessorRunner runner = new ProcessorRunner.Builder(DataParserDProcessor.class, processor)
            .addOutputLane(outputLane).setOnRecordError(OnRecordError.TO_ERROR).build();
    Map<String, Field> map = new HashMap<>();
    map.put(inputFieldPath, Field.create(syslogMsg));
    Record record = RecordCreator.create();
    record.set(Field.create(map));
    List<Record> input = new ArrayList<>();
    input.add(record);/*from  w  ww.  j  a  v  a 2 s. c  o  m*/
    try {
        runner.runInit();
        StageRunner.Output output = runner.runProcess(input);
        assertTrue(output.getRecords().containsKey(outputLane));
        final List<Record> records = output.getRecords().get(outputLane);
        assertEquals(1, records.size());
        assertTrue(records.get(0).has(outputFieldPath));
        assertEquals(Field.Type.MAP, records.get(0).get(outputFieldPath).getType());
        Map<String, Field> syslogFields = records.get(0).get(outputFieldPath).getValueAsMap();
        assertThat(syslogFields, hasKey(SyslogMessage.FIELD_SYSLOG_PRIORITY));
        assertThat(syslogFields.get(SyslogMessage.FIELD_SYSLOG_PRIORITY), fieldWithValue(priority));
        assertThat(syslogFields.get(SyslogMessage.FIELD_SYSLOG_FACILITY), fieldWithValue(facility));
        assertThat(syslogFields.get(SyslogMessage.FIELD_SYSLOG_SEVERITY), fieldWithValue(severity));
        assertThat(syslogFields.get(SyslogMessage.FIELD_HOST), fieldWithValue(host));
        assertThat(syslogFields.get(SyslogMessage.FIELD_REMAINING), fieldWithValue(rest));
        assertThat(syslogFields.get(SyslogMessage.FIELD_RAW), fieldWithValue(syslogMsg));
        assertThat(syslogFields.get(SyslogMessage.FIELD_TIMESTAMP),
                fieldWithValue(date.toInstant(ZoneOffset.UTC).toEpochMilli()));

    } finally {
        runner.runDestroy();
    }
}