public class

DateUtils

extends Object
java.lang.Object
   ↳ com.atlassian.core.util.DateUtils

Summary

Nested Classes
class DateUtils.DateRange  
enum DateUtils.Duration  
Constants
String AM
String PM
Fields
public static final long DAY_MILLIS
public static final long HOUR_MILLIS
public static final DateFormat ISO8601DateFormat Date Format to be used for internal logging operations
public static final long MINUTE_MILLIS
public static final long MONTH_MILLIS
public static final long SECOND_MILLIS
public static final long YEAR_MILLIS
Public Constructors
DateUtils(ResourceBundle resourceBundle)
Public Methods
static String dateDifference(long dateA, long dateB, long resolution, ResourceBundle resourceBundle)
Resolution is the degree of difference.
String dateDifferenceBean(long dateA, long dateB, long resolution, ResourceBundle resourceBundle)
static boolean equalTimestamps(Timestamp t1, Timestamp t2)
compares if these two timestamps are within 10 milliseconds of each other (precision error)
static String formatDateISO8601(Date ts)
String formatDurationPretty(String seconds)
This is used by the Velocity templates as a bean
String formatDurationPretty(long l)
This is used by the Velocity templates as a bean
String formatDurationString(long l)
This method is deprecated. You should be calling formatDurationPretty
static int get24HourTime(String meridianIndicator, int hours)
static Calendar getCalendarDay(int year, int month, int day)
static Date getDateDay(int year, int month, int day)
static long getDuration(String durationStr)
Given a duration string, get the number of seconds it represents (all case insensitive):
  • w = weeks
  • d = days
  • h = hours
  • m = minutes
If no category is specified, assume minutes.
static long getDuration(String durationStr, int hoursPerDay, int daysPerWeek, DateUtils.Duration defaultUnit)
This function retrieves a duration in seconds that depends on number of hours in a day and days in a week
static long getDuration(String durationStr, DateUtils.Duration defaultUnit)
Given a duration string, get the number of seconds it represents (all case insensitive):
  • w = weeks
  • d = days
  • h = hours
  • m = minutes
ie 2h = 7200, 60m = 3600, 3d = 259200, 30m
static long getDuration(String durationStr, int hoursPerDay, int daysPerWeek)
This function retrieves a duration in seconds that depends on number of hours in a day and days in a week.
static String getDurationPretty(long numSecs, ResourceBundle resourceBundle)
Converts a number of seconds into a pretty formatted data string.
static String getDurationPretty(long numSecs, int hoursPerDay, int daysPerWeek, ResourceBundle resourceBundle)
Converts a number of seconds into a pretty formatted data string.
static String getDurationPrettySeconds(long numSecs, long secondsPerDay, long secondsPerWeek, ResourceBundle resourceBundle)
Get a pretty formatted duration for the given number of seconds.
static String getDurationPrettySecondsResolution(long numSecs, int hoursPerDay, int daysPerWeek, ResourceBundle resourceBundle)
Converts a number of seconds into a pretty formatted data string.
static String getDurationPrettySecondsResolution(long numSecs, ResourceBundle resourceBundle)
Converts a number of seconds into a pretty formatted data string.
static long getDurationSeconds(String durationStr, long secondsPerDay, long secondsPerWeek, DateUtils.Duration defaultUnit)
Convert a duration string in the number of seconds it represents.
static String getDurationString(long seconds)
Get String representation of a duration

