Soustraire de dates
sassi
-
xyzlm Messages postés 6 Date d'inscription Statut Membre Dernière intervention -
xyzlm Messages postés 6 Date d'inscription Statut Membre Dernière intervention -
Bonjour à tous,
Je voudrais savoir si quelqu'un peut m'aider à calculer le temps écoulé entre 2 dates, par exemple une date date et le jour présent.
Je sais qu'il faut faire un timestamp pour les 2 dates après les convertir en secondes, les soustraires et les reconvertir en mois et années mais j'y arrive pas... quelqu'un peut me donner un exemple concrêt en j2me svp et mercii
Je voudrais savoir si quelqu'un peut m'aider à calculer le temps écoulé entre 2 dates, par exemple une date date et le jour présent.
Je sais qu'il faut faire un timestamp pour les 2 dates après les convertir en secondes, les soustraires et les reconvertir en mois et années mais j'y arrive pas... quelqu'un peut me donner un exemple concrêt en j2me svp et mercii
A voir également:
- Soustraire de dates
- Nombre de jours entre deux dates excel - Guide
- Airpods 3 date de sortie - Guide
- Office 2024 date de sortie - Accueil - Bureautique
- Comment trier des dates par mois sur excel - Guide
- Samsung a33 date de sortie - Guide
2 réponses
Date date1 = date; Date date2 = new Date(); // aujourd'hui long tps = date2.getTime() - date1.getTime(); System.out.println(tps); // en milli-secondes System.out.println(tps/1000); // en secondes System.out.println(tps/1000/60); // en minutes System.out.println(tps/1000/60/60); // en heures System.out.println(tps/1000/60/60/24); // en jours System.out.println(tps/1000/60/60/24/30.4); // en mois System.out.println(tps/1000/60/60/24/365); // en années
Sujet Résolu ?
Non pas vraiment :/ je crois bien que j'ai un probleme dans mon code je ne sais pas ou exactement ;En effet , j'ai besoin de la soustraction des deux dates pour faire un test quant la soustraction des 2 dates > 0 le message a envoyer doit reste en attente jusqu'à se que le jour de l'envoie arrive (la date entre par l'utilisateur) mais je sais pas pourquoi quant j'exécute mon programme le compilateur ne tien pas conte du test que j'ai fais alors il envoi le message .Je suis entrain d'ecrire un code en j2me qui envoie un sms automatiquement selon la date enregistre par l'utilisateur.
package sms;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import java.util.Date;
/**
* @author Toshiba
*/
public class Midlet1 extends MIDlet implements CommandListener {
private Display display;
private Command exit;
private Command send;
private TextField msg;
private DateField calendrier;
private String message;
private String message1;
private String num;
private String phoneNumber;
private Form form;
private Form form1;
private TextField numero;
private long i;
private Date date;
protected void pauseApp(){
}
public void startApp() throws MIDletStateChangeException {
this.display = Display.getDisplay(this);
this.form=new Form("send sms");
this.msg=new TextField("number","",256,0);
this.send=new Command("send",4,1);
this.exit=new Command("exit",3,1);
this.calendrier=new DateField("La date d'envoi est :",DateField.DATE);
this.form.append(this.msg);
this.form.addCommand(this.send);
this.form.addCommand(this.exit);
this.form.setCommandListener(this);
this.display.setCurrent(this.form);
this.form.append(this.calendrier);
}
public Date getDate(){
Date date1 = this.calendrier.getDate();
return date1;
}
public void temps(){
Date date2 = new Date(); // aujourd'hui
i= date1.getTime() - date.getTime();
System.out.println("le temps est"+i);
}
public void commandAction(Command c, Displayable d) {
if(c==this.exit){
notifyDestroyed();
}
if(c==this.send){
if (i==0) {
System.out.println("sms envoyer");
this.num=this.msg.getString();
System.out.println("in loop"+this.num);
this.message1="joyeux anniverser ^^";
try {
sendit(this.message1, this.num);
} catch (IOException ex) {
ex.printStackTrace();
}
}
else
System.out.println("sms en attente");
try {
Thread.sleep((i));
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private void sendit(String message1, String num) throws IOException {
this.phoneNumber=num;
this.message=message1;
StringBuffer localStringBuffer=new StringBuffer(20);
String str="sms://"+num;
this.form1=new Form("");
MessageConnection localMessageConnection=null;
try{
localMessageConnection=(MessageConnection)Connector.open(str);
TextMessage localTextMessage=(TextMessage)localMessageConnection.newMessage("text");
localTextMessage.setAddress(str);
localTextMessage.setPayloadText(message1);
localMessageConnection.send(localTextMessage);
localMessageConnection.send(localTextMessage);
this.form1.append("message sent");
this.display.setCurrent(this.form1);
}catch (Exception localException){
this.form1.append("phone number"+str+localException.getMessage());
this.display.setCurrent(this.form1);
}
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
voila mon code aidez moi svp je suis désespéré :(
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import java.util.Date;
/**
* @author Toshiba
*/
public class Midlet1 extends MIDlet implements CommandListener {
private Display display;
private Command exit;
private Command send;
private TextField msg;
private DateField calendrier;
private String message;
private String message1;
private String num;
private String phoneNumber;
private Form form;
private Form form1;
private TextField numero;
private long i;
private Date date;
protected void pauseApp(){
}
public void startApp() throws MIDletStateChangeException {
this.display = Display.getDisplay(this);
this.form=new Form("send sms");
this.msg=new TextField("number","",256,0);
this.send=new Command("send",4,1);
this.exit=new Command("exit",3,1);
this.calendrier=new DateField("La date d'envoi est :",DateField.DATE);
this.form.append(this.msg);
this.form.addCommand(this.send);
this.form.addCommand(this.exit);
this.form.setCommandListener(this);
this.display.setCurrent(this.form);
this.form.append(this.calendrier);
}
public Date getDate(){
Date date1 = this.calendrier.getDate();
return date1;
}
public void temps(){
Date date2 = new Date(); // aujourd'hui
i= date1.getTime() - date.getTime();
System.out.println("le temps est"+i);
}
public void commandAction(Command c, Displayable d) {
if(c==this.exit){
notifyDestroyed();
}
if(c==this.send){
if (i==0) {
System.out.println("sms envoyer");
this.num=this.msg.getString();
System.out.println("in loop"+this.num);
this.message1="joyeux anniverser ^^";
try {
sendit(this.message1, this.num);
} catch (IOException ex) {
ex.printStackTrace();
}
}
else
System.out.println("sms en attente");
try {
Thread.sleep((i));
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private void sendit(String message1, String num) throws IOException {
this.phoneNumber=num;
this.message=message1;
StringBuffer localStringBuffer=new StringBuffer(20);
String str="sms://"+num;
this.form1=new Form("");
MessageConnection localMessageConnection=null;
try{
localMessageConnection=(MessageConnection)Connector.open(str);
TextMessage localTextMessage=(TextMessage)localMessageConnection.newMessage("text");
localTextMessage.setAddress(str);
localTextMessage.setPayloadText(message1);
localMessageConnection.send(localTextMessage);
localMessageConnection.send(localTextMessage);
this.form1.append("message sent");
this.display.setCurrent(this.form1);
}catch (Exception localException){
this.form1.append("phone number"+str+localException.getMessage());
this.display.setCurrent(this.form1);
}
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
voila mon code aidez moi svp je suis désespéré :(
getTime n'est pas une méthode de DateFilied donc sa marche pas.