Java Date Convert convertDateToDoubleTime(Date time)

Here you can find the source of convertDateToDoubleTime(Date time)

Description

convert Date To Double Time

License

Open Source License

Parameter

Parameter Description
time a time (hh:mm:ss) in the interval 00:00:00 - 23:59:59

Return

the time value as a double in the range from [0..1[

Declaration

public static Double convertDateToDoubleTime(Date time) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*www .  j  a va 2  s  .  c  o m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static final long DAY_MILLIS = 24L * 3600L * 1000L;

    /**
     * @param time
     *          a time (hh:mm:ss) in the interval 00:00:00 - 23:59:59
     * @return the time value as a double in the range from [0..1[
     * @see #convertDoubleTimeToDate(Number) inverse function
     */
    public static Double convertDateToDoubleTime(Date time) {
        if (time == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        double t = ((c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE)) * 60 + c.get(Calendar.SECOND))
                * 1000 + c.get(Calendar.MILLISECOND);
        Double d = new Double(t / DAY_MILLIS);
        // range check;
        if (d.doubleValue() < 0) {
            d = new Double(0);
        }
        if (d.doubleValue() > 1) {
            d = new Double(1);
        }
        return d;
    }
}

Related

  1. convert(Date d)
  2. convertDate(Object obj)
  3. convertDateTime(String date, String time)
  4. convertDateTimeToSysTime(String sDateTime)
  5. ConvertDateToAge(String date)
  6. convertDateToInt(final Date date)
  7. convertDateToLong(int year, int month, int day, int hour, int minute, int second)
  8. convertDateToServerTimeZoneDateInMilliSec(String timeZoneName, long milliSecond)
  9. convertDateToTimeStamp(Date date)