Advertisement
marcelofoxes

get fixed clock at specified hour with java8 time api

Aug 7th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. import java.time.Clock;
  2. import java.time.Instant;
  3. import java.time.LocalDateTime;
  4. import java.time.ZoneId;
  5. import java.time.ZonedDateTime;
  6.  
  7. public class App {
  8.  
  9.     public static void main(String[] args) {
  10.     }
  11.  
  12.     private static Instant getTodayAtHour(int hour) {
  13.         LocalDateTime todayAtSpecifiedHour = LocalDateTime.now().withHour(hour).withMinute(0).withSecond(0).withNano(0);
  14.         ZonedDateTime dt = todayAtSpecifiedHour.atZone(ZoneId.systemDefault());
  15.         Instant instant = dt.toInstant();
  16.         return instant;
  17.     }
  18.  
  19.     private static Clock get15hsClock() {
  20.         Clock clock15hs = Clock.fixed(getTodayAtHour(15), ZoneId.systemDefault());
  21.         return clock15hs;
  22.     }
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement