Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.cantaa.util;
  
  import java.sql.Timestamp;
  import java.util.Calendar;
  import java.util.Date;

Utility-Methods around Date and Time

Author(s):
Hans Lesmeister
 
 public class DateUtil {

    
Get current date as timestamp

Returns:
timestamp
 
     public static Timestamp createTimestamp() {
         return new Timestamp(new Date().getTime());
     }

    
Creates a date specified with the passed values

Parameters:
year Year
month Month (1-based, so 1 = Jan., 2 = Feb.)
day Day of the month
Returns:
Date-Instance
 
     @SuppressWarnings("MagicConstant")
     public static Date createDate(int yearint monthint day) {
         Calendar cal = Calendar.getInstance();
         cal.set(yearmonth - 1, day);
         resetTimePart(cal);
         return cal.getTime();
     }

    
Creates a date based on the actual date but without time-settings, so the time part of this date is 00:00:00.000

Returns:
Date-Instance
 
     public static Date createDate() {
         Calendar calendar = Calendar.getInstance();
         resetTimePart(calendar);
         return calendar.getTime();
     }

    
Sets the time part in a calendar to zero (Hours, Minutes, Seconds, Milliseconds)

Parameters:
calendar Calendar
Throws:
java.lang.NullPointerException if calendar is null
 
     public static void resetTimePart(Calendar calendar) {
         setTimePart(calendar, 0, 0, 0, 0);
     }
 
     private static void setTimePart(Calendar calendarint hoursint minutesint secondsint milliseconds) {
         calendar.set(.hours);
         calendar.set(.minutes);
         calendar.set(.seconds);
         calendar.set(.milliseconds);
     }

    
Sets the time part to the last second of the day. This is used for queries with something like "date <= :someDate" and the date values in the table could have a time-part.

Parameters:
date Date. If null the the actual date will be taken
Returns:
date with the time-part set to 23:59:59 999
 
     public static Date setTimeToEndOfDay(Date date) {
         Calendar calendar = getCalendar(date);
         setTimePart(calendar, 23, 59, 59, 999);
         return calendar.getTime();
     }

    
Sets the time part of a date to zero

Parameters:
date Date
Returns:
new Date with time part set to zero
 
     public static Date resetTimePart(Date date) {
         Calendar calendar = getCalendar(date);
         resetTimePart(calendar);
         return calendar.getTime();
     }

    
Get a calendar

Parameters:
date a date or null
Returns:
calendar with the passe date set. If date is null then calendar represants the actual date
 
     public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        return cal;
    }
    public static Date addDays(Date dateint days) {
        Calendar c = getCalendar(date);
        c.add(.days);
        return c.getTime();
    }

    
Gets the Year-Part from a date

Parameters:
date Date to extract the year from. If null, then the actual year is returned
Returns:
The year, extracted from the date
    public static int getYear(Date date) {
        Calendar c = getCalendar(date);
        return c.get(.);
    }
New to GrepCode? Check out our FAQ X