IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

récuperer les dates de la semaine courante


Sujet :

Langage Java

  1. #1
    Nouveau membre du Club
    Inscrit en
    Décembre 2006
    Messages
    54
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 54
    Points : 34
    Points
    34
    Par défaut récuperer les dates de la semaine courante
    Bonjour

    Je galére pour récuperer avec une fonction les dates de la semaine courante et que ça change toutes les semaines.

    Si quelqu'un a une idée sur commenr faire.

    merci

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    81
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2007
    Messages : 81
    Points : 90
    Points
    90
    Par défaut
    Peut tu nous montrez le code que tu as déjà ?

    Je galére pour récuperer avec une fonction les dates de la semaine courante et que ça change toutes les semaines.
    et que ça change toutes les semaines ?
    La je n'ai pas compris. Une fonction qui te retourne les Date() de la semaine suivante ?


    As-tu réussi à récupérer la Date d'aujourd'hui ?

  3. #3
    Nouveau membre du Club
    Inscrit en
    Décembre 2006
    Messages
    54
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 54
    Points : 34
    Points
    34
    Par défaut
    Voici le code que j'ai pour l'instant mais ça ne colle pas trop

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
     
    package test;
     
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.List;
     
    public class TestSemaineDate {
     
    	public static void main(String[] args){
     
    		GregorianCalendar calendar = new java.util.GregorianCalendar(); 
    //		 Initialisé à la date et l'heure courrante. 
    		Date maDate=new Date();
    		calendar.setTime( maDate ); 
    //		 Initialisé avec une instance de Date.
    		//int currentDay = maDate.getDay();
    		//int currentMonth = maDate.getMonth();
    		//int currentYear = maDate.getYear();
    		int currentDay = calendar.getFirstDayOfWeek();
    		int currentMonth = calendar.getTime().getMonth();
    		int currentYear = calendar.getTime().getYear();
    		List week=new ArrayList();
     
    		int monday=0;
    		int tuesday=0;
    		int wednesday=0;
    		int thursday=0;
    		int friday=0;
    		int saturday=0;
    		int sunday=0;
     
     
     
     
    		if(currentDay==Calendar.MONDAY||currentDay==Calendar.TUESDAY
    				|| currentDay==Calendar.WEDNESDAY ||currentDay==Calendar.THURSDAY
    				|| currentDay==Calendar.FRIDAY || currentDay==Calendar.SATURDAY
    				|| currentDay==Calendar.SUNDAY){
    			if(currentDay==Calendar.MONDAY){
    				monday=currentDay;
    				tuesday=currentDay+1;
    				wednesday=currentDay+2;
    				thursday=currentDay+3;
    				friday=currentDay+4;
    				saturday=currentDay+5;
    				sunday=currentDay+6;
    			}
    			else if(currentDay==Calendar.TUESDAY){
    				monday=currentDay-1;
    				tuesday=currentDay;
    				wednesday=currentDay+1;
    				thursday=currentDay+2;
    				friday=currentDay+3;
    				saturday=currentDay+4;
    				sunday=currentDay+5;
    			}else if(currentDay==Calendar.WEDNESDAY){
    				monday=currentDay-2;
    				tuesday=currentDay-1;
    				wednesday=currentDay;
    				thursday=currentDay+1;
    				friday=currentDay+2;
    				saturday=currentDay+3;
    				sunday=currentDay+4;
    			}else if(currentDay==Calendar.THURSDAY){
    				monday=currentDay-3;
    				tuesday=currentDay-2;
    				wednesday=currentDay-1;
    				thursday=currentDay;
    				friday=currentDay+1;
    				saturday=currentDay+2;
    				sunday=currentDay+3;
    			}else if(currentDay==Calendar.FRIDAY){
    				monday=currentDay-4;
    				tuesday=currentDay-3;
    				wednesday=currentDay-2;
    				thursday=currentDay-1;
    				friday=currentDay;
    				saturday=currentDay+1;
    				sunday=currentDay+2;
    			}else if(currentDay==Calendar.SATURDAY){
    				monday=currentDay-2;
    				tuesday=currentDay-4;
    				wednesday=currentDay-3;
    				thursday=currentDay-2;
    				friday=currentDay-1;
    				saturday=currentDay;
    				sunday=currentDay+1;
    			}else if(currentDay==Calendar.SUNDAY){
    				monday=currentDay-6;
    				tuesday=currentDay-5;
    				wednesday=currentDay-4;
    				thursday=currentDay-3;
    				friday=currentDay-2;
    				saturday=currentDay-1;
    				sunday=currentDay;
    			}
    		}
     
    		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd");
     
    		dateFormat.format(maDate);
     
    		week.add(monday+"/"+currentMonth+"/"+currentYear);
    		week.add(tuesday+"/"+currentMonth+"/"+currentYear); 
    		week.add(wednesday+"/"+currentMonth+"/"+currentYear); 
    		week.add(thursday+"/"+currentMonth+"/"+currentYear); 
    		week.add(friday+"/"+currentMonth+"/"+currentYear); 
    		week.add(saturday+"/"+currentMonth+"/"+currentYear); 
    		week.add(sunday+"/"+currentMonth+"/"+currentYear); 
     
     
    	}
     
    }

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    81
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2007
    Messages : 81
    Points : 90
    Points
    90
    Par défaut
    Tu essaye de faire ca ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
     
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.List;
     
    public class Main
    {
     
    	public static void main(String[] args)
    	{
     
    		GregorianCalendar calendar = new GregorianCalendar();
    		Date date = new Date();
    		calendar.setTime(date);
     
    		int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
    		int currentMonth = calendar.getTime().getMonth();
    		int currentYear = calendar.getTime().getYear();
    		List week = new ArrayList();
     
     
     
    		int monday = 0;
    		int tuesday = 0;
    		int wednesday = 0;
    		int thursday = 0;
    		int friday = 0;
    		int saturday = 0;
    		int sunday = 0;
     
    		if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)
    		{
    			monday = currentDay;
    			tuesday = currentDay + 1;
    			wednesday = currentDay + 2;
    			thursday = currentDay + 3;
    			friday = currentDay + 4;
    			saturday = currentDay + 5;
    			sunday = currentDay + 6;
    		}
    		else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY)
    		{
    			monday = currentDay - 1;
    			tuesday = currentDay;
    			wednesday = currentDay + 1;
    			thursday = currentDay + 2;
    			friday = currentDay + 3;
    			saturday = currentDay + 4;
    			sunday = currentDay + 5;
    		}
    		else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY)
    		{
    			monday = currentDay - 2;
    			tuesday = currentDay - 1;
    			wednesday = currentDay;
    			thursday = currentDay + 1;
    			friday = currentDay + 2;
    			saturday = currentDay + 3;
    			sunday = currentDay + 4;
    		}
    		else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY)
    		{
    			monday = currentDay - 3;
    			tuesday = currentDay - 2;
    			wednesday = currentDay - 1;
    			thursday = currentDay;
    			friday = currentDay + 1;
    			saturday = currentDay + 2;
    			sunday = currentDay + 3;
    		}
    		else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)
    		{
    			monday = currentDay - 4;
    			tuesday = currentDay - 3;
    			wednesday = currentDay - 2;
    			thursday = currentDay - 1;
    			friday = currentDay;
    			saturday = currentDay + 1;
    			sunday = currentDay + 2;
    		}
    		else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
    		{
    			monday = currentDay - 2;
    			tuesday = currentDay - 4;
    			wednesday = currentDay - 3;
    			thursday = currentDay - 2;
    			friday = currentDay - 1;
    			saturday = currentDay;
    			sunday = currentDay + 1;
    		}
    		else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
    		{
    			monday = currentDay - 6;
    			tuesday = currentDay - 5;
    			wednesday = currentDay - 4;
    			thursday = currentDay - 3;
    			friday = currentDay - 2;
    			saturday = currentDay - 1;
    			sunday = currentDay;
    		}
     
    		System.out.println("Lundi " + monday + "/" + currentMonth + "/" + currentYear);
    		System.out.println("Mardi " + tuesday + "/" + currentMonth + "/" + currentYear);
    		System.out.println("Mercredi " + wednesday + "/" + currentMonth + "/" + currentYear);
    		System.out.println("Jeudi " + thursday + "/" + currentMonth + "/" + currentYear);
    		System.out.println("Vendredi " + friday + "/" + currentMonth + "/" + currentYear);
    		System.out.println("Samedi " + saturday + "/" + currentMonth + "/" + currentYear);
    		System.out.println("Dimanche " + sunday + "/" + currentMonth + "/" + currentYear);
     
    	}
    }

    PS: Tu as une méthode qui te permet de faire ca :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    // Permet d'augmenter la date de 6 jours.
    calendar.add(GregorianCalendar.DAY_OF_MONTH, 6);

  5. #5
    Nouveau membre du Club
    Inscrit en
    Décembre 2006
    Messages
    54
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 54
    Points : 34
    Points
    34
    Par défaut
    Je veux que la date s'auto incremente seule. Mon code actuel marche je crois

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
     
    package test;
     
     
     
     
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.List;
     
    public class TestSemaineDate {
     
    	public static void main(String[] args){
     
    		GregorianCalendar calendar =new GregorianCalendar();
    		calendar.setTime(new Date());
    		int today =calendar.get(calendar.DAY_OF_WEEK);
     
    //		 Initialisé à la date et l'heure courrante. 
    		Date maDate=new Date();
     
    	    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMMM-yyyy");
    	    String formatedDate = formatter.format(maDate);
    	    Integer currentDay=new Integer(formatedDate.substring(0,2));
    	    String currentMonth=formatedDate.substring(3,7);
    	    String currentYear=formatedDate.substring(8,12);	
     
    		List week=new ArrayList();
     
    		int monday=0;
    		int tuesday=0;
    		int wednesday=0;
    		int thursday=0;
    		int friday=0;
    		int saturday=0;
    		int sunday=0;	
     
     
    		if(today==Calendar.MONDAY||today==Calendar.TUESDAY
    				|| today==Calendar.WEDNESDAY ||today==Calendar.THURSDAY
    				|| today==Calendar.FRIDAY || today==Calendar.SATURDAY
    				|| today==Calendar.SUNDAY){
    			if(today==Calendar.MONDAY){
    				monday=currentDay;
    				tuesday=currentDay+1;
    				wednesday=currentDay+2;
    				thursday=currentDay+3;
    				friday=currentDay+4;
    				saturday=currentDay+5;
    				sunday=currentDay+6;
    			}
    			else if(today==Calendar.TUESDAY){
    				monday=currentDay-1;
    				tuesday=currentDay;
    				wednesday=currentDay+1;
    				thursday=currentDay+2;
    				friday=currentDay+3;
    				saturday=currentDay+4;
    				sunday=currentDay+5;
    			}else if(today==Calendar.WEDNESDAY){
    				monday=currentDay-2;
    				tuesday=currentDay-1;
    				wednesday=currentDay;
    				thursday=currentDay+1;
    				friday=currentDay+2;
    				saturday=currentDay+3;
    				sunday=currentDay+4;
    			}else if(today==Calendar.THURSDAY){
    				monday=currentDay-3;
    				tuesday=currentDay-2;
    				wednesday=currentDay-1;
    				thursday=currentDay;
    				friday=currentDay+1;
    				saturday=currentDay+2;
    				sunday=currentDay+3;
    			}else if(today==Calendar.FRIDAY){
    				monday=currentDay-4;
    				tuesday=currentDay-3;
    				wednesday=currentDay-2;
    				thursday=currentDay-1;
    				friday=currentDay;
    				saturday=currentDay+1;
    				sunday=currentDay+2;
    			}else if(today==Calendar.SATURDAY){
    				monday=currentDay-2;
    				tuesday=currentDay-4;
    				wednesday=currentDay-3;
    				thursday=currentDay-2;
    				friday=currentDay-1;
    				saturday=currentDay;
    				sunday=currentDay+1;
    			}else if(today==Calendar.SUNDAY){
    				monday=currentDay-6;
    				tuesday=currentDay-5;
    				wednesday=currentDay-4;
    				thursday=currentDay-3;
    				friday=currentDay-2;
    				saturday=currentDay-1;
    				sunday=currentDay;
    			}
    		}
     
    		week.add(monday+"/"+currentMonth+"/"+currentYear);
    		week.add(tuesday+"/"+currentMonth+"/"+currentYear); 
    		week.add(wednesday+"/"+currentMonth+"/"+currentYear); 
    		week.add(thursday+"/"+currentMonth+"/"+currentYear); 
    		week.add(friday+"/"+currentMonth+"/"+currentYear); 
    		week.add(saturday+"/"+currentMonth+"/"+currentYear); 
    		week.add(sunday+"/"+currentMonth+"/"+currentYear); 
     
     
    	}
     
    }

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    81
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2007
    Messages : 81
    Points : 90
    Points
    90
    Par défaut
    As-tu testé ma seconde réponse et vue le PS ? Tu as même une méthode qui te permet de faire ceci de façon automatique avec la gestion des changements de mois ou d'année.

    En effet avec ton code, si nous somme le lundi 31.
    Tu auras pour réponse :
    Mardi 32
    Mercredi 33
    ...

    La méthode permet d'éviter ces erreurs :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    calendar.add(GregorianCalendar.DAY_OF_MONTH, 6);

  7. #7
    Nouveau membre du Club
    Inscrit en
    Décembre 2006
    Messages
    54
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 54
    Points : 34
    Points
    34
    Par défaut
    si je l'ai testé sans l'appel à la méthode d'ajout pour le mois de juin j'ai par 01/05/2007 au lieu de 01/06/2007 je pense que janvier commence par 0? c'est ça?

    Je teste l'ajout avec ta méthide te je te reviens

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    81
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2007
    Messages : 81
    Points : 90
    Points
    90
    Par défaut
    Oui pour le mois 0 correspond à Janvier. De plus si tu affiche le jour de la semaine, Dimanche = 1.

  9. #9
    Nouveau membre du Club
    Inscrit en
    Décembre 2006
    Messages
    54
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 54
    Points : 34
    Points
    34
    Par défaut
    Ok ça marche mais je ne sais pas où ajouter cette méthode.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    calendar.add(GregorianCalendar.DAY_OF_MONTH, 6);
    Logiquement j'ai pas besoin de le faire parce que me donne la date courante à chaque fois.

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    81
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2007
    Messages : 81
    Points : 90
    Points
    90
    Par défaut
    Oui c'est vrai, mais toi tu veux récupérer les Date() de la semaine

    Toi tu fait :
    Lundi = courant
    Mardi = Lundi + 1 // Imagine si Lundi = 31 alors Mardi = 32 ce qui est faux...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. récupérer les dates d'une semaine
    Par nicerico dans le forum Langage
    Réponses: 3
    Dernier message: 26/07/2010, 20h32
  2. Réponses: 5
    Dernier message: 27/07/2007, 16h54
  3. remplir un comboBox avec les dates de la semaine courante et glissante
    Par k6971 dans le forum Interfaces Graphiques en Java
    Réponses: 2
    Dernier message: 25/05/2007, 10h11
  4. Réponses: 6
    Dernier message: 11/04/2007, 16h54
  5. Extraire les semaines dans les dates
    Par LE CORRE dans le forum C
    Réponses: 4
    Dernier message: 22/02/2006, 12h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo