Java SQL Time Field getDay(long time)

Here you can find the source of getDay(long time)

Description

Get earliest time of a day (truncate)

License

Open Source License

Parameter

Parameter Description
time day and time

Return

day with 00:00

Declaration

static public Timestamp getDay(long time) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
 * Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
 * are Copyright (C) 1999-2005 Jorg Janke.
 * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/

import java.sql.Timestamp;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    /**//from w  w w.  j a v a  2s .c o  m
     *    Get earliest time of a day (truncate)
     *  @param time day and time
     *  @return day with 00:00
     */
    static public Timestamp getDay(long time) {
        if (time == 0)
            time = System.currentTimeMillis();
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTimeInMillis(time);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return new Timestamp(cal.getTimeInMillis());
    }

    /**
     *    Get earliest time of a day (truncate)
     *  @param dayTime day and time
     *  @return day with 00:00
     */
    static public Timestamp getDay(Timestamp dayTime) {
        if (dayTime == null)
            return getDay(System.currentTimeMillis());
        return getDay(dayTime.getTime());
    }

    /**
     *    Get earliest time of a day (truncate)
     *   @param day day 1..31
     *   @param month month 1..12
     *   @param year year (if two diguts: < 50 is 2000; > 50 is 1900)
     *   @return timestamp ** not too reliable
     */
    static public Timestamp getDay(int year, int month, int day) {
        if (year < 50)
            year += 2000;
        else if (year < 100)
            year += 1900;
        if (month < 1 || month > 12)
            throw new IllegalArgumentException("Invalid Month: " + month);
        if (day < 1 || day > 31)
            throw new IllegalArgumentException("Invalid Day: " + month);
        GregorianCalendar cal = new GregorianCalendar(year, month - 1, day);
        return new Timestamp(cal.getTimeInMillis());
    }
}

Related

  1. clearTime(final Calendar self)
  2. clearTimeCommon(final Calendar self)
  3. clearTimeFields(java.sql.Date date)
  4. clearTimeFields(java.util.Date date)
  5. getHoraFormatada(Time hora, String formato)
  6. getHour(String time)
  7. getHoursAndMinutes(String dateTime)
  8. getSecond(Time time)