tig
Class Dates
- java.lang.Object
-
- tig.Dates
-
- All Implemented Interfaces:
- GeneralConstants
public abstract class Dates extends java.lang.Object implements GeneralConstants
Class containing utility static methods for date manipulation.
Dates are represented as an array of integers.
The internationalization of this class is hard coded (French and English are supported).
This class handles :- Date - julian day conversions (
dateToJd(), jdToDate()).- Conversion between a String and a date (
parseDate(), parseHour()).- Date formatting for display (
formatDate(), formatHour()).
-
-
Constructor Summary
Constructors Constructor and Description Dates()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static doublecalcDecDay(int d, int h, int m, int s)Returns the decimal part of the day.static doubledateToJd(int[] date)Convenient method equivalent todateToJd(int, int, int, int, int, int).static doubledateToJd(int year, int month, int day, int hour, int minut, int sec)Calculates the number of julian days elapsed since jan 0.5 4712 BC, from a date and an hour.static java.lang.StringformatDate(int[] date, java.lang.String lang)Formats a date into a displayable String.static java.lang.StringformatDate(java.lang.String strDate, java.lang.String lang)Facility method, callingparseDate()andformatDate(int[], String).static java.lang.StringformatHour(int[] hour, java.lang.String lang)Formats an hour into a displayable String.static java.lang.StringformatHour(java.lang.String strHour, java.lang.String lang)Facility method, callingparseHour()andformatHour(int[], String).static java.lang.StringformatHourDate(int[] hourDate, java.lang.String lang)Formats a date and an hour into a displayable String.static java.lang.StringformatHourDate(java.lang.String strDate, java.lang.String strHour, java.lang.String lang)Facility method, callingparseHourDate()andformatHourDate(int[], String).static booleanisLeapYear(int y)Tests ifycorresponds to a leap year.static int[]jdToDate(double jd)Computes the date and hour corresponding to a julian day (nb of days elapsed since jan 0.5 4712 BC).static int[]now()Returns an array containing date fields (in this order : year, month, day, hour, minuts, seconds) initialized to the current date.static int[]parseDate(java.lang.String strDate, java.lang.String lang)Transforms a date representated as a String to an array of integers containing the year, month, day.static int[]parseHour(java.lang.String strHour, java.lang.String lang)Transforms an hour representated as a String to an array of integers containing (in this order : hours, minutes, seconds.static int[]parseHourDate(java.lang.String strDate, java.lang.String strHour, java.lang.String lang)Transforms a date and an hour representated as Strings to an array of integers containing (in this order : years, months, days, hours, minutes, seconds.
-
-
-
Method Detail
-
dateToJd
public static double dateToJd(int[] date)
Convenient method equivalent todateToJd(int, int, int, int, int, int).
-
dateToJd
public static double dateToJd(int year, int month, int day, int hour, int minut, int sec)Calculates the number of julian days elapsed since jan 0.5 4712 BC, from a date and an hour.
-
jdToDate
public static int[] jdToDate(double jd)
Computes the date and hour corresponding to a julian day (nb of days elapsed since jan 0.5 4712 BC).
From Jean Meeus' Astronomical Algorithms, 2nd edition, p 63.- Parameters:
jd- A positive number expressing the julian day.- Returns:
- An array of six integers containing (in this order) : year, month, day, hour, minut, second.
- Throws:
java.lang.RuntimeException- If parameter 'jd' < 0.
-
isLeapYear
public static boolean isLeapYear(int y)
Tests ifycorresponds to a leap year.- Parameters:
y- positive number representing the year to test.- Returns:
- true if
yis positive and corresponds to a leap year.
-
now
public static int[] now()
Returns an array containing date fields (in this order : year, month, day, hour, minuts, seconds) initialized to the current date.
-
formatHourDate
public static java.lang.String formatHourDate(int[] hourDate, java.lang.String lang)Formats a date and an hour into a displayable String.- Parameters:
hourDate- An array containing integers representing : year, month, day, hour, minut, second.lang- language code, as specified by ISO-639 norm (supported languages : "en" and "fr").- Returns:
- if lang='fr', format is "JJ/MM/AAAA HH:MM:SS" ;
if lang='en', format is "MM/JJ/AAAA HH:MM:SS".
- Throws:
java.lang.IllegalArgumentException- if :- the length of 'hourDate' is different from 6 ;
- the language is different from "en" or "fr".
-
-
formatDate
public static java.lang.String formatDate(int[] date, java.lang.String lang)Formats a date into a displayable String.- Parameters:
date- An array containing 3 integers representing : year, month, day.lang- language code, as specified by ISO-639 norm (supported languages : "en" and "fr").- Returns:
- if lang='fr', format is "JJ/MM/AAAA" ;
if lang='en', format is "MM/JJ/AAAA".
- Throws:
java.lang.IllegalArgumentException- if :- the length of 'date' is different from 3 ;
- the language is different from "en" or "fr".
-
-
formatHour
public static java.lang.String formatHour(int[] hour, java.lang.String lang)Formats an hour into a displayable String.- Parameters:
hour- An array containing 3 integers representing : hour, minut, second.- Returns:
- A String formatted "HH:MM:SS", for lang='fr' or 'en';
- Throws:
java.lang.IllegalArgumentException- if :- the length of 'hour' is different from 3 ;
- the language is different from "en" or "fr".
-
formatHourDate
public static java.lang.String formatHourDate(java.lang.String strDate, java.lang.String strHour, java.lang.String lang) throws DateFormatException, HourFormatExceptionFacility method, callingparseHourDate()andformatHourDate(int[], String).- Throws:
DateFormatException- if parameter 'strDate' is not correct.HourFormatException- if parameter 'strHour' is not correct.
-
formatDate
public static java.lang.String formatDate(java.lang.String strDate, java.lang.String lang) throws DateFormatExceptionFacility method, callingparseDate()andformatDate(int[], String).- Throws:
DateFormatException- if parameter 'strDate' is not correct.
-
formatHour
public static java.lang.String formatHour(java.lang.String strHour, java.lang.String lang) throws HourFormatExceptionFacility method, callingparseHour()andformatHour(int[], String).- Throws:
HourFormatException- if parameter 'strHour' is not correct.
-
parseHourDate
public static int[] parseHourDate(java.lang.String strDate, java.lang.String strHour, java.lang.String lang) throws DateFormatException, HourFormatException, java.lang.IllegalArgumentExceptionTransforms a date and an hour representated as Strings to an array of integers containing (in this order : years, months, days, hours, minutes, seconds.
This method just callsparseHour(String, String)andparseDate(String, String). See their documentation for informations about parameter formats and exception thrown.- Throws:
DateFormatExceptionHourFormatExceptionjava.lang.IllegalArgumentException
-
parseDate
public static int[] parseDate(java.lang.String strDate, java.lang.String lang) throws DateFormatException, java.lang.IllegalArgumentExceptionTransforms a date representated as a String to an array of integers containing the year, month, day.
How to express the date in 'strDate' :- Years, months and days must be separated by any non-numeric separator.
- Years, months and days can be expressed with any number of digits ; years can be preceeded by a minus sign (if you want to express a negative year, first put a separator, and then the '-').
- For English ('lang' = "en"), the order in 'strDate' must be months, days, years.
- For French ('lang' = "fr"), the order in 'strDate' must be days, months, years.
Example of valid inputs and results.
French :strDate years months days 18/07/1998 18 7 1998 18.007:1998 18 7 1998 9-10 2000 9 10 2000 9 10 1 9 10 1 9 10 -16 9 10 -16 9-10--16 9 10 -16 9-10-16 9 10 16
English :strDate year Month Day 07/18/1998 18 7 1998 007:18.1998 18 7 1998 9-10 2000 10 9 2000 9 10 1 9 10 1 9 10 -16 10 9 -16 9-10--16 10 9 -16 9-10-16 10 9 16 - Parameters:
strDate- A String containing the date, as specified above.lang- language code, as specified by ISO-639 norm (supported languages : "en" and "fr").- Returns:
- an array of integers in which are stored (in this order) years, months, days.
- Throws:
java.lang.IllegalArgumentException- if parameter 'lang' is different from 'en' or 'fr'.DateFormatException- if parameter 'strDate' is not correctly formatted.
-
parseHour
public static int[] parseHour(java.lang.String strHour, java.lang.String lang) throws HourFormatException, java.lang.IllegalArgumentExceptionTransforms an hour representated as a String to an array of integers containing (in this order : hours, minutes, seconds.
Parameter 'lang' has no effect.
How to express the hour in 'strHour' :- Hours, minuts and seconds must be separated by any non-numeric character
- Any non-numeric digit at the beginning or at the end of 'strHour" are skipped
- If 'strHour' is empty, hours, minuts, seconds will be considered as equal to 0.
- Seconds or {minutes and seconds} can be omitted, they will be considered as equal to 0.
Example of valid inputs and results.strHour hours minuts seconds (empty string) 0 0 0 12 12 0 0 12h30m 12 30 0 -12:30:40s 12 30 40 000012h000030m00040 12 30 40 - Parameters:
strHour- A String containing the hour, as specified above..lang- language code, as specified by ISO-639 norm (supported languages : 'en' and 'fr').- Returns:
- an array of integers in which are stored (in this order) hours, minuts, seconds.
- Throws:
HourFormatException- if parameter 'strHour' is not correctly formatted.java.lang.IllegalArgumentException- if parameter 'lang' is different from 'en' or 'fr'.
-
calcDecDay
public static double calcDecDay(int d, int h, int m, int s)Returns the decimal part of the day. The decimal part of the day expresses the fraction within the day.
ex : 23.75 means 23rd, 18h00m00s.
DMelt 3.0 © DataMelt by jWork.ORG