Example usage for java.time Duration minusHours

List of usage examples for java.time Duration minusHours

Introduction

In this page you can find the example usage for java.time Duration minusHours.

Prototype

public Duration minusHours(long hoursToSubtract) 

Source Link

Document

Returns a copy of this duration with the specified duration in hours subtracted.

Usage

From source file:org.ng200.openolympus.controller.api.ContestTimerController.java

@RequestMapping("/api/contestTimeRemaining/{contest}")
public @ResponseBody Map<String, String> getTimeRemaining(@PathVariable("contest") final Contest contest,
        final Principal principal) {
    Assertions.resourceExists(contest);//from  w  ww. ja v  a  2s . c o  m

    Instant end = contest.getStartTime().toInstant();
    final boolean isInProcess = contest.getStartTime().before(java.util.Date.from(Instant.now()));
    if (isInProcess) {
        if (this.contestSecurityService.isSuperuser(principal)) {
            end = this.contestService.getContestEndIncludingAllTimeExtensions(contest);
        } else if (principal != null) {
            final User user = this.userRepository.findByUsername(principal.getName());
            if (user != null) {
                end = end.plusMillis(contest.getDuration())
                        .plusMillis(this.contestService.getTimeExtensions(contest, user));
            }
        }
    }
    if (principal != null) {
        this.contestService.getTimeExtensions(contest, this.userRepository.findByUsername(principal.getName()));
    }
    final Instant cur = Instant.now();
    if (end.isBefore(cur)) {
        return new HashMap<String, String>() {
            /**
             *
             */
            private static final long serialVersionUID = 5419629757758058933L;

            {
                this.put("timer", "00:00:00");
                this.put("status", "ended");
            }
        };
    }
    final Duration duration = Duration.between(cur, end);
    final DecimalFormat format = new DecimalFormat("00");
    final long hours = duration.toHours();
    final long minutes = duration.minusHours(hours).toMinutes();
    final long seconds = duration.minusHours(hours).minusMinutes(minutes).getSeconds();
    return new HashMap<String, String>() {
        /**
         *
         */
        private static final long serialVersionUID = -4698243010184691932L;

        {
            this.put("timer",
                    format.format(hours) + ":" + format.format(minutes) + ":" + format.format(seconds));

            this.put("status", isInProcess ? "inProgress" : "notStartedYet");
        }
    };
}