com.hfg.datetime.DateUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.datetime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import com.hfg.units.TimeUnit;
//------------------------------------------------------------------------------
/**
Date-related utility functions.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class DateUtil
{
/**
ISO 8601 datetime format. Ex: 2001-07-04T12:08:56.235-07:00
See http://www.w3.org/TR/NOTE-datetime.
Not thread-safe on its own.
*/
// Shim for Java 6. Java 7 adds the 'X' formatting char in place of 'Z' for timezone offsets containing a colon.
public static final SimpleDateFormat W3CDTF_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") {
public StringBuffer format(Date date, StringBuffer toAppendTo, java.text.FieldPosition pos) {
String fix = super.format(date, toAppendTo, pos).toString();
fix = fix.substring(0, fix.length()-2) + ":" + fix.substring(fix.length()-2, fix.length());
return new StringBuffer().append(fix);
};
};
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public static synchronized Date threadsafeParse(String inDateString, SimpleDateFormat inFormat)
throws ParseException
{
return inFormat.parse(inDateString);
}
//--------------------------------------------------------------------------
public static String generateElapsedTimeString(long inStartTime)
{
return generateElapsedTimeString(inStartTime, System.currentTimeMillis());
}
//--------------------------------------------------------------------------
public static int getCurrentYear()
{
return new GregorianCalendar().get(Calendar.YEAR);
}
//--------------------------------------------------------------------------
public static Date roundToNextMinute(Date inStartTime)
{
Calendar calendar = new GregorianCalendar();
calendar.setTime(inStartTime);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.MINUTE, 1);
return calendar.getTime();
}
//--------------------------------------------------------------------------
public static Date roundToNextHour(Date inStartTime)
{
Calendar calendar = new GregorianCalendar();
calendar.setTime(inStartTime);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.add(Calendar.HOUR_OF_DAY, 1);
return calendar.getTime();
}
//--------------------------------------------------------------------------
public static String generateElapsedTimeString(long inStartTime, long inEndTime)
{
long elapsedMillis = inEndTime - inStartTime;
long elapsedSec = 0;
long elapsedMin = 0;
long elapsedHour = 0;
String output;
if (elapsedMillis >= TimeUnit.second.getMilliseconds())
{
elapsedSec = elapsedMillis / TimeUnit.second.getMilliseconds();
if (elapsedSec >= 60)
{
elapsedMin = elapsedSec / 60;
elapsedSec = elapsedSec % 60;
if (elapsedMin >= 60)
{
elapsedHour = elapsedMin / 60;
elapsedMin = elapsedMin % 60;
}
}
output = String.format("%02d:%02d:%02d", elapsedHour, elapsedMin, elapsedSec);
}
else
{
output = String.format("00:00:00.%03d", elapsedMillis);
}
return output;
}
//--------------------------------------------------------------------------
public static String getISO_8601_Date()
{
return getISO_8601_Date(new Date());
}
//--------------------------------------------------------------------------
public static String getISO_8601_Date(Date inValue)
{
return new SimpleDateFormat("yyyy-MM-dd").format(inValue);
}
//--------------------------------------------------------------------------
public static String getYYYYMMDD()
{
return getYYYYMMDD(new Date());
}
//--------------------------------------------------------------------------
public static String getYYYYMMDD(Date inValue)
{
return new SimpleDateFormat("yyyyMMdd").format(inValue);
}
//--------------------------------------------------------------------------
public static String getYYYY_MM_DD()
{
return new SimpleDateFormat("yyyy.MM.dd").format(new Date());
}
//--------------------------------------------------------------------------
public static String getYYYY_MM_DD_HH_mm_aa()
{
return getYYYY_MM_DD_HH_mm_aa(new Date());
}
//--------------------------------------------------------------------------
public static String getYYYY_MM_DD_HH_mm_aa(Date inDate)
{
return new SimpleDateFormat("yyyy.MM.dd HH:mm aa").format(inDate);
}
//--------------------------------------------------------------------------
public static String getYYYY_MM_DD_HH_mm_ss()
{
return getYYYY_MM_DD_HH_mm_aa(new Date());
}
//--------------------------------------------------------------------------
public static String getYYYY_MM_DD_HH_mm_ss(Date inDate)
{
return new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(inDate);
}
}