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

This code example shows how to use the following methods:
 
    private Date now;
 
    public TimestampedTable () { 
        super(); 
        this.now = new Date();
    } 
 
    @Override 
    public Table startHeaders() { 
        inHeaders = true; 
        currentCells = new ArrayList<Cell>();
         
        final Map<String, String> epochAttr = Maps.newHashMap();
        epochAttr.put("desc", "seconds since 1970-01-01 00:00:00 when request executed");
         
        final Map<String, String> hmsAttr = Maps.newHashMap();
        hmsAttr.put("desc", "human time when request executed");
         
        currentCells.add(new Cell("epoch", epochAttr));
Full Snippet Info
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: getDate, getDay, getHours, getMinutes, getMonth
    	} 
    	 
        Date date = (Date) object;
        
        JSONObject json = new JSONObject();
        json.put("date", date.getDate());
        json.put("day", date.getDay());
        json.put("hours", date.getHours());
        json.put("minutes", date.getMinutes());
        json.put("month", date.getMonth());
        json.put("seconds", date.getSeconds());
        json.put("time", date.getTime());
        json.put("timezoneOffset", date.getTimezoneOffset());
        json.put("year", date.getYear());
 
        serializer.write(json);
    } 
} 
Full Snippet Info
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
 
public class BasicDBObjectTest extends TestCase {
 
    @Test 
    public void testGetDate() { 
        final Date date = new Date();
        BasicDBObject doc = new BasicDBObject( "foo" , date);
        assertTrue( doc.getDate( "foo" ).equals( date ) );
    } 
 
    @Test 
    public void testGetDateWithDefault() { 
        final Date date = new Date();
        BasicDBObject doc = new BasicDBObject( "foo" , date);
        assertTrue( doc.getDate( "foo", new Date() ).equals( date ) );
        assertTrue( doc.getDate( "bar", date ).equals( date ) );
    } 
 
    @Test 
    public void testGetObjectId() { 
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: getTime, setHours, setMinutes, setSeconds, setTime
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    } 
 
    private static long clearTime(Date date) {
        date.setTime(date.getTime() / 1000L * 1000);
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        return date.getTime();
    } 
 
    public static boolean isoStringHasTime(String iso8601String) {
        return iso8601String.length() > 10;
    } 
 
    public static long parseIso8601(String iso8601String) throws ParseException {
        if (iso8601String == null)
            return 0; 
        String formatString;
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: setDate, setHours, setMinutes, setMonth, setSeconds
    assertEquals(06, date.getMinutes());
    assertEquals(18, date.getSeconds());
  }   
   
  public void testParseDateToString() { 
    Date date = new Date();
    date.setYear(110);
    date.setMonth(04);
    date.setDate(20);
    date.setHours(10);
    date.setMinutes(25);
    date.setSeconds(30);
     
    String expectedDateString = "2010-05-20T10:25:30+03:00"; // +xx:xx depends on timezone
     
    String parsedDateString = Util.formatDateToString(date); 
     
    Log.d("UtilTest", "************");
    Log.d("UtilTest", "expectedDateString" + expectedDateString);
    Log.d("UtilTest", "parsedDateString" + parsedDateString);
    Log.d("UtilTest", "************");
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: equals, toString
       assertEquals(oid, oid2);
   } 
 
   @Test 
   public void testDate() { 
       Date d = new Date();
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
       format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
       String formattedDate = format.format(d);
 
       String serialized = JSON.serialize(d);
       assertEquals("{ \"$date\" : \"" + formattedDate + "\"}", serialized);
 
       Date d2 = (Date)JSON.parse(serialized);
       assertEquals(d.toString(), d2.toString());
       assertTrue(d.equals(d2));
   } 
 
    @Test 
    public void testJSONEncoding() throws ParseException { 
        String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' :  null, 'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: getTime, setHours, setMinutes, setSeconds, setTime
		} 
    } 
 
    private Date getToday(Calendar calendar) {
        Date today = calendar.getTime();
        today.setTime(today.getTime() / 1000L * 1000L);
        today.setHours(12);
        today.setMinutes(0);
        today.setSeconds(0);
        return today;
    } 
 
    public Date getCoercedDate(Date ifZero, Date ifNotZero) {
        return (calendarDate.getTime() == 0 ? ifZero : ifNotZero);
    } 
 
    public Date getCalendarDate() {
    	return calendarDate;
    } 
 
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
    Date reference;
 
    @Override 
    public void configure(InPast past) {
        try { 
            this.reference = past.value().equals("") ? new Date() : AlternativeDateFormat.getDefaultFormatter().parse(past.value());
        } catch (ParseException ex) {
            throw new UnexpectedException("Cannot parse date " + past.value(), ex);
        } 
        if (!past.value().equals("") && past.message().equals(mes)) {
            setMessage("validation.before"); 
        } else { 
            setMessage(past.message());
        } 
    } 
 
    public boolean isSatisfied(Object validatedObject, Object value, OValContext context, Validator validator) {
        requireMessageVariablesRecreation(); 
        if (value == null) {
            return true; 
Full Snippet Info
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
 
    @Override 
    public void close() { 
        if (this.valid.get()) {
            Date lastAccessedTime = this.entry.getLastAccessedTime();
            this.entry.setLastAccessedTime(new Date());
            if (lastAccessedTime != null) {
                this.mutator.mutate();
            } 
        } 
        if (this.group.isCloseable()) {
            this.group.close();
        } 
    } 
} 
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: getTime
        assertNotEquals( a , b );
    } 
     
    @Test 
    public void testDateCons(){ 
        java.util.Date d = new java.util.Date();
        ObjectId a = new ObjectId( d );
        assertEquals( d.getTime() / 1000 , a.getTime() / 1000 );
    } 
 
    @Test 
    public void testStringOnServer(){ 
        ObjectId oid = new ObjectId();
        DBObject res = getDatabase().command( new BasicDBObject( "driverOIDTest" , oid ) );
        assertEquals( oid.toString() , res.get( "str" ).toString() );
    } 
 
    void _testFlip( int x , int Y ){
        int y = ObjectId._flip( x );
        int z = ObjectId._flip( y );
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Java 8 Date Questions & Answers:

Other Java 8 Date Examples

266:			// Create a media file name
267:			String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
268:					Locale.getDefault()).format(new Date());
269:			File mediaFile;
270:			if (type == MEDIA_TYPE_IMAGE) {

Read More

373:			SimpleDateFormat dateFormat = new SimpleDateFormat(
374:					"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
375:			Date date = new Date();
376:			return dateFormat.format(date);
377:		}
-
373:			SimpleDateFormat dateFormat = new SimpleDateFormat(
374:					"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
375:			Date date = new Date();
376:			return dateFormat.format(date);
377:		}

Read More

See Code Examples for Java 8 Date Methods: