Java Code Examples for java.time.ZonedDateTime#toLocalDateTime()

The following examples show how to use java.time.ZonedDateTime#toLocalDateTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeUtil.java    From alpaca-java with MIT License 6 votes vote down vote up
/**
 * From date time string.
 *
 * @param dateTimeString the date time string
 *
 * @return the local date time using the system time zone
 */
public static LocalDateTime fromDateTimeString(String dateTimeString) {
    LocalDateTime ldt = LocalDateTime.parse(dateTimeString, inputDateTimeFormatter);

    ZonedDateTime ldtZoned;

    if (dateTimeString.endsWith("Z")) {
        ldtZoned = ldt.atZone(ZoneId.of("UTC"));
    } else {
        ldtZoned = ldt.atZone(ZoneId.of("America/New_York"));
    }

    ZonedDateTime localTimeZoned = ldtZoned.withZoneSameInstant(ZoneId.systemDefault());

    return localTimeZoned.toLocalDateTime();
}
 
Example 2
Source File: Main.java    From Java-11-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    var dateTime = ZonedDateTime.now();
    System.out.println("Date Time using now(): " + dateTime);

    var indianTz = ZoneId.of("Asia/Kolkata");
    var istDateTime = ZonedDateTime.now(indianTz);
    System.out.println("Date Time using ZoneId: " + istDateTime);

    var indianTzOffset = ZoneOffset.ofHoursMinutes(5, 30);
    istDateTime = ZonedDateTime.now(indianTzOffset);
    System.out.println("Date Time using ZoneOffset: " + istDateTime);
    
    ZonedDateTime dateTimeOf = ZonedDateTime.of(2018, 4, 22, 14, 30, 11, 33, indianTz);
    System.out.println("Date Time using of(): " + dateTimeOf);

    var localDateTime = dateTimeOf.toLocalDateTime();
    System.out.println("LocalDateTime from ZonedDateTime: " + localDateTime);
    
}
 
Example 3
Source File: SQL.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
/**
 * Binds arguments to prepared statement
 * @param stmt  the prepared statement reference
 * @return      the same as arg
 * @throws SQLException if binding fails
 */
private PreparedStatement bindArgs(PreparedStatement stmt) throws SQLException {
    for (int i=0; i<args.length; ++i) {
        final Object value = args[i];
        if      (value instanceof Boolean)          stmt.setBoolean(i+1, (Boolean)value);
        else if (value instanceof Short)            stmt.setShort(i+1, (Short)value);
        else if (value instanceof Integer)          stmt.setInt(i+1, (Integer)value);
        else if (value instanceof Float)            stmt.setFloat(i+1, (Float)value);
        else if (value instanceof Long)             stmt.setLong(i+1, (Long)value);
        else if (value instanceof Double)           stmt.setDouble(i+1, (Double)value);
        else if (value instanceof String)           stmt.setString(i+1, (String)value);
        else if (value instanceof java.sql.Date)    stmt.setDate(i+1, (java.sql.Date)value);
        else if (value instanceof Timestamp)        stmt.setTimestamp(i+1, (Timestamp)value);
        else if (value instanceof LocalDate)        stmt.setDate(i + 1, java.sql.Date.valueOf((LocalDate)value));
        else if (value instanceof LocalTime)        stmt.setTime(i+1, Time.valueOf((LocalTime)value));
        else if (value instanceof LocalDateTime)    stmt.setTimestamp(i+1, Timestamp.valueOf((LocalDateTime)value));
        else if (value instanceof ZonedDateTime) {
            final ZonedDateTime zonedDateTime = (ZonedDateTime)value;
            final LocalDateTime dateTime = zonedDateTime.toLocalDateTime();
            stmt.setTimestamp(i+1, Timestamp.valueOf(dateTime));
        } else {
            throw new RuntimeException("Unsupported argument, cannot be bound to SQL statement: " + value);
        }
    }
    return stmt;
}
 
Example 4
Source File: ZonedDateTimeHandle.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public void wrap(final ZonedDateTime zonedDateTime) {
    this.dateTime = zonedDateTime.toLocalDateTime();
    this.offset = zonedDateTime.getOffset();
    if (zonedDateTime.getZone() != null) {
        this.zoneId = zonedDateTime.getZone().getId();
    }
}
 
Example 5
Source File: DateTimeParserContext.java    From jphp with Apache License 2.0 5 votes vote down vote up
DateTimeParserContext(List<DateTimeTokenizer.Token> tokens, Cursor cursor, DateTimeTokenizer tokenizer,
                      ZonedDateTime dateTime) {
    this.tokens = tokens;
    this.cursor = cursor;
    this.tokenizer = tokenizer;
    this.modified = new HashSet<>();
    this.dateTime = dateTime.toLocalDateTime();
    this.zoneId = dateTime.getZone();
}
 
Example 6
Source File: DateTimeConverters.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public LocalDateTime convert(ZonedDateTime source) {
	return source.toLocalDateTime();
}
 
Example 7
Source File: CommitFactory.java    From javers with Apache License 2.0 4 votes vote down vote up
private CommitMetadata newCommitMetadata(String author, Map<String, String> properties){
    ZonedDateTime now = dateProvider.now();
    return new CommitMetadata(author, properties,
            now.toLocalDateTime(), now.toInstant(),
            commitIdFactory.nextId());
}
 
Example 8
Source File: PersistentZonedDateTimeAsStringAndStringZone.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Override
protected Object[] toConvertedColumns(ZonedDateTime value) {

    return new Object[] { value.toLocalDateTime(), value.getZone() };
}
 
Example 9
Source File: PDTFactory.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nullable
public static LocalDateTime createLocalDateTime (@Nullable final ZonedDateTime aDT)
{
  return aDT == null ? null : aDT.toLocalDateTime ();
}
 
Example 10
Source File: LocalDateTimeTest.java    From jackcess with Apache License 2.0 4 votes vote down vote up
public void testWriteAndReadTemporals() throws Exception {
  ZoneId zoneId = ZoneId.of("America/New_York");
  for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
    Database db = createMem(fileFormat);
    db.setZoneId(zoneId);
    db.setDateTimeType(DateTimeType.LOCAL_DATE_TIME);

    Table table = new TableBuilder("test")
      .addColumn(new ColumnBuilder("name", DataType.TEXT))
      .addColumn(new ColumnBuilder("date", DataType.SHORT_DATE_TIME))
      .toTable(db);

    // since jackcess does not really store millis, shave them off before
    // storing the current date/time
    long curTimeNoMillis = (System.currentTimeMillis() / 1000L);
    curTimeNoMillis *= 1000L;

    DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    List<Date> tmpDates =
      new ArrayList<Date>(
          Arrays.asList(
              df.parse("19801231 00:00:00"),
              df.parse("19930513 14:43:27"),
              df.parse("20210102 02:37:00"),
              new Date(curTimeNoMillis)));

    List<Object> objs = new ArrayList<Object>();
    List<LocalDateTime> expected = new ArrayList<LocalDateTime>();
    for(Date d : tmpDates) {
      Instant inst = Instant.ofEpochMilli(d.getTime());
      objs.add(inst);
      ZonedDateTime zdt = inst.atZone(zoneId);
      objs.add(zdt);
      LocalDateTime ldt = zdt.toLocalDateTime();
      objs.add(ldt);

      for(int i = 0; i < 3; ++i) {
        expected.add(ldt);
      }
    }

    ((DatabaseImpl)db).getPageChannel().startWrite();
    try {
      for(Object o : objs) {
        table.addRow("row " + o, o);
      }
    } finally {
      ((DatabaseImpl)db).getPageChannel().finishWrite();
    }

    List<LocalDateTime> foundDates = new ArrayList<LocalDateTime>();
    for(Row row : table) {
      foundDates.add(row.getLocalDateTime("date"));
    }

    assertEquals(expected, foundDates);

    db.close();
  }
}
 