static String getDurationString(long l, int hoursPerDay, int daysPerWeek)
Get a duration string representing the given number of seconds.
static String getDurationStringSeconds(long l, long secondsPerDay, long secondsPerWeek)
Get a duration string representing the given number of seconds.
static String getDurationStringWithNegative(long seconds)
Get String representation of a (possibly negative) duration.
static long getDurationWithNegative(String durationStr)
Get a duration string with the possibility of a negative.
static Date getSqlDateDay(int year, int month, int day)
static DateUtils.DateRange toDateRange(Calendar date, int period)
Given a period, and a date that falls within that period, create a range of dates such that the period is contained exactly within [startDate
static Calendar toEndOfPeriod(Calendar calendar, int period)
Change the date of a Calendar object so that it has the maximum resolution of "period" where period is one of the constants in CALENDAR_PERIODS above.
static Calendar toStartOfPeriod(Calendar calendar, int period)
Change the date of a Calendar object so that it has the minimum resolution of "period" where period is one of the constants in CALENDAR_PERIODS above.
static Date tomorrow()
static boolean validDuration(String s)
Check whether a given duration string is valid
static Date yesterday()
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String AM

Constant Value: "am"

public static final String PM

Constant Value: "pm"

Fields

public static final long DAY_MILLIS

public static final long HOUR_MILLIS

public static final DateFormat ISO8601DateFormat

Date Format to be used for internal logging operations

public static final long MINUTE_MILLIS

public static final long MONTH_MILLIS

public static final long SECOND_MILLIS

public static final long YEAR_MILLIS

Public Constructors

public DateUtils (ResourceBundle resourceBundle)

Public Methods

public static String dateDifference (long dateA, long dateB, long resolution, ResourceBundle resourceBundle)

Resolution is the degree of difference.

0 = months 1 = days 2 = hours 3 = minutes 4 = seconds
Parameters
dateA first date to compare
dateB second date to compare
resolution the degree of difference
resourceBundle contains localizations for core.dateutils strings
Returns
  • the difference between the two dates as a human readable string (i.e. 2 months, 3 days, 4 hours)

public String dateDifferenceBean (long dateA, long dateB, long resolution, ResourceBundle resourceBundle)

public static boolean equalTimestamps (Timestamp t1, Timestamp t2)

compares if these two timestamps are within 10 milliseconds of each other (precision error)

Parameters
t1 first timestamp to compare
t2 second timestamp to compare
Returns
  • true if the two timestamps are within 10 milliseconds of one another

public static String formatDateISO8601 (Date ts)

public String formatDurationPretty (String seconds)

This is used by the Velocity templates as a bean

Parameters
seconds duration as a string
Returns
  • a pretty formatted version of the duration

public String formatDurationPretty (long l)

This is used by the Velocity templates as a bean

Parameters
l a duration in seconds
Returns
  • a pretty formatted version of the duration

public String formatDurationString (long l)

This method is deprecated.
You should be calling formatDurationPretty

This is used by the WebWork tags as a bean Despite the name it doesn't actually format a duration string. It takes a long.

Parameters
l a duration in seconds
Returns
  • a pretty formatted version of the duration

public static int get24HourTime (String meridianIndicator, int hours)

public static Calendar getCalendarDay (int year, int month, int day)

public static Date getDateDay (int year, int month, int day)

public static long getDuration (String durationStr)

Given a duration string, get the number of seconds it represents (all case insensitive):

  • w = weeks
  • d = days
  • h = hours
  • m = minutes
If no category is specified, assume minutes.
Each field must be separated by a space, and they can come in any order. Case is ignored.

ie 2h = 7200, 60m = 3600, 3d = 259200, 30m
Parameters
durationStr the duration string
Returns
  • the duration in seconds
Throws
InvalidDurationException if the duration is invalid

public static long getDuration (String durationStr, int hoursPerDay, int daysPerWeek, DateUtils.Duration defaultUnit)

This function retrieves a duration in seconds that depends on number of hours in a day and days in a week

Parameters
durationStr to convert to a duration
hoursPerDay Number of hourse i day
daysPerWeek Days Per Week
defaultUnit the unit used when one is not specified on a measure in the durationStr
Returns
  • the duration in seconds
Throws
InvalidDurationException if its badly formatted duration

public static long getDuration (String durationStr, DateUtils.Duration defaultUnit)

Given a duration string, get the number of seconds it represents (all case insensitive):

  • w = weeks
  • d = days
  • h = hours
  • m = minutes
ie 2h = 7200, 60m = 3600, 3d = 259200, 30m
Parameters
durationStr the duration string
defaultUnit the unit used when another is not specified in the durationStr
Returns
  • the duration in seconds
Throws
InvalidDurationException if the duration is invalid

public static long getDuration (String durationStr, int hoursPerDay, int daysPerWeek)

This function retrieves a duration in seconds that depends on number of hours in a day and days in a week. The default unit is MINUTE (i.e. "2" == "2 minutes")

Parameters
durationStr to convert to a duration
hoursPerDay Number of hourse i day
daysPerWeek Days Per Week
Returns
  • the duration in seconds
Throws
InvalidDurationException if its badly formatted duration

public static String getDurationPretty (long numSecs, ResourceBundle resourceBundle)

Converts a number of seconds into a pretty formatted data string. The resolution is in minutes. So if the number of seconds is greater than a minute, it will only be shown down top minute resolution. If the number of seconds is less than a minute it will be shown in seconds.

So for example 76 becomes '1 minute', while 42 becomes '42 seconds'
Parameters
numSecs the number of seconds in the duration
resourceBundle a resouce bundle for i18n
Returns
  • a string in readable pretty duration format, using minute resolution

public static String getDurationPretty (long numSecs, int hoursPerDay, int daysPerWeek, ResourceBundle resourceBundle)

Converts a number of seconds into a pretty formatted data string. The resolution is in minutes. So if the number of seconds is greater than a minute, it will only be shown down top minute resolution. If the number of seconds is less than a minute it will be shown in seconds.

So for example 76 becomes '1 minute', while 42 becomes '42 seconds'
Parameters
numSecs the number of seconds in the duration
hoursPerDay the hours in a day
daysPerWeek the number of days in a week
resourceBundle a resouce bundle for i18n
Returns
  • a string in readable pretty duration format, using minute resolution

public static String getDurationPrettySeconds (long numSecs, long secondsPerDay, long secondsPerWeek, ResourceBundle resourceBundle)

Get a pretty formatted duration for the given number of seconds. (e.g. "4 days, 2 hours, 30 minutes")

Parameters
numSecs the number of seconds in the duration
secondsPerDay the number of seconds in a "day"
secondsPerWeek the number of seconds in a "week"
resourceBundle the bundle containing translations for the strings used in the pretty string (e.g. "days")
Returns
  • the formatted pretty duration

public static String getDurationPrettySecondsResolution (long numSecs, int hoursPerDay, int daysPerWeek, ResourceBundle resourceBundle)

Converts a number of seconds into a pretty formatted data string. The resolution is in seconds.

So for example 76 becomes '1 minute, 16 seconds', while 42 becomes '42 seconds'
Parameters
numSecs the number of seconds in the duration
hoursPerDay the hours in a day
daysPerWeek the number of days in a week
resourceBundle a resouce bundle for i18n
Returns
  • a string in readable pretty duration format, using second resolution

public static String getDurationPrettySecondsResolution (long numSecs, ResourceBundle resourceBundle)

Converts a number of seconds into a pretty formatted data string. The resolution is in seconds.

So for example 76 becomes '1 minute, 16 seconds', while 42 becomes '42 seconds'
Parameters
numSecs the number of seconds in the duration
resourceBundle a resouce bundle for i18n
Returns
  • a string in readable pretty duration format, using second resolution

public static long getDurationSeconds (String durationStr, long secondsPerDay, long secondsPerWeek, DateUtils.Duration defaultUnit)

Convert a duration string in the number of seconds it represents. This method takes seconds per day and seconds per weeks instead of "hours per day" or "days per week" because we may want a non-integral number of hours per day.

Parameters
durationStr the duration string
secondsPerDay number of seconds in a working "day" (e.g. could be equal 6.5 hours)
secondsPerWeek number of seconds in a working "week" (e.g. could be equal to 4.5 days)
defaultUnit the unit to use for numbers with no unit specified (e.g. "12")
Returns
  • the number of seconds representing the duration string
Throws
InvalidDurationException if the duration string cannot be parsed

public static String getDurationString (long seconds)

Get String representation of a duration

Parameters
seconds Number of seconds
Returns
  • String representing duration, eg: "1h 30m"

public static String getDurationString (long l, int hoursPerDay, int daysPerWeek)

Get a duration string representing the given number of seconds. The string will use the largest unit possible. (i.e. 1w 3d)

Parameters
l the number of seconds
hoursPerDay hours in a working day
daysPerWeek days in a working week
Returns
  • the duration string

public static String getDurationStringSeconds (long l, long secondsPerDay, long secondsPerWeek)

Get a duration string representing the given number of seconds. The string will use the largest unit possible. (i.e. 1w 3d). Use this method when you want to specify a non-integral number of hours in a day (e.g. 7.5) or days per week.

Parameters
l the number of seconds
secondsPerDay the number of seconds in a working day
secondsPerWeek the number of seconds in a working week
Returns
  • the formatted duration string

public static String getDurationStringWithNegative (long seconds)

Get String representation of a (possibly negative) duration.

Parameters
seconds Number of seconds
Returns
  • String representing duration, eg: "-1h 30m"

public static long getDurationWithNegative (String durationStr)

Get a duration string with the possibility of a negative.

A duration will be considered negative if the first non-space character is a - sign.
Parameters
durationStr the duration string
Returns
  • the duration in seconds, which can be negative
Throws
InvalidDurationException if its a badly formatted duration

public static Date getSqlDateDay (int year, int month, int day)

public static DateUtils.DateRange toDateRange (Calendar date, int period)

Given a period, and a date that falls within that period, create a range of dates such that the period is contained exactly within [startDate

Parameters
date a calendar object of a date falling in that range
period something in CALENDAR_PERIODS
Returns
  • resulting range of dates

public static Calendar toEndOfPeriod (Calendar calendar, int period)

Change the date of a Calendar object so that it has the maximum resolution of "period" where period is one of the constants in CALENDAR_PERIODS above.

e.g. to obtain the maximum value for a month, call toEndOfPeriod(calendarObject, Calendar.MONTH)
Parameters
calendar The Calendar to change
period The period to "maximise"
Returns
  • A modified Calendar object

public static Calendar toStartOfPeriod (Calendar calendar, int period)

Change the date of a Calendar object so that it has the minimum resolution of "period" where period is one of the constants in CALENDAR_PERIODS above.

Parameters
calendar calendar to modify
period the new calendar period from CALENDAR_PERIODS
Returns
  • the calendar that was passed in

public static Date tomorrow ()

public static boolean validDuration (String s)

Check whether a given duration string is valid

Parameters
s the duration string
Returns
  • true if it a valid duration

public static Date yesterday ()