Java Hour Format format(final Date date)

Here you can find the source of format(final Date date)

Description

format

License

Apache License

Declaration

private static String format(final Date date) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final long MILLIS_UPPER_BOUND = 1000;
    private static final long SECONDS_UPPER_BOUND = 60000;
    private static final long MINUTES_UPPER_BOUND = 3600000;
    private static final String MILLIS_FORMAT = "SSS";
    private static final String SECONDS_MILLIS_FORMAT = "ss:SSS";
    private static final String MINUTES_SECONDS_MILLIS_FORMAT = "mm:ss:SSS";
    private static final String HOURS_MINUTES_SECONDS_MILLIS_FORMAT = "hh:mm:ss:SSS";

    private static String format(final Date date) {
        final long duration = date.getTime();
        final SimpleDateFormat simpleDateFormat;
        if (duration < MILLIS_UPPER_BOUND) {
            simpleDateFormat = new SimpleDateFormat(MILLIS_FORMAT);
        } else if (duration < SECONDS_UPPER_BOUND) {
            simpleDateFormat = new SimpleDateFormat(SECONDS_MILLIS_FORMAT);
        } else if (duration < MINUTES_UPPER_BOUND) {
            simpleDateFormat = new SimpleDateFormat(MINUTES_SECONDS_MILLIS_FORMAT);
        } else {/*from w w  w . j a va  2 s  .  c o m*/
            simpleDateFormat = new SimpleDateFormat(HOURS_MINUTES_SECONDS_MILLIS_FORMAT);
        }
        return simpleDateFormat.format(date);
    }
}

Related

  1. format(Date dateTime)
  2. format(Date dt)
  3. format(Date self, String format, TimeZone tz)
  4. format(Date time, String fmt)
  5. format(double number, int decimalPlace)
  6. format(final Date date)
  7. format(final Date date)
  8. format(int level, String message, String source, boolean verbose)
  9. format(java.util.Date date, String format)