Java on dates
JDate: Date/time
Don't forget to check the documentation for the JDate Class
Retrieving the week number (from Monday to Sunday)
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
return jDate.getWeekOfYear();
Unfortunately the getWeekOfYear() method returns 53 for jan. 1st 2016.
In order to get a string with the year and week number (v1.16):
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
return jDate.formatYYYY_WW();
Returning the day of the week (warning: 1 = Sunday)
To transform the numbers in week days
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
int jour = jDate.getDayOfWeek();
// In French
if (jour==1) return "07-DIMANCHE";
if (jour==2) return "01-LUNDI";
if (jour==3) return "02-MARDI";
if (jour==4) return "03-MERCREDI";
if (jour==5) return "04-JEUDI";
if (jour==6) return "05-VENDREDI";
if (jour==7) return "06-SAMEDI";
// In English
if (jour==1) return "07-SUNDAY";
if (jour==2) return "01-MONDAY";
if (jour==3) return "02-TUESDAY";
if (jour==4) return "03-WEDNESDAY";
if (jour==5) return "04-THURSDAY";
if (jour==6) return "05-FRIDAY";
if (jour==7) return "06-SATURDAY";
else return "";
/**
* ============================
* MX New common function
* ============================
*
* Description : Returning the day of the week (warning: 1 = Sunday)
* Input : String _str1 : Date
* Output : String "0X-DAY"
*
*/
return MxDate_day_of_week(_doJava, _str1);
Returning the name of the month: 0 = January and 11 = December
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
int mois = jDate.getMonth();
if (mois==0) return "01-JANUARY";
if (mois==1) return "02-FEBRUARY";
if (mois==2) return "03-MARCH";
if (mois==3) return "04-APRIL";
if (mois==4) return "05-MAY";
if (mois==5) return "06-JUNE";
if (mois==6) return "07-JULY";
if (mois==7) return "08-AUGUST";
if (mois==8) return "09-SEPTEMBER";
if (mois==9) return "10-OCTOBER";
if (mois==10) return "11-NOVEMBER";
if (mois==11) return "12-DECEMBER";
else return "";
/**
* ============================
* MX New common function
* ============================
*
* Description : Returning the name of the month: 0 = January and 11 = December
* Input : String _str1 : Date
* Output : String "0X-MONTH"
*
*/
return MxDate_name_month(_doJava, _str1);
To extract the year
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
return jDate.getYear();
Returning the first day of a week (with the year and number of the week)
I use the variable Year_Week as _str1
String Year = Str.RemoveAfter(_str1,"_");
String Week = Str.RemoveBefore(_str1,"_");
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.WEEK_OF_YEAR, Str.ToIntOrZero(Week));
cal.set(Calendar.YEAR, Str.ToIntOrZero(Year));
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return (sdf.format(cal.getTime())) + "_000000";
//And it return, 20160516_000000 for example with Year_Week : 2016_20
Returning the number of days in a month
Also works for leap years : returns "29" for "20160201_000000"
JDate jDate=_doJava.getNewJDate();
if (!jDate.set(_str1))
return "";
return jDate.getNbDaysInMonth();