Java 8 Instant - 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 Instant class.

This code example shows how to use the following methods:getEpochSecond, getNano
This code example shows how to use the following methods:getEpochSecond, getNano
    int nanoseconds = extractNanosecondDecimal(value, seconds);
    return Instant.ofEpochSecond(seconds, nanoseconds);
  } 
 
  public static BigDecimal toDecimal(Instant instant) {
    return new BigDecimal(toDecimal(instant.getEpochSecond(), instant.getNano()));
  } 
 
  public static String toDecimal(long seconds, int nanoseconds) {
    StringBuilder string = new StringBuilder(Integer.toString(nanoseconds));
    if (string.length() < 9)
      string.insert(0, ZEROES, 0, 9 - string.length());
    return seconds + "." + string;
  } 
 
  public static int extractNanosecondDecimal(BigDecimal value, long integer) {
    return value.subtract(new BigDecimal(integer)).multiply(ONE_BILLION).intValue();
  } 
} 
5
CodeRank
This code example shows how to use the following methods:isBefore
		// null values are valid 
		if ( value == null ) {
			return true; 
		} 
 
		return value.isBefore( Instant.now() );
	} 
} 
5
CodeRank
This code example shows how to use the following methods:isAfter
		// null values are valid 
		if ( value == null ) {
			return true; 
		} 
 
		return value.isAfter( Instant.now() );
	} 
} 
5
CodeRank
This code example shows how to use the following methods:getEpochSecond, getNano
This code example shows how to use the following methods:compareTo, isAfter, isBefore
 
        // Here is another way 
        System.out.format("[opt 2]Between %s and %s there are %d %s.%n", instant, instant1, ChronoUnit.MINUTES.between(instant, instant1), ChronoUnit.MINUTES);
 
        // Compare the two 
        System.out.format("instant1.compareTo(instant)=%d.%n", instant1.compareTo(instant));
 
        // We can check to see which Instant is before/after the other 
        System.out.format("instant1.isAfter(instant=%b, instant1.isBefore(instant)=%b.%n", instant1.isAfter(instant), instant1.isBefore(instant));
 
    } 
} 
4
CodeRank
This code example shows how to use the following methods:compareTo
 
    private static final long serialVersionUID = -1960271853562222300L;
 
    @Override 
    public int compare(Object o1, Object o2) { 
        return ((Instant) o1).compareTo((Instant) o2);
    } 
} 
4
CodeRank
This code example shows how to use the following methods:isBefore
        if ( this.timestamp == o.timestamp )
        { 
            return 0; 
        } 
 
        if ( this.timestamp.isBefore( o.timestamp ) )
        { 
            return 1; 
        } 
 
        return -1; 
    } 
 
    @Override 
    public boolean equals( final Object o ) 
    { 
        if ( this == o )
        { 
            return true; 
        } 
4
CodeRank
This code example shows how to use the following methods:toString
node.putNull( fieldName );
        } 
        else 
        { 
            node.put( fieldName, instant.toString() );
        } 
    } 
 
    public static Instant getInstantValue( String fieldName, JsonNode node )
    { 
        JsonNode subNode = node.get( fieldName );
        if ( subNode == null )
        { 
            throw new JsonParsingException( "Field [" + fieldName + "]  does not exist in: " + node.toString() );
        } 
        else if ( subNode.isNull() )
        { 
            return null; 
        } 
4
CodeRank
This code example shows how to use the following methods:toString
jgen.writeNumber( Instant.now().toEpochMilli() );
        } 
        else 
        { 
            jgen.writeString( value.toString() );
        } 
    } 
 
    @Override 
    public void serializeWithType( Instant value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer )
        throws IOException, JsonProcessingException
    { 
        typeSer.writeTypePrefixForScalar( value, jgen );
        serialize( value, jgen, provider );
        typeSer.writeTypeSuffixForScalar( value, jgen );
    } 
 
    @Override 
    public JsonNode getSchema( SerializerProvider provider, java.lang.reflect.Type typeHint )
4
CodeRank
See Code Examples for Java 8 Instant Methods: