Java 8 LocalDateTime - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 LocalDateTime class.

This code example shows how to use the following methods:format, get, getDayOfMonth, getHour, getMinute
    public void serialize(LocalDateTime value, JsonGenerator g, SerializerProvider provider)
        throws IOException
    { 
        if (useTimestamp(provider)) {
            g.writeStartArray();
            g.writeNumber(value.getYear());
            g.writeNumber(value.getMonthValue());
            g.writeNumber(value.getDayOfMonth());
            g.writeNumber(value.getHour());
            g.writeNumber(value.getMinute());
            if (value.getSecond() > 0 || value.getNano() > 0) {
                g.writeNumber(value.getSecond());
                if(value.getNano() > 0) {
                    if (provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS))
                        g.writeNumber(value.getNano());
                    else 
                        g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND));
                } 
            } 
            g.writeEndArray();

6
CodeRank
This code example shows how to use the following methods:toLocalDate
    private List<Logs> dailyLog(String channelName, LocalDateTime date, Boolean logged) {
        List<Logs> list = null;
        if (logged) {
            LocalDate start = date == null
                              ? LocalDate.now()
                              : date.toLocalDate();
            LocalDate tomorrow = start.plusDays(1);
            LogsCriteria criteria = new LogsCriteria(ds);
            criteria.channel(channelName);
            LocalDateTime nextMidnight = tomorrow.atStartOfDay();
            LocalDateTime lastMidnight = start.atStartOfDay();
            criteria.and(
                            criteria.updated().lessThanOrEq(nextMidnight),
                            criteria.updated().greaterThanOrEq(lastMidnight)
                        ); 
            list = criteria.query().asList();
        } 
        return list;
    } 
 
4
CodeRank
This code example shows how to use the following methods:toInstant
  @Override 
  public Object encode(final Object value, final MappedField optionalExtraInfo) {
    if (value == null) { 
      return null; 
    } 
    return new Date(((LocalDateTime) value).toInstant(ZoneOffset.UTC).toEpochMilli());
  } 
} 
4
CodeRank
This code example shows how to use the following methods:toLocalDate
This code example shows how to use the following methods:toInstant
        { 
            return ( (LocalTime) value ).atDate( LocalDate.now() ).toInstant( ZoneOffset.UTC );
        } 
        if ( value instanceof LocalDateTime )
        { 
            return ( (LocalDateTime) value ).toInstant( ZoneOffset.UTC );
        } 
        if ( value instanceof Instant )
        { 
            return ( (Instant) value );
        } 
        else if ( value instanceof String )
        { 
            return Instant.parse( (String) value );
        } 
        else 
        { 
            return null; 
        } 
    } 
3
CodeRank
This code example shows how to use the following methods:format
      list.add(format("%-12s: %s", label, value));
    } 
  } 
 
  private String toString(LocalDateTime date) {
    return date != null ? date.format(DateTimeFormatter.ofPattern(NSERV_DATE_FORMAT)) : null;
  } 
 
  public void setLastAddress(final String lastAddress) {
    this.lastAddress = lastAddress;
  } 
} 
4
CodeRank
This code example shows how to use the following methods:getDayOfMonth, getMonth
This code example shows how to use the following methods:toInstant
        { 
            return ( (LocalTime) value ).atDate( LocalDate.now() ).toInstant( ZoneOffset.UTC );
        } 
        if ( value instanceof LocalDateTime )
        { 
            return ( (LocalDateTime) value ).toInstant( ZoneOffset.UTC );
        } 
        if ( value instanceof Instant )
        { 
            return ( (Instant) value );
        } 
        else if ( value instanceof String )
        { 
            return Instant.parse( (String) value );
        } 
        else 
        { 
            return null; 
        } 
    } 
4
CodeRank
This code example shows how to use the following methods:toInstant
    @Test
    public void convert_from_date_to_instant() 
    { 
        LocalDate date = LocalDate.of( 2014, 12, 31 );
        Property property = Property.newInstant( "myInstant", date );
        Instant expected = date.atStartOfDay().toInstant( ZoneOffset.UTC );
        Instant actual = property.getInstant();
        assertEquals( expected, actual );
    } 
 
} 
 
4
CodeRank
This code example shows how to use the following methods:atZone
    public Timestamp toNonNullValue(LocalTime value) {
 
    	ZoneOffset currentDatabaseZone = getDatabaseZone() == null ? getDefault() : getDatabaseZone();        
    	 
    	LocalDateTime ldt = value.atDate(LocalDate.of(1970, 1, 1));
    	ZonedDateTime zdt = ldt.atZone(currentDatabaseZone);
        Instant ins = zdt.toInstant();
 
    	ZoneOffset off = getDefault();
        int adjustment = TimeZone.getDefault().getOffset(ins.toEpochMilli()) - (off.getRules().getOffset(LocalDateTime.now()).getTotalSeconds() * MILLIS_IN_SECOND);
         
        final Timestamp timestamp = new Timestamp(ins.toEpochMilli() - adjustment);
        timestamp.setNanos(value.getNano());
        return timestamp;
    } 
 
    private static ZoneOffset getDefault() {
 
    	ZoneOffset zone = null;
        try { 
4
CodeRank
See Code Examples for Java 8 LocalDateTime Methods: