Commande "cal" sous *ux

Soyez le premier à donner votre avis sur cette source.

Vue 6 527 fois - Téléchargée 271 fois

Description

Je sais : Ca existe depuis toujours sous *Ux...
Mais, malheureux consultant que je suis, je dois utiliser le même OS que le client...
Comme j'ai viré l'horloge de la "StartBar" et que j'ai toujours un shell. "Pardon!" Un "command prompt" ouvert.
J'ai fait une petite recherche pour trouver l'équivalent de cal en Wintendo... Après la première page, je me suis dit : Pourquoi pas le coder? J'ai donc vite codé ça sur l'heure de table(une grosse heure de table). ;-)

En clair: Ce que fait le programme :
Il affiche le calendrier de l'année demandée sinon l'année courante.
Les strings, ["chaines de caractères"], sont tirés de la local du PC. Ca veut dire que si votre PC et configuré English, ce sera January, February,... Et Mo, Tu ...
Ho! Il affiche un petit "<" après la date du jour.

Ce code n'a rien d'exceptionnel, mais si ça peut aider l'un ou l'autre...

PS: Dans le zip, j'ai packagé en Jar, contenant un MANIFEST.MF pour que la class soit exécuté toute seule.
----------------------------
MANIFEST.MF
----------------------------
Manifest-Version: 1.0
Main-Class: be.dje.tools.Cal
----------------------------
Ainsi qu'un cal.bat pour ceux qui n'aurait déjà(comme moi) une association .jar vers "7Zip".

Source / Exemple :


package be.dje.tools;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Vector;

public class Cal 
{
	static String semyear[][][]=new String [13][6][8];

