Java Date Convert convertDoubleTimeToDate(Number d)

Here you can find the source of convertDoubleTimeToDate(Number d)

Description

convert Double Time To Date

License

Open Source License

Parameter

Parameter Description
d a value in [0..1[ representing a day

Return

the time value as date in the range from 00:00 - 23:59:59

Declaration

public static Date convertDoubleTimeToDate(Number d) 

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:/*from ww w.  j  av  a  2  s. c om*/
 *     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 d
     *          a value in [0..1[ representing a day
     * @return the time value as date in the range from 00:00 - 23:59:59
     * @see #convertDateToDoubleTime(Date) inverse function
     */
    public static Date convertDoubleTimeToDate(Number d) {
        if (d == null) {
            return null;
        }
        int m;
        if (d.doubleValue() < 0) {
            m = (int) (((long) (d.doubleValue() * DAY_MILLIS - 0.5)) % DAY_MILLIS);
        } else {
            m = (int) (((long) (d.doubleValue() * DAY_MILLIS + 0.5)) % DAY_MILLIS);
        }

        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.MILLISECOND, m % 1000);
        m = m / 1000;
        c.set(Calendar.SECOND, m % 60);
        m = m / 60;
        c.set(Calendar.MINUTE, m % 60);
        m = m / 60;
        c.set(Calendar.HOUR_OF_DAY, m % 24);
        if (m < 0) {
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
        return c.getTime();
    }
}

Related

  1. convertDateToServerTimeZoneDateInMilliSec(String timeZoneName, long milliSecond)
  2. convertDateToTimeStamp(Date date)
  3. convertDateToUTC(Date date)
  4. convertDateToWindowsTime(Date javaDate)
  5. convertDateValueToMillis(TimeZone tz, long dateValue)
  6. convertFromFileman(String date)
  7. convertInputDateToString(Date inputDate)
  8. convertInt2Date(int date)
  9. convertIntToDate(final int iDate)