title: How to round of a millisecond in java?
body: <p><strong>Java Code</strong></p>
  
  <pre><code>public static long round(long millis , TimeUnit unit){
      Calendar calendar = Calendar.getInstance();
      switch(unit){
          case DAYS:
              calendar.set(Calendar.MILLISECOND, 0);
              calendar.set(Calendar.SECOND,0);
              calendar.set(Calendar.MINUTE, 0);
              calendar.set(Calendar.HOUR, 0);
              return calendar.getTimeInMillis();
          case HOURS:
              calendar.set(Calendar.MILLISECOND, 0);
              calendar.set(Calendar.SECOND, 0);
              calendar.set(Calendar.MINUTE, 0);
              return calendar.getTimeInMillis();
          case MINUTES:
              calendar.set(Calendar.MILLISECOND , 0);
              calendar.set(Calendar.SECOND, 0);
              return calendar.getTimeInMillis();
          case SECONDS:
              calendar.set(Calendar.MILLISECOND, 0);
              return calendar.getTimeInMillis();
          case MILLISECONDS:
          default:
              calendar.set(Calendar.MILLISECOND, 0);
              return  calendar.getTimeInMillis();
      }
  }
  </code></pre>
  
  <p>Currently , I am using this code to round of the milliseconds value. In this method redundancy of the code is the big problem.</p>
  
  <p>Is there any better solution or math equations for computing this?</p>