Here you can find the source of roundToHour(Timestamp arg)
Parameter | Description |
---|---|
arg | input Timestamp |
public static Timestamp roundToHour(Timestamp arg)
//package com.java2s; /*/*from ww w . j av a 2 s. c o m*/ * @(#)TimeUtils.java 0.51 1999/07/14 * * Copyright (c) 2004, Pat Farrell, All rights reserved. * based on work Copyright (c) 2000, OneBigCD, Inc. All rights reserved. * Copyright (C) 2011 Patrick Farrell All Rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * It is abstract because all functions are static */ import java.sql.Timestamp; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; public class Main { /** constant for UTC */ public static final TimeZone utcTZ = TimeZone.getTimeZone("UTC"); /** * rounds the input value to the nearest whole hour * @param arg input Timestamp * @return the input value to the nearest whole hour */ public static Timestamp roundToHour(Timestamp arg) { return new Timestamp(roundToSecs(arg, 60 * 60)); } /** * internal routine to convert timestamp to number of milliseconds rounded * @param arg incoming timestamp * @param seconds seconds to round to (1 gives seconds, 60 gives minues, etc.) * @return milliseconds rounded. */ private static long roundToSecs(Timestamp arg, int seconds) { long restored = 0; if (arg != null) { Timestamp odark = startOfDay(arg); long delta = arg.getTime() - odark.getTime(); long rounded = Math.round((delta) / (1000.0 * seconds)); restored = odark.getTime() + rounded * (1000 * seconds); } return restored; } /** * covert Date to start of day, 00:00:00 leaving date part * untouched * @param dd a date to use * @return same date, first minute of first second */ public static java.sql.Date startOfDay(java.sql.Date dd) { Calendar cal = GregorianCalendar.getInstance(utcTZ); if (dd != null) { cal.setTime(dd); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); } java.sql.Date rval = new java.sql.Date(cal.getTime().getTime()); return rval; } /** * covert Timestamp to start of day, 00:00:00 leaving date part * untouched * @param dd a date to use * @return same date, first minute of first second */ public static java.sql.Timestamp startOfDay(java.sql.Timestamp dd) { Calendar cal = GregorianCalendar.getInstance(utcTZ); if (dd != null) { cal.setTime(dd); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); } java.sql.Timestamp rval = new java.sql.Timestamp(cal.getTime().getTime()); return rval; } /** * covert calendar to start of day, 00:00:00 leaving date part * untouched * @param cal a calendar to use * @return same date, last minute of last second */ public static Calendar startOfDay(Calendar cal) { Calendar rval = (Calendar) cal.clone(); rval.set(Calendar.HOUR_OF_DAY, 0); rval.set(Calendar.MINUTE, 0); rval.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.getTimeInMillis(); return rval; } }