xandevieira 5 de ago. de 2008
nunca utilizei o joda-time, mas segue cod para calculo de diferença de horas pela classe Date
public static final long DAY = 24L * 60L * 60L * 1000L ;
public static final long HOUR = 60L * 60L * 1000L ;
public static final long MINUTES = 1000L * 60L ;
public static final long SECONDS = 1000L ;
public static long getDifHour ( java . util . Date dtMenor , java . util . Date dtMaior ) {
return ( dtMaior . getTime () - dtMenor . getTime ()) / ( HOUR );
}
public static long getDifMinutes ( java . util . Date dtMenor , java . util . Date dtMaior ) {
return ( dtMaior . getTime () - dtMenor . getTime ()) / ( MINUTES );
}
public static long getDifSecond ( java . util . Date dtMenor , java . util . Date dtMaior ) {
return ( dtMaior . getTime () - dtMenor . getTime ()) / ( SECONDS );
}
public static long getDifMilisecond ( java . util . Date dtMenor , java . util . Date dtMaior ) {
return dtMaior . getTime () - dtMenor . getTime ();
}
BiRdReD 5 de ago. de 2008
já tentei fazer assim só que não deu certo
private void tf_horasaidaFocusLost ( java . awt . event . FocusEvent evt ) {
double diferencahoras , diferencaminutos , diferencahoraminutos , tempototalhoras ;
String tempototal , horatotal ;
tempototal = ( String . valueOf ( Double . parseDouble ( tf_horasaida . getText ()) -
Double . parseDouble ( tf_horaentrada2 . getText ())));
diferencahoras = ( Double . parseDouble ( tempototal ) / ( 1000 * 60 * 60 ));
diferencaminutos = ( Double . parseDouble ( tempototal ) / ( 1000 * 60 ));
diferencahoraminutos = diferencaminutos % 60 ;
horatotal = String . valueOf ( diferencahoraminutos ) + ":" + String . valueOf ( diferencaminutos );
tf_horatotal . setText ( String . valueOf ( horatotal ));
}
porém falaram que era melhor com o joda
xandevieira 5 de ago. de 2008
experimenta esse codigo em conjunto com os ja passados.
public static java .util .Date convertDate ( String date ) {
DateFormat format = DateFormat .getDateInstance () ;
format .setLenient ( false ) ;
try {
return new java .util .Date ( format .parse ( date ) .getTime ()) ;
} catch ( Exception e ) {
return null ;
}
}
BiRdReD 5 de ago. de 2008
mas aonde exatamente eu colocaria esses códigos?
xandevieira 5 de ago. de 2008
sugiro vc criar uma classe utilitaria p.e DateUtil e jogar estes metodos la.
o uso ficaria algo mais ou meno assim:
private void tf_horasaidaFocusLost(java.awt.event.FocusEvent evt) {
double diferencahoras,diferencaminutos,diferencahoraminutos,tempototalhoras;
String tempototal,horatotal;
tempototal = (String.valueOf(Double.parseDouble(tf_horasaida.getText()) - Double.parseDouble(tf_horaentrada2.getText())));
diferencahoras = DateUtils.getDifHour(DateUtils.convertDate(tf_horaentrada2.getText()), DateUtils.convertDate(tf_horasaida.getText()));
diferencaminutos = DateUtils.getDifMinutes(DateUtils.convertDate(tf_horaentrada2.getText()), DateUtils.convertDate(tf_horasaida.getText()));
horatotal = String.valueOf(diferencahoraminutos)+":"+String.valueOf(diferencaminutos);
tf_horatotal.setText(String.valueOf(horatotal));
}
BiRdReD 5 de ago. de 2008
o que eu tenho que importar pra ele funcionar?
já que dá erro na parte de Dateutils
xandevieira 5 de ago. de 2008
que erro ta dando?
a principio somente estes imports
import java.util.Date ;
import java.text.DateFormat ;
BiRdReD 5 de ago. de 2008
Compiling 1 source file to C:\Documents and Settings\Admin\Desktop\Farme80\build\classes
C:\Documents and Settings\Admin\Desktop\Farme80\src\Servicos\geral.java:930: cannot find symbol
symbol : variable DateUtils
location: class Servicos.geral
diferencahoras = DateUtils.getDifHour(DateUtils.convertDate(tf_horaentrada2.getText()), DateUtils.convertDate(tf_horasaida.getText()));
BiRdReD 5 de ago. de 2008
Aonde tem dataUtils ele dá esse erro
xandevieira 5 de ago. de 2008
tá, mas vc criou essa classe?
Se nao criou vc deve criar e colocar os metodos que te passei nela.
BiRdReD 5 de ago. de 2008
cara mais quando eu crio a classe tambem daerro no public class…
xandevieira 5 de ago. de 2008
Ficou assim?
public class DateUtils {
public static java . util . Date convertDate ( String date ) {
DateFormat format = DateFormat . getDateInstance ();
format . setLenient ( false );
try {
return new java . util . Date ( format . parse ( date ). getTime ());
} catch ( Exception e ) {
return null ;
}
}
public static long getDifHour ( java . util . Date dtMenor , java . util . Date dtMaior ) {
return ( dtMaior . getTime () - dtMenor . getTime ()) / ( HOUR );
}
public static long getDifMinutes ( java . util . Date dtMenor , java . util . Date dtMaior ) {
return ( dtMaior . getTime () - dtMenor . getTime ()) / ( MINUTES );
}
public static long getDifSecond ( java . util . Date dtMenor , java . util . Date dtMaior ) {
return ( dtMaior . getTime () - dtMenor . getTime ()) / ( SECONDS );
}
public static long getDifMilisecond ( java . util . Date dtMenor , java . util . Date dtMaior ) {
return dtMaior . getTime () - dtMenor . getTime ();
}
public static final long DAY = 24L * 60L * 60L * 1000L ;
public static final long HOUR = 60L * 60L * 1000L ;
public static final long MINUTES = 1000L * 60L ;
public static final long SECONDS = 1000L ;
}
troque este linha
double diferencahoras , diferencaminutos , diferencahoraminutos , tempototalhoras ;
por esta
String diferencahoras , diferencaminutos , diferencahoraminutos , tempototalhoras ;
no metodo tf_horasaidaFocusLost
BiRdReD 6 de ago. de 2008
cara continua dando o mesmo erro =/
repetidamente