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

Servlets/JSP Java Discussion :

[Date] Comment déclarer une variable sous la forme date(year-month-day)?


Sujet :

Servlets/JSP Java

  1. #1
    Membre habitué Avatar de adil_vpb
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Août 2006
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Août 2006
    Messages : 326
    Points : 132
    Points
    132
    Par défaut [Date] Comment déclarer une variable sous la forme date(year-month-day)?
    je voudrais tout simplement, déclarer une variable dans une class qui doit avoir une valeur de type date (de la forme suivante : 2007-03-06), par exemple j'ai un constructeur qui a trois paramétres :
    Test t=new Test(non,prenom,date_naiss);
    et je veux que le troisiéme paramétres soit de la forme (yyyy-mm-dd)

    Commment je peux faire ca?

    Merci d'avance !!!

  2. #2
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Points : 9 529
    Points
    9 529
    Billets dans le blog
    1
    Par défaut
    Regarde SimpleDateFormat

    A+

  3. #3
    Membre habitué Avatar de adil_vpb
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Août 2006
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Août 2006
    Messages : 326
    Points : 132
    Points
    132
    Par défaut
    oui j'ai la vue, mais le probléme c'est que :
    le constructeur doit avoir les trois params :
    Test t=new(String nom,Strong Pre, Date dn);

    et comment je peux passer à mon constructeur une variable de type Date aprés le formatage du date avec SimpleDateFormat ???

    Merci

  4. #4
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Points : 9 529
    Points
    9 529
    Billets dans le blog
    1
    Par défaut
    new SimpleDateFormat("yyyy/MM/dd").parse("2007/01/01")

  5. #5
    Membre habitué Avatar de adil_vpb
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Août 2006
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Août 2006
    Messages : 326
    Points : 132
    Points
    132
    Par défaut
    est ce que c'est comme ca :
    Date date=new SimpleDateFormat("yyyy-mm-dd");
    il me renvoie un erreur d'incompatible type !

    je veux que la variable date :
    Date date:
    String nom;
    String pren;
    public Test (String nom,String pren,Date date){
    this.nom=nom:
    this.pren=pren;
    this.date=date;
    }

    et si j'instancie un objet de la classe Test :
    Test t=new Test("a","b","2007-03-06");

    est ce que c'est mieux maintenant?
    svp j'attend votre réponse.
    Merci

  6. #6
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Points : 9 529
    Points
    9 529
    Billets dans le blog
    1
    Par défaut
    Choisit...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
           public Test (String nom,String pren,Date date)
    {
       this.nom=nom:
       this.pren=pren;
       this.date=date;
           }
     
    pour l'appel
     
           Test t=new Test("a", "b", new SimpleDateFormat("yyyy-MM-dd").parse("2007-03-06") );
    ou

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
           public Test (String nom,String pren, String date)
    {
       this.nom=nom:
       this.pren=pren;
       this.date= SimpleDateFormat("yyyy-MM-dd").parse(date);
           }
     
    pour l'appel
     
            Test t=new Test("a", "b", "2007-03-06" );

  7. #7
    Membre averti
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2004
    Messages
    265
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2004
    Messages : 265
    Points : 342
    Points
    342
    Par défaut
    Salut,

    Si j'ai bien compris il te faut :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Date date:
    String nom;
    String pren;
    public Test (String nom,String pren, String date){
    this.nom=nom:
    this.pren=pren;
    this.date=new SimpleDateFormat("yyyy-MM-dd").parse(date);
    }
    Edit : Arg grillé

  8. #8
    Membre habitué Avatar de adil_vpb
    Homme Profil pro
    Chef de projet MOA
    Inscrit en
    Août 2006
    Messages
    326
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Août 2006
    Messages : 326
    Points : 132
    Points
    132
    Par défaut
    voila ce que j'ai ecris :
    Date dated:
    String nom;
    String pren;
    public Test (String nom,String pren, String date){
    this.nom=nom:
    this.pren=pren;
    this.dated=new SimpleDateFormat("yyyy-MM-dd").parse(date);
    }

    mais il me renvoie une erreure :
    unreported exception java.text.ParseException; must be caught or declared to be thrown
    this.dated=new SimpleDateFormat("yyyy-MM-dd").parse(date);

    1 error

    c'est quoi ca?

  9. #9
    Membre averti
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2004
    Messages
    265
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2004
    Messages : 265
    Points : 342
    Points
    342
    Par défaut
    Tu devrais lire quelque documents/tutoriaux sur java... Ici tu as tout ce qu'il faut.

    La méthode parse(String) peut renvoyer une exception si la chaine passée en paramètre est incorrecte : regarde la javadoc

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    Date date:
    String nom;
    String pren;
    public Test (String nom,String pren, String date){
      this.nom=nom:
      this.pren=pren;
      try{
        this.date=new SimpleDateFormat("yyyy-MM-dd").parse(date);
      } catch(ParseException e) {
         e.printStackTrace();
      }
    }

Discussions similaires

  1. [Date] Comment déclarer une variable de la forme (year-month-day)?
    Par adil_vpb dans le forum Collection et Stream
    Réponses: 2
    Dernier message: 06/03/2007, 13h51
  2. Comment déclarer une variable binaire?
    Par Pragmateek dans le forum C++
    Réponses: 12
    Dernier message: 22/03/2006, 17h35
  3. Réponses: 5
    Dernier message: 20/09/2005, 22h48

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