Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#TimeUnit' - 4 code snippet(s) found

 Sample 1. Convert Time in milliseconds to Days

TimeUnit.DAYS.convert(timeinMs, TimeUnit.MILLISECONDS)

   Like      Feedback     convert time in ms to days  TimeUnit  java.util.concurrent.TimeUnit


 Sample 2. Time Taken to call a method or service

Date date1 = new Date();

// Call Service or Method

Date date2 = new Date();
System.out.println(TimeUnit.SECONDS.convert(date2.getTime()-date1.getTime(), TimeUnit.MILLISECONDS));// Time Taken in Seconds
System.out.println(TimeUnit.MINUTES.convert(date2.getTime()-date1.getTime(), TimeUnit.MILLISECONDS));// Time Taken in Minutes
System.out.println(TimeUnit.HOURS.convert(date2.getTime()-date1.getTime(), TimeUnit.MILLISECONDS));// Time Taken in Hours

   Like      Feedback     Difference between 2 dates  Time to call a method  TimeUnit  Date.java.util.Date


 Sample 3. Clear Map entries after expiration time using Apache commons PassiveExpiringMap

PassiveExpiringMap<String,String> cache = new PassiveExpiringMap<String,String>(1,TimeUnit.SECONDS); // Expiration time of 1 sec
cache.put("Key1", "Value1");
System.out.println(cache.containsKey("Key1")); // prints true
Thread.sleep(1000);
System.out.println(cache.containsKey("Key1")); // prints false

   Like      Feedback     Clear Map entries after expiration  PassiveExpiringMap  map  apache commons  TimeUnit.SECONDS  Thread.sleep


 Sample 4. Code Sample / Example / Snippet of org.apache.calcite.avatica.util.TimeUnitRange

  private void checkTimestampString(String s, long d) {

assertThat(unixTimestampToString(d), equalTo(s));

assertThat(timestampStringToUnixDate(s), equalTo(d));

}



@Test public void testIntervalYearMonthToString() {

TimeUnitRange range = TimeUnitRange.YEAR_TO_MONTH;

assertEquals("+0-00", intervalYearMonthToString(0, range));

assertEquals("+1-00", intervalYearMonthToString(12, range));

assertEquals("+1-01", intervalYearMonthToString(13, range));

assertEquals("-1-01", intervalYearMonthToString(-13, range));

}


   Like      Feedback      org.apache.calcite.avatica.util.TimeUnitRange



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner