Java on dates
JDate: Date/time
Don't forget to check the documentation for the JDate Class
Rounding off a time to 5 minutes (modulo, to the lower value)
This can also be done more simply by configuring the java operation to return a date and setting a modulo. In this case, only return jDate is necessary for the modulo to be computed.
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
jDate.setSecond(0);
jDate.setMinute(jDate.getMinute() - jDate.getMinute() % 5);
return jDate;
/**
* ============================
* MX New common function
* ============================
*
* Description : Rounding time to _str2 min
* Input : String _str1 : DateTime
* String _str2 : Number of minutes to round to
* Output : Double : Datetime rounded
*
*/
// _str2 is a constant
return MxDate_rounding_lower(_doJava, _str1, _str2);
Rounding a date day from 5AM to 5AM
You have to use the modulo date 1 day and add following code java
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1)) return "";
jDate.setHour(jDate.getHour() - 5);
return jDate;
Rounding off a time to 5 minutes (round, to the nearest value)
This is an alternate method for specific cases. Rounding using the modulo is the standard method.
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
jDate.setSecond(0);
jDate.setMinute((int) Math.round(jDate.getMinute()/5.0)*5);
return jDate;
/**
* ============================
* MX New common function
* ============================
*
* Description : Rounding time to _str2 min
* Input : String _str1 : DateTime
* String _str2 : Number of minutes to round to
* Output : Double : Datetime rounded
*
*/
// _str2 is a constant
return MxDate_rounding_nearest(_doJava, _str1, _str2);
Rounding a date to 2 ODD hours
There is certainly a simpler way to do this
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
int jour=jDate.getDay();
int heure=jDate.getHour();
jDate.setMinute(0);
jDate.setSecond(0);
if (heure == 1 || heure == 2) heure = 1;
if (heure == 3 || heure == 4) heure = 3;
if (heure == 5 || heure == 6) heure = 5;
if (heure == 7 || heure == 8) heure = 7;
if (heure == 9 || heure == 10) heure = 9;
if (heure == 11 || heure == 12) heure = 11;
if (heure == 13 || heure == 14) heure = 13;
if (heure == 15 || heure == 16) heure = 15;
if (heure == 17 || heure == 18) heure = 17;
if (heure == 19 || heure == 20) heure = 19;
if (heure == 21 || heure == 22 ) heure = 21;
if (heure == 00 )
{
heure = 23;
jour = jour - 1;
}
jDate.setDay(jour);
jDate.setHour(heure);
return jDate.formatAAAAMMJJ_HHMMSS();