Java Timestamp from timestampFromCalendar(Calendar calendar)

Here you can find the source of timestampFromCalendar(Calendar calendar)

Description

Answer a Timestamp from a Calendar.

License

Open Source License

Declaration

public static java.sql.Timestamp timestampFromCalendar(Calendar calendar) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.//from   ww w  . jav a  2s .  co m
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 *     dminsky - added countOccurrencesOf(Object, List) API
 *     08/23/2010-2.2 Michael O'Brien
 *        - 323043: application.xml module ordering may cause weaving not to occur causing an NPE.
 *                       warn if expected "_persistence_*_vh" method not found
 *                       instead of throwing NPE during deploy validation.
 ******************************************************************************/

import java.util.Calendar;

public class Main {
    /**
     * Answer a Timestamp from a Calendar.
     */
    public static java.sql.Timestamp timestampFromCalendar(Calendar calendar) {
        return timestampFromLong(calendar.getTimeInMillis());
    }

    /**
     * Answer a Time from a long
     *
     * @param longObject - milliseconds from the epoch (00:00:00 GMT
     * Jan 1, 1970).  Negative values represent dates prior to the epoch.
     */
    public static java.sql.Timestamp timestampFromLong(Long millis) {
        return timestampFromLong(millis.longValue());
    }

    /**
     * Answer a Time from a long
     *
     * @param longObject - milliseconds from the epoch (00:00:00 GMT
     * Jan 1, 1970).  Negative values represent dates prior to the epoch.
     */
    public static java.sql.Timestamp timestampFromLong(long millis) {
        java.sql.Timestamp timestamp = new java.sql.Timestamp(millis);

        // P2.0.1.3: Didn't account for negative millis < 1970
        // Must account for the jdk millis bug where it does not set the nanos.
        if ((millis % 1000) > 0) {
            timestamp.setNanos((int) (millis % 1000) * 1000000);
        } else if ((millis % 1000) < 0) {
            timestamp.setNanos((int) (1000000000 - (Math
                    .abs((millis % 1000) * 1000000))));
        }
        return timestamp;
    }
}

Related

  1. convertToTimestamp(String s)
  2. convertToTimeStamp(Timestamp timestamp, String format, TimeZone timeZone)
  3. convertToTimestampDate(Date date)
  4. convertToTimestampHMS(String dateData)
  5. getFirstTimeOfDay(Calendar calendar)
  6. timestampFromLong(long t)
  7. timestampFromYearMonthDateHourMinuteSecondNanos( int year, int month, int date, int hour, int minute, int second, int nanos)