Example 11
Source File: DateTimeConverters.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public LocalDateTime convert(ZonedDateTime source) {
	return source.toLocalDateTime();
}
 
Example 12
Source File: ToolDatePlus.java    From protools with Apache License 2.0 4 votes vote down vote up
public static LocalDateTime date2LocalDateTime(final Date date) {
    Instant instant = date.toInstant();
    ZonedDateTime zdt = instant.atZone(DEFAULT_ZONE_ID);
    return zdt.toLocalDateTime();
}
 
Example 13
Source File: DateTimeConverters.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public LocalDateTime convert(ZonedDateTime source) {
	return source.toLocalDateTime();
}
 
Example 14
Source File: DateTimeConverters.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public LocalDateTime convert(ZonedDateTime source) {
	return source.toLocalDateTime();
}
 
Example 15
Source File: ZonedDateTimeHandle.java    From alibaba-rsocket-broker with Apache License 2.0 4 votes vote down vote up
public ZonedDateTimeHandle(ZonedDateTime o) {
    this.dateTime = o.toLocalDateTime();
    this.offset = o.getOffset();
    this.zoneId = o.getZone().getId();
}
 
Example 16
Source File: PDTWebDateHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Parses a Date out of a String with a date in W3C date-time format or in a
 * RFC822 format.
 *
 * @param sDate
 *        string to parse for a date.
 * @return the Date represented by the given W3C date-time string. It returns
 *         <b>null</b> if it was not possible to parse the given string into a
 *         Date.
 */
@Nullable
public static LocalDateTime getLocalDateTimeFromW3COrRFC822 (@Nullable final String sDate)
{
  final ZonedDateTime aDateTime = getDateTimeFromW3COrRFC822 (sDate);
  return aDateTime == null ? null : aDateTime.toLocalDateTime ();
}
 
Example 17
Source File: DateTimeUtility.java    From sailfish-core with Apache License 2.0 2 votes vote down vote up
/**
 * Merge a LocalTime and a LocalDate to a LocalDateTime in UTC time zone
 *
 * @param millisecond
 * @param nanosecond
 * @return LocalDateTime (UTC)
 */
public static LocalDateTime toLocalDateTime(LocalDate date, LocalTime time) {
    ZonedDateTime zonedDateTime = toZonedDateTime(date, time);
    return zonedDateTime.toLocalDateTime();
}
 
Example 18
Source File: DateTimeUtility.java    From sailfish-core with Apache License 2.0 2 votes vote down vote up
/**
 * Convert TemporalAccessor to LocalDateTime
 * 
 * @param temporal
 * @return LocalDateTime (UTC)
 */
public static LocalDateTime toLocalDateTime(TemporalAccessor temporal) {
    ZonedDateTime zonedDateTime = toZonedDateTime(temporal);
    return zonedDateTime.toLocalDateTime();
}
 
Example 19
Source File: DateTimeExpression.java    From jstarcraft-core with Apache License 2.0 2 votes vote down vote up
/**
 * 根据指定日期时间获取下一次日期时间
 * 
 * @param dateTime
 * @return
 */
public LocalDateTime getNextDateTime(LocalDateTime dateTime) {
    ZonedDateTime instant = getNextDateTime(ZonedDateTime.of(dateTime, ZoneOffset.UTC));
    return instant == null ? null : instant.toLocalDateTime();
}
 
Example 20
Source File: DateTimeExpression.java    From jstarcraft-core with Apache License 2.0 2 votes vote down vote up
/**
 * 根据指定日期时间获取上一次日期时间
 * 
 * @param dateTime
 * @return
 */
public LocalDateTime getPreviousDateTime(LocalDateTime dateTime) {
    ZonedDateTime instant = getPreviousDateTime(ZonedDateTime.of(dateTime, ZoneOffset.UTC));
    return instant == null ? null : instant.toLocalDateTime();
}