Description
Convert the argument date string to a vanilla java.util.Date.
License
Open Source License
Parameter
Parameter | Description |
---|
date | the original date |
Exception
Parameter | Description |
---|
UnsupportedOperationException | an exception |
Return
a Date
Declaration
public static Date toDate(Object date)
Method Source Code
//package com.java2s;
/*//from w w w . j ava 2 s .c o m
************************************************************************
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONN?ES ASTRONOMIQUES **************
*
* (c) 2009. (c) 2009.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits r?serv?s
*
* NRC disclaims any warranties, Le CNRC d?nie toute garantie
* expressed, implied, or ?nonc?e, implicite ou l?gale,
* statutory, of any kind with de quelque nature que ce
* respect to the software, soit, concernant le logiciel,
* including without limitation y compris sans restriction
* any warranty of merchantability toute garantie de valeur
* or fitness for a particular marchande ou de pertinence
* purpose. NRC shall not be pour un usage particulier.
* liable in any event for any Le CNRC ne pourra en aucun cas
* damages, whether direct or ?tre tenu responsable de tout
* indirect, special or general, dommage, direct ou indirect,
* consequential or incidental, particulier ou g?n?ral,
* arising from the use of the accessoire ou fortuit, r?sultant
* software. Neither the name de l'utilisation du logiciel. Ni
* of the National Research le nom du Conseil National de
* Council of Canada nor the Recherches du Canada ni les noms
* names of its contributors may de ses participants ne peuvent
* be used to endorse or promote ?tre utilis?s pour approuver ou
* products derived from this promouvoir les produits d?riv?s
* software without specific prior de ce logiciel sans autorisation
* written permission. pr?alable et particuli?re
* par ?crit.
*
* This file is part of the Ce fichier fait partie du projet
* OpenCADC project. OpenCADC.
*
* OpenCADC is free software: OpenCADC est un logiciel libre ;
* you can redistribute it and/or vous pouvez le redistribuer ou le
* modify it under the terms of modifier suivant les termes de
* the GNU Affero General Public la ?GNU Affero General Public
* License as published by the License?? telle que publi?e
* Free Software Foundation, par la Free Software Foundation
* either version 3 of the : soit la version 3 de cette
* License, or (at your option) licence, soit (? votre gr?)
* any later version. toute version ult?rieure.
*
* OpenCADC is distributed in the OpenCADC est distribu?
* hope that it will be useful, dans l?espoir qu?il vous
* but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE
* without even the implied GARANTIE : sans m?me la garantie
* warranty of MERCHANTABILITY implicite de COMMERCIALISABILIT?
* or FITNESS FOR A PARTICULAR ni d?AD?QUATION ? UN OBJECTIF
* PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence
* General Public License for G?n?rale Publique GNU Affero
* more details. pour plus de d?tails.
*
* You should have received Vous devriez avoir re?u une
* a copy of the GNU Affero copie de la Licence G?n?rale
* General Public License along Publique GNU Affero avec
* with OpenCADC. If not, see OpenCADC ; si ce n?est
* <http://www.gnu.org/licenses/>. pas le cas, consultez :
* <http://www.gnu.org/licenses/>.
*
* $Revision: 4 $
*
************************************************************************
*/
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
/**
* Convert the argument date string to a vanilla java.util.Date. The input
* object can be a java.sql.Date (or subclass) or java.sql.Timestamp
* (or subclass).
*
* @param date the original date
* @return a Date
* @throws UnsupportedOperationException
*/
public static Date toDate(Object date) {
if (date == null)
return null;
if (date instanceof java.sql.Timestamp) {
java.sql.Timestamp ts = (java.sql.Timestamp) date;
long millis = ts.getTime();
// NOTE: On DB2 the millis is only to the second and all the fractional
// part is in the nanos
//int nanos = ts.getNanos();
//millis += (long) (nanos / 1000000);
return new Date(millis);
}
if (date instanceof java.sql.Date) {
java.sql.Date sd = (java.sql.Date) date;
return new Date(sd.getTime());
}
if (date instanceof Date) {
return (Date) date;
}
throw new UnsupportedOperationException(
"failed to convert " + date.getClass().getName() + " to java.util.Date");
}
/**
* Convert a Modified Julian Date to a date in the UTC timezone.
*
* @param mjd the MJD value
* @return a Date in the UTC timezone
*/
public static Date toDate(double mjd) {
int[] ymd = slaDjcl(mjd);
// fraction of a day
double frac = mjd - ((double) (long) mjd);
int hh = (int) (frac * 24);
// fraction of an hour
frac = frac * 24.0 - hh;
int mm = (int) (frac * 60);
// fraction of a minute
frac = frac * 60.0 - mm;
int ss = (int) (frac * 60);
// fraction of a second
frac = frac * 60.0 - ss;
int ms = (int) (frac * 1000);
//frac = frac*1000.0 - ms;
Calendar cal = Calendar.getInstance(UTC);
cal.set(Calendar.YEAR, ymd[0]);
cal.set(Calendar.MONTH, ymd[1] - 1); // Calendar is 0-based
cal.set(Calendar.DAY_OF_MONTH, ymd[2]);
cal.set(Calendar.HOUR_OF_DAY, hh);
cal.set(Calendar.MINUTE, mm);
cal.set(Calendar.SECOND, ss);
cal.set(Calendar.MILLISECOND, ms);
return cal.getTime();
}
/**
* Obtain the Date from the given DMF seconds.
*
* @param dmfSeconds The DMF Seconds value.
* @return Date object from the dmf Seconds.
*/
public static Date toDate(final long dmfSeconds) {
return new Date((dmfSeconds * 1000L) + getDMFEpoch().getTime());
}
private static int[] slaDjcl(double djm)
/*
** - - - - - - - -
** s l a D j c l
** - - - - - - - -
**
** Modified Julian Date to Gregorian year, month, day,
** and fraction of a day.
**
** Given:
** djm double Modified Julian Date (JD-2400000.5)
**
** Returned:
** *iy int year
** *im int month
** *id int day
** *fd double fraction of day
** *j int status:
** -1 = unacceptable date (before 4701BC March 1)
**
** The algorithm is derived from that of Hatcher 1984 (QJRAS 25, 53-55).
**
** Defined in slamac.h: dmod
**
** Last revision: 12 March 1998
**
** Copyright P.T.Wallace. All rights reserved.
*/
{
//System.out.println("[slaDjcl] " + djm);
//double f, d;
//double f;
long ld, jd, n4, nd10;
/* Check if date is acceptable */
if ((djm <= -2395520.0) || (djm >= 1e9))
//{
//*j = -1;
//return;
{
throw new IllegalArgumentException("MJD out of valid range");
//}
//else
//{
// *j = 0;
/* Separate day and fraction */
//f = dmod ( djm, 1.0 );
//if ( f < 0.0 ) f += 1.0;
//d = djm - f;
//d = dnint ( d );
}
ld = (long) djm;
/* Express day in Gregorian calendar */
//jd = (long) dnint ( d ) + 2400001;
jd = ld + 2400001L;
n4 = 4L * (jd + ((6L * ((4L * jd - 17918L) / 146097L)) / 4L + 1L) / 2L - 37L);
nd10 = 10L * (((n4 - 237L) % 1461L) / 4L) + 5L;
//*iy = (int) (n4/1461L-4712L);
//*im = (int) (((nd10/306L+2L)%12L)+1L);
//*id = (int) ((nd10%306L)/10L+1L);
//*fd = f;
int[] ret = new int[3];
ret[0] = (int) (n4 / 1461L - 4712L);
ret[1] = (int) (((nd10 / 306L + 2L) % 12L) + 1L);
ret[2] = (int) ((nd10 % 306L) / 10L + 1L);
//*j = 0;
return ret;
//}
}
/**
* Obtain the Date object representing the DMF Epoch of January 1st, 1980.
*
* @return Date object.
*/
public static Date getDMFEpoch() {
final Calendar cal = Calendar.getInstance(UTC);
cal.set(1980, Calendar.JANUARY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}
Related
- getDateTime(Object value)
- toDate(Calendar cal)
- toDate(int value)
- toDate(java.sql.Date sqlDate)
- toDate(java.util.Date date)
- toDate(Object o)
- toDate(Object obj)
- toDate(Object value)
- toDate(String dateString)