CALCULATION - MX

Java on dates

JDate: Date/time

Don't forget to check the documentation for the JDate Class

Returning a date -17h

JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1)) return "";
  jDate.setHour(jDate.getHour() - 17);
return jDate;
// ================================
// New MX Common function 
// ================================
/**
 * Description : Removing x hours
 * Input : Date _str1 
 *         int hours to remove
 * Output : Date _str1 - _str2
 */
return MxDate_remove_hours(_doJava, _str1, _str2);
// or
return MxDate_remove_hours(_doJava, _str1, "17");

// Exist also with 
MxDate_remove_minutes(_doJava, _str1, _str2);
MxDate_remove_seconds(_doJava, _str1, _str2);

Returning a date relative to a variable duration (in hours)

JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
   return "";
Long lag = (long) (Str.ToDouble(_str2)*JDatePeriod.ONE_HOUR);
jDate.setTimeInMillis(jDate.getTimeInMillis() - lag);
jDate.setSecond(0);
return jDate;

Getting the month to change on the 1st day at 4 AM

if (!_doJava.isDate(_str1))
return "";
JDate jDate=_doJava.getNewJDate(_str1);

int month = jDate.getMonth()+1;
int prev_month = jDate.getMonth();
int heure=jDate.getHour();
int jour = jDate.getDay();

if (heure <=3 && jour <=1 && jour >=1)
  return prev_month;
return month;

Computing shifts or factions

Sorting hours per group: 6h-14h-22h...

JDate jDate=new JDate();
if (!jDate.set(_str1))
  return "";

int heure=jDate.getHour();
//test 
jDate.setMinute(0);
jDate.setSecond(0);

if (heure >=6 && heure <14)
  jDate.setHour(14);
if (heure >=14 && heure <22)
  jDate.setHour(22);
if (heure >=22)
{
  jDate.setHour(6);
  jDate.setDay(jDate.getDay()+1);
}
if (heure <6)
  jDate.setHour(6);
return jDate;
// ================================
// New MX Common function 
// ================================
/**
 * Description : Removing everything after first non-numeric character
 * Input : String _str1 : input string
 *         int _str2 : first shift 
 *         int _str3 : second shift
 *         int _str4 : third shift
 * Output : first hour of shift 

 */
// _str2, 3 & 4 set as constants 
return MxShifts_sort_hours(_doJava, _str1, _str2, _str3, _str4);

// _str2, 3 & 4 set as whatever constant (not used)
// example is same as above
 return MxShifts_sort_hours(_doJava, _str1, "6", "14", "22");

Sorting hours per group: 5h-13h-21h & dealing with the week-ends

JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
  return "";
int heure=jDate.getHour();
int day=jDate.getDayOfWeek()-1;
if (day==0) day=7;
if (heure <5 && day==1)
return "WE Nuit";
if (heure >=5 && heure <13 && day<=5)
  return "Matin";
if (heure >=13 && heure <21 && day<=5)
  return "Soir";
if ((heure >=21 || heure <5 )&& day<=5)
return "Nuit";
if (heure <5 && day==5)
return "Nuit";
if (heure >=5 && heure <17 && day>5)
  return "WE Matin";
if ((heure >=17 || heure <5) && day>5)
  return "WE Nuit";
return "";
}}

Computing crews

JDate jDate=new JDate();
if (!jDate.set(_str1))
   return "";
JDate jDatedeb=new JDate("20131231_235959"); 
jDate.setHour(jDate.getHour() - 3); // In the example the crew starts at 3h30
jDate.setMinute(jDate.getMinute() - 30);

int heure=jDate.getHour();

long heurehe=0;
// If it is summer time add 1, or else stay at 0 (because the computation start date is in winter time. Or else maybe we should remove 1 ==> to be tested)
if (java.util.TimeZone.getDefault().inDaylightTime(new java.util.Date(jDate.getTimeInMillis())))
   heurehe=1;
else
   heurehe=0;

long faction=((((((jDate.getTimeInMillis()-jDatedeb.getTimeInMillis())/1000)/3600)+heurehe)/24 ) % 10); // In this example the rotation cycle is over 10 days

if (heure>=0 && heure<8 && (faction==0 || faction==1))
   return "A";
if (heure>=0 && heure<8 && (faction==2 || faction==3))
   return "B";
if (heure>=0 && heure<8 && (faction==4 || faction==5))
   return "C";
if (heure>=0 && heure<8 && (faction==6 || faction==7))
   return "D";
if (heure>=0 && heure<8 && (faction==8 || faction==9))
   return "E";

if (heure>=8 && heure<16 && (faction==2 || faction==3))
   return "A";
if (heure>=8 && heure<16 && (faction==4 || faction==5))
   return "B";
if (heure>=8 && heure<16 && (faction==6 || faction==7))
   return "C";
if (heure>=8 && heure<16 && (faction==8 || faction==9))
   return "D";
if (heure>=8 && heure<16 && (faction==0 || faction==1))
   return "E";

if (heure>=16 && heure<24 && (faction==4 || faction==5))
   return "A";
if (heure>=16 && heure<24 && (faction==6 || faction==7))
   return "B";
if (heure>=16 && heure<24 && (faction==8 || faction==9))
   return "C";
if (heure>=16 && heure<24 && (faction==0 || faction==1))
   return "D";
if (heure>=16 && heure<24 && (faction==2 || faction==3))
   return "E";

return "";

DateNew and DateModify

Difference between dateNew and dateModify :

long lDateNew=Str.ToLong(_str1);
long lDateModify=Str.ToLong(_str2);
if (lDateModify==0)
  // Data was never modified
  return "";
return (lDateNew-lDateModify)/1000/60/24;

Converting a string to a timestamp to get the milliseconds

try
{
   java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
   simpleDateFormat.setTimeZone(_doJava.getTimeZone());
   java.util.Date date = simpleDateFormat.parse(_str1);
   JDate jDate = _doJava.getNewJDate();
   jDate.setTime(date);
   return jDate.getTimeInMillis();
}
catch (java.text.ParseException ex)
{
}
return null;

Periods

They are in milliseconds.

The JDatePeriod class can be used.

For example JDatePeriod.ONE_MINUTE is equal to 60 000

// Number of milliseconds in one second
final static public long ONE_SECOND = 1000;

// Number of milliseconds in ten seconds
final static public long TEN_SECOND = 10000;

// Number of milliseconds in thirty seconds
final static public long THIRTY_SECOND = 30000;

// Number of milliseconds in one minute
final static public long ONE_MINUTE = 60000;

// Number of milliseconds in ten minutes
final static public long TEN_MINUTE = 600000;

// Number of milliseconds in one hour
final static public long ONE_HOUR = 60 * ONE_MINUTE;

// Number of milliseconds in onde day
final static public long ONE_DAY = 24 * ONE_HOUR;

// Number of milliseconds in one week
final static public long ONE_WEEK = ONE_DAY * 7;

// Number of milliseconds in one month
final static public long ONE_MONTH = 30 * ONE_DAY;

// Number of milliseconds in one year
final static public long ONE_YEAR = 365 * ONE_DAY;

Lines blocked for a certain time

If you want to block some lines for 1h for exampe you have to compare the creation date with the currenttime :
_str1 = CreationDate/in.java

JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
  return "";
jDate.setHour(jDate.getHour() + 1);

if (jDate.getTimeInMillis() > System.currentTimeMillis())
  return DoJava.DO_NOT_ADD_OPERATION_IN;
else
  return "OK";

Perform Multiple Operations on a JDate

This method is useful for performing multiple operations on a JDate. The trick is to reset the JDate in between operations.

JDate jDate = _doJava.getNewJDate();

if (!jDate.set(_str1))return "";

//Get the exact time yesterday
jDate.setHour(jDate.getHour() - 24);

//Commit change to jDate. The "" casts jDate as a string so .set() can accept it.
jDate.set(jDate + "");

//Remove hours/minutes
jDate.setHour(0);
jDate.setMinute(0);

return jDate;

List of French public holidays

This code is used to calculate the date of Easter for the year of the date given in parameter (_str1) and the date of the others public holidays that depend on it. It returns in the form of a list in JDate format all (calculated and fixed) French public holidays for the given year (with option for the specificities of Alsace-Lorraine).

/ Algorithm valid for French public holidays between the years 1583 and 4099
int Y,G,C,X,Z,D,E,N,P,J,M;
String StringPaques ="";

JDate JourAn=_doJava.getNewJDate();
JDate VendrediSaint=_doJava.getNewJDate();
JDate Paques=_doJava.getNewJDate();
JDate PaquesL=_doJava.getNewJDate();
JDate FeteTravail=_doJava.getNewJDate();
JDate Armistice39=_doJava.getNewJDate();
JDate Ascension=_doJava.getNewJDate();
JDate Pentecote=_doJava.getNewJDate();
JDate PentecoteL=_doJava.getNewJDate();
JDate FeteNationale=_doJava.getNewJDate();
JDate Assomption=_doJava.getNewJDate();
JDate Toussaint=_doJava.getNewJDate();
JDate Armistice14=_doJava.getNewJDate();
JDate Noel=_doJava.getNewJDate();
JDate LendemainNoel=_doJava.getNewJDate();
JDate jDate=_doJava.getNewJDate();

if (!jDate.set(_str1))
    return "";
jDate.set(_str1);

//Easter date calculation
Y = jDate.getYear();
G = (Y % 19) + 1;
C = (int) Math.floor(Y / 100) + 1;
X = (int) Math.floor(3 * C / 4) - 12;
Z = (int) Math.floor(((8 * C) + 5) / 25) - 5;
D = (int) Math.floor(((5 * Y) / 4) - X - 10);
E = ((11 * G) + 20 + Z - X) % 30;
if ((E == 25 && G > 11) || E == 24) E = E + 1;
N = 44 - E;
if (N < 21) N = N + 30;
P = N + 7 - ((D + N) % 7);
if (P > 31)
    J = P - 31;
else
    J = P;
if (J == P)
    M = 3;
else
    M = 4;

StringPaques = Y + Str.SetLen0(String.valueOf(M),2) + Str.SetLen0(String.valueOf(J),2) + "_000000";
Paques.set(StringPaques);

//Calculation of public holidays depending on easter
PaquesL.set(StringPaques);
Ascension.set(StringPaques);
Pentecote.set(StringPaques);
PentecoteL.set(StringPaques);

PaquesL.addDays(1);
Ascension.addDays(39);
Pentecote.addDays(49);
PentecoteL.addDays(50);

//Fixed public holidays
JourAn.set(Y + "0101_000000");
FeteTravail.set(Y + "0501_000000");
Armistice39.set(Y + "0508_000000");
FeteNationale.set(Y + "0714_000000");
Assomption.set(Y + "0815_000000");
Toussaint.set(Y + "1101_000000");
Armistice14.set(Y + "1111_000000");
Noel.set(Y + "1225_000000");

//For Alsace-Lorraine uncomment the 3 lines below
//VendrediSaint.set(StringPaques);
//VendrediSaint.addDays(-2);
//LendemainNoel.set(Y + "1226_000000");

//Storage of public holiday dates as a list

//General case
List<JDate> FERIES = Arrays.asList(JourAn,Paques,PaquesL,FeteTravail,Armistice39,Ascension,Pentecote,PentecoteL,FeteNationale,Assomption,Toussaint,Armistice14,Noel);
//For Alsace-Lorraine comment the line above and uncomment the one below
//List<JDate> FERIES = Arrays.asList(JourAn,VendrediSaint,Paques,PaquesL,FeteTravail,Armistice39,Ascension,Pentecote,PentecoteL,FeteNationale,Assomption,Toussaint,Armistice14,Noel,LendemainNoel);

You can then loop through the items in the list to, for example, compare them to your timestamp or any other use needed

for (int i = 0; i < FERIES.size(); i++)
    {
    //YOUR CODE HERE
    }

Was this article helpful?

Powered by Zendesk