	/**

  • @param args
  • /
public static void main(String[] args) { int annee=0; int dayofweek=0; int weekofmonth=0; int month=0; int index=0; Vector vDayNames=new Vector(); Vector vMonthNames=new Vector(); String MonthTitle=""; Date dt1; try { Calendar epoch = GregorianCalendar.getInstance(TimeZone.getDefault());//TimeZone.getTimeZone("Europe/Paris")); if (args.length!=0) annee=Integer.parseInt(args[0]); else annee=epoch.get(Calendar.YEAR); //init table for(int i=0;i<13;i++) for (int j=0;j<6;j++) for (int k=0;k<8;k++) semyear[i][j][k]=" "; System.out.println(annee); //Set epoch to 1st jan of the year dt1 = stringToDate("01/01/"+annee,"dd/MM/yyyy"); epoch.setTime(dt1); //fill the vector vDayNames with short names of days of the week DateFormat dateFormat = new SimpleDateFormat("EEE"); index=1;//sunday 1st while (index<8) { if (epoch.get(Calendar.DAY_OF_WEEK)==index) { vDayNames.add(dateFormat.format(epoch.getTime())); index++; } epoch.add(Calendar.DAY_OF_YEAR, 1);//while epoch isn't a sunday } //re initialize to 1st jan of the year dt1 = stringToDate("01/01/"+annee,"dd/MM/yyyy"); epoch.setTime(dt1); //fill the vector vMonthNames with full month names dateFormat = new SimpleDateFormat("MMMMM"); index=Calendar.JANUARY; while (index<=Calendar.DECEMBER) { vMonthNames.add(dateFormat.format(epoch.getTime())); index++; epoch.add(Calendar.MONTH, 1); } //re initialize to 1st jan of the year dt1 = stringToDate("01/01/"+annee,"dd/MM/yyyy"); epoch.setTime(dt1); //fill the table of the current year while (epoch.get(Calendar.YEAR)==annee) { dayofweek=epoch.get(Calendar.DAY_OF_WEEK); weekofmonth=epoch.get(Calendar.WEEK_OF_MONTH); month=epoch.get(Calendar.MONTH); if (epoch.get(Calendar.DAY_OF_MONTH)<10) semyear[month][weekofmonth][dayofweek]="0"+Integer.toString(epoch.get(Calendar.DAY_OF_MONTH)); else semyear[month][weekofmonth][dayofweek]=Integer.toString(epoch.get(Calendar.DAY_OF_MONTH)); epoch.add(Calendar.DAY_OF_YEAR, 1); } //Cycle for Lines of months for (int monthboucle=0;monthboucle<3;monthboucle++) { epoch = GregorianCalendar.getInstance(TimeZone.getDefault()); //************************************************************************************* //Print month names MonthTitle=((String)vMonthNames.get(0+monthboucle*4)); for(int i=MonthTitle.length();i<22;i++) MonthTitle+=" ";//up to char pos 22 MonthTitle+=((String)vMonthNames.get(1+monthboucle*4)); for(int i=MonthTitle.length();i<44;i++) MonthTitle+=" ";//up to char pos 44 MonthTitle+=((String)vMonthNames.get(2+monthboucle*4)); for(int i=MonthTitle.length();i<66;i++) MonthTitle+=" ";//up to char pos 66 MonthTitle+=((String)vMonthNames.get(3+monthboucle*4)); System.out.println(MonthTitle); //Show days name for (int i=0;i<4;i++) { for (int j=epoch.getFirstDayOfWeek()-1;j<7;j++) System.out.print(((String)vDayNames.get(j)).substring(0, 2)+" "); if (epoch.getFirstDayOfWeek()>1) for (int j=0;j<epoch.getFirstDayOfWeek()-1;j++) System.out.print(((String)vDayNames.get(j)).substring(0, 2)+" "); System.out.print(" "); } System.out.println(""); //print a line of months for (weekofmonth=0;weekofmonth<6;weekofmonth++)//6 lines per months { for (month=0;month<4;month++) //4 months per line { for (int day=epoch.getFirstDayOfWeek();day<8;day++) //7 days per week { System.out.print(semyear[month+monthboucle*4][weekofmonth][day]); if ((annee==epoch.get(Calendar.YEAR))&& //test to put a "<" after the current day ((month+monthboucle*4)==epoch.get(Calendar.MONTH))&& (!semyear[month+monthboucle*4][weekofmonth][day].trim().isEmpty())&& ( Integer.parseInt(semyear[month+monthboucle*4][weekofmonth][day].trim())==epoch.get(Calendar.DAY_OF_MONTH)) ) System.out.print("<"); else System.out.print(" "); } if (epoch.getFirstDayOfWeek()>1) for (int day=1;day<epoch.getFirstDayOfWeek();day++) { System.out.print(semyear[month+monthboucle*4][weekofmonth][day]); if ((annee==epoch.get(Calendar.YEAR))&& //test to put a "<" after the current day ((month+monthboucle*4)==epoch.get(Calendar.MONTH))&& (!semyear[month+monthboucle*4][weekofmonth][day].trim().isEmpty())&& ( Integer.parseInt(semyear[month+monthboucle*4][weekofmonth][day].trim())==epoch.get(Calendar.DAY_OF_MONTH)) ) System.out.print("<"); else System.out.print(" "); } System.out.print(" ");//split to next month on the same line } System.out.println("");// New line after week printed for 4 months } System.out.println(""); //new line after the "Months line" //************************************************************************************* }//endfor } catch (Exception e) { e.printStackTrace(); } } public static Date stringToDate(String sDate, String sFormat) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(sFormat); return sdf.parse(sDate); } }

Conclusion :


Tite remarque : J'ai pas blindé la lecture param. Si on s'ammuse à mettre autre chose qu'une valeur du type année, ça va petter en exception...

Codes Sources

A voir également

Ajouter un commentaire Commentaires
dje_jay Messages postés 58 Date d'inscription mercredi 17 décembre 2003 Statut Membre Dernière intervention 16 février 2011 2
26 sept. 2007 à 17:15
"Au secours !" pour?
Ca a l'air de fonctionner...
C'est le décalage qui t'en*? =>C'est prévu pour tourner en terminal "normal" avec une fonte de type courrier, ou [fixed width] en ts cas...
yvkoe Messages postés 32 Date d'inscription jeudi 20 septembre 2007 Statut Membre Dernière intervention 19 janvier 2009
25 sept. 2007 à 11:46
Au secours !
voila ce que cela donne une fois comilé et executé:
2007
janvier février mars avril
lu ma me je ve sa di lu ma me je ve sa di lu ma me je ve sa di lu ma me je ve sa di
01
01 02 03 04 05 06 07 01 02 03 04 01 02 03 04 02 03 04 05 06 07 08
08 09 10 11 12 13 14 05 06 07 08 09 10 11 05 06 07 08 09 10 11 09 10 11 12 13 14 15
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18 16 17 18 19 20 21 22
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25 23 24 25 26 27 28 29
29 30 31 26 27 28 26 27 28 29 30 31 30

mai juin juillet août
lu ma me je ve sa di lu ma me je ve sa di lu ma me je ve sa di lu ma me je ve sa di
01 02 03 01
01 02 03 04 05 06 04 05 06 07 08 09 10 02 03 04 05 06 07 08 01 02 03 04 05
07 08 09 10 11 12 13 11 12 13 14 15 16 17 09 10 11 12 13 14 15 06 07 08 09 10 11 12
14 15 16 17 18 19 20 18 19 20 21 22 23 24 16 17 18 19 20 21 22 13 14 15 16 17 18 19
21 22 23 24 25 26 27 25 26 27 28 29 30 23 24 25 26 27 28 29 20 21 22 23 24 25 26
28 29 30 31 30 31 27 28 29 30 31

septembre octobre novembre décembre
lu ma me je ve sa di lu ma me je ve sa di lu ma me je ve sa di lu ma me je ve sa di
01 02 01 02
03 04 05 06 07 08 09 01 02 03 04 05 06 07 01 02 03 04 03 04 05 06 07 08 09
10 11 12 13 14 15 16 08 09 10 11 12 13 14 05 06 07 08 09 10 11 10 11 12 13 14 15 16
17 18 19 20 21 22 23 15 16 17 18 19 20 21 12 13 14 15 16 17 18 17 18 19 20 21 22 23
24 25<26 27 28 29 30 22 23 24 25 26 27 28 19 20 21 22 23 24 25 24 25 26 27 28 29 30
29 30 31 26 27 28 29 30 31
C'st un petit peu décalé...
voila le code une fois les modif exécutée:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Vector;

public class Cal {
static String semyear[][][] = new String[13][6][8];

/**
* @param args
*/
public static void main(String[] args) {
int annee = 0;
int dayofweek = 0;
int weekofmonth = 0;
int month = 0;
int index = 0;
Vector vDayNames = new Vector();
Vector vMonthNames = new Vector();
String MonthTitle = "";

Date dt1;
try {
Calendar epoch = GregorianCalendar.getInstance(TimeZone
.getDefault());// TimeZone.getTimeZone("Europe/Paris"));
if (args.length != 0)
annee = Integer.parseInt(args[0]);
else
annee = epoch.get(Calendar.YEAR);

// init table
for (int i = 0; i < 13; i++)
for (int j = 0; j < 6; j++)
for (int k = 0; k < 8; k++)
semyear[i][j][k] = " ";

System.out.println(annee);

// Set epoch to 1st jan of the year
dt1 = stringToDate("01/01/" + annee, "dd/MM/yyyy");
epoch.setTime(dt1);

// fill the vector vDayNames with short names of days of the week
DateFormat dateFormat = new SimpleDateFormat("EEE");
index = 1;// sunday 1st
while (index < 8) {
if (epoch.get(Calendar.DAY_OF_WEEK) == index) {
vDayNames.add(dateFormat.format(epoch.getTime()));
index++;
}
epoch.add(Calendar.DAY_OF_YEAR, 1);// while epoch isn't a
// sunday
}

// re initialize to 1st jan of the year
dt1 = stringToDate("01/01/" + annee, "dd/MM/yyyy");
epoch.setTime(dt1);

// fill the vector vMonthNames with full month names
dateFormat = new SimpleDateFormat("MMMMM");
index = Calendar.JANUARY;
while (index <= Calendar.DECEMBER) {
vMonthNames.add(dateFormat.format(epoch.getTime()));
index++;
epoch.add(Calendar.MONTH, 1);
}

// re initialize to 1st jan of the year
dt1 = stringToDate("01/01/" + annee, "dd/MM/yyyy");
epoch.setTime(dt1);

// fill the table of the current year
while (epoch.get(Calendar.YEAR) == annee) {
dayofweek = epoch.get(Calendar.DAY_OF_WEEK);
weekofmonth = epoch.get(Calendar.WEEK_OF_MONTH);
month = epoch.get(Calendar.MONTH);
if (epoch.get(Calendar.DAY_OF_MONTH) < 10)
semyear[month][weekofmonth][dayofweek] = "0"
+ Integer
.toString(epoch.get(Calendar.DAY_OF_MONTH));
else
semyear[month][weekofmonth][dayofweek] = Integer
.toString(epoch.get(Calendar.DAY_OF_MONTH));
epoch.add(Calendar.DAY_OF_YEAR, 1);
}

// Cycle for Lines of months
for (int monthboucle = 0; monthboucle < 3; monthboucle++) {
epoch = GregorianCalendar.getInstance(TimeZone.getDefault());
// *************************************************************************************
// Print month names
MonthTitle = ((String) vMonthNames.get(0 + monthboucle * 4));
for (int i = MonthTitle.length(); i < 22; i++)
MonthTitle += " ";// up to char pos 22
MonthTitle += ((String) vMonthNames.get(1 + monthboucle * 4));
for (int i = MonthTitle.length(); i < 44; i++)
MonthTitle += " ";// up to char pos 44
MonthTitle += ((String) vMonthNames.get(2 + monthboucle * 4));
for (int i = MonthTitle.length(); i < 66; i++)
MonthTitle += " ";// up to char pos 66
MonthTitle += ((String) vMonthNames.get(3 + monthboucle * 4));
System.out.println(MonthTitle);

// Show days name
for (int i = 0; i < 4; i++) {
for (int j = epoch.getFirstDayOfWeek() - 1; j < 7; j++)
System.out.print(((String) vDayNames.get(j)).substring(
0, 2)
+ " ");
if (epoch.getFirstDayOfWeek() > 1)
for (int j = 0; j < epoch.getFirstDayOfWeek() - 1; j++)
System.out.print(((String) vDayNames.get(j))
.substring(0, 2)
+ " ");
System.out.print(" ");
}
System.out.println("");

// print a line of months
for (weekofmonth = 0; weekofmonth < 6; weekofmonth++)// 6
// lines
// per
// months
{
for (month = 0; month < 4; month++) // 4 months per line
{
for (int day = epoch.getFirstDayOfWeek(); day < 8; day++) // 7
// days
// per
// week
{
System.out
.print(semyear[month + monthboucle * 4][weekofmonth][day]);
if ((annee == epoch.get(Calendar.YEAR))
&& // test to put a "<" after the current
// day
((month + monthboucle * 4) == epoch
.get(Calendar.MONTH))
&& (semyear[month+monthboucle*4][weekofmonth][day].trim().length()!=0)
&& (Integer
.parseInt(semyear[month
+ monthboucle * 4][weekofmonth][day]
.trim()) == epoch
.get(Calendar.DAY_OF_MONTH)))
System.out.print("<");
else
System.out.print(" ");
}
if (epoch.getFirstDayOfWeek() > 1)
for (int day = 1; day < epoch.getFirstDayOfWeek(); day++) {
System.out.print(semyear[month + monthboucle
* 4][weekofmonth][day]);
if ((annee == epoch.get(Calendar.YEAR))
&& // test to put a "<" after the
// current day
((month + monthboucle * 4) == epoch
.get(Calendar.MONTH))
&& (semyear[month+monthboucle*4][weekofmonth][day].trim().length()!=0)
&& (Integer
.parseInt(semyear[month
+ monthboucle * 4][weekofmonth][day]
.trim()) == epoch
.get(Calendar.DAY_OF_MONTH)))
System.out.print("<");
else
System.out.print(" ");
}
System.out.print(" ");// split to next month on the
// same line
}
System.out.println("");// New line after week printed for 4
// months

}
System.out.println(""); // new line after the "Months line"
// *************************************************************************************
}// endfor
} catch (Exception e) {
e.printStackTrace();
}

}

public static Date stringToDate(String sDate, String sFormat)
throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
return sdf.parse(sDate);
}

}
PS: Plus d'erreurs a la compil
yvkoe Messages postés 32 Date d'inscription jeudi 20 septembre 2007 Statut Membre Dernière intervention 19 janvier 2009
25 sept. 2007 à 11:26
bonjour,
bravo là pas de blème tout fonctionne bravo encore
dje_jay Messages postés 58 Date d'inscription mercredi 17 décembre 2003 Statut Membre Dernière intervention 16 février 2011 2
24 sept. 2007 à 17:39
Bon ... Ben ok! :-p lol!
"isEmpty" est apparu à partir du JDK 1.6 ...
(Mon JDK par defaut n'est pas celui que je pensais)...
Vous pouvez remplacer "isEmpty" par ".length()!=0" (En effaçant le "!" du début de condition). Ca donne :
...
(semyear[month+monthboucle*4][weekofmonth][day].trim().length()!=0)&&
...
yvkoe Messages postés 32 Date d'inscription jeudi 20 septembre 2007 Statut Membre Dernière intervention 19 janvier 2009
24 sept. 2007 à 14:47
bonjour,
eh oui j'ai JDK 1.5 et eclipse dernière version(mise à jour automatique à l'ouverture)
j'ai fait un copy past ,essaye avec eclipse et copy paste sur la version ci dessus ,crois moi je préfèrerais ne pas avoir d'erreur car je trouve le cal très sympa...

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.