Ciao
vorrei sapere come posso convertire una stringa (es. 01/01/1900) in formato data ???
in java
infatti sto usando jsp e mi serve comparare se una data presa dal database e' uguale a "01/01/1900"
Come posso fare ?
grazie
billy
Ciao
vorrei sapere come posso convertire una stringa (es. 01/01/1900) in formato data ???
in java
infatti sto usando jsp e mi serve comparare se una data presa dal database e' uguale a "01/01/1900"
Come posso fare ?
grazie
billy
Ciao,
che io sappia non esiste di default un formato data in java, cmq visto che i formati data sono tutti uguali, potresti semplicemente usare o ridefinire il metodo equals a tuo piacimento e tramite quello fare la comparazione di due date.
In output avrai un boolean con valore true o false a seconda...
A presto,
kekko
mi da errore cosi' :
Timestamp strd = rs.getTimestamp("datam");
String strdata;
strdata = "01/01/1900";
if(strd.toString().trim().compareTo(strdata) != 0)
{
................
}
Il supporto per le date e' contenuto nel package java.util, se vuoi solo convertire la stringa prova con
java.tex.DateFormat.
Questa e' una classe che fornisce un metodo statico per fare quello di cui necessiti:
DateFormat.parse( String)
Accetta in input la stringa da convertire e restituisce un oggetto Date relativo alla data inserita
Ciao,
Lorenzo
prova a fare così:
if(!(strd.toString().trim().equals(strdata)))
{
.... entra nel metodo se le due date sono diverse...
}
fammi sapere come va,
ciao,
kekko
ciao
mi dice cannot resolve symbol strdata
Timestamp strd = rs.getTimestamp(getCampo());
String strdata;
strdata = "01/01/1900";
if(!(strd.toString().trim().equals(strdata)))
Timestamp strd = rs.getTimestamp(getCampo());
String strdata;
strdata = "01/01/1900";
if(!(strd.toString().trim().equals(strdata)))
Non credo che sia possibile, è una semplice variabile di tipo stringa che hai dichiarato al rigo sopra.
Hai delle restrizioni nella classe?
Prova a dichiarare la variabile pubblica.
prima del if, prova a mettere la seguente istruzione:
System.out.println(strd.toString().trim());
System.out.println(strdata);
ti da errori?
non dovrebbe, dimmi un po'
non mi da' errore solo se questo if lo metto in un punto diverso
ecco l'intero codice :
se l'if lo metto in un punto (colore verde) non mi da errore, se lo metto dove mi serve (colore rosso) mi da' errore...
public void otherDoStartTagOperations() throws JspException {
String str;
boolean ret;
TagSupport tag;
ResultSet rs = null;
tag = (TagSupport)this.getParent();
try{
rs = (ResultSet)tag.getClass().getField("rs").get(this. getParent());
}
catch (NoSuchFieldException e) {
throw new JspException(e.toString());
}
catch (IllegalAccessException e) {
throw new JspException(e.toString());
}
try {
ret = rs.isAfterLast();
}
catch (SQLException e) {
throw new JspException(e.toString());
}
if (!ret) {
try {
int format = DateFormat.MEDIUM;
if (getFormat().trim().toLowerCase().compareTo("short ") == 0 ) {format = DateFormat.SHORT;}
else if (getFormat().trim().toLowerCase().compareTo("mediu m") == 0 ) {format = DateFormat.MEDIUM;}
else if (getFormat().trim().toLowerCase().compareTo("long" ) == 0 ) {format = DateFormat.LONG;}
else if (getFormat().trim().toLowerCase().compareTo("full" ) == 0 ) {format = DateFormat.FULL;}
else {format = DateFormat.MEDIUM;}
/*
SHORT is completely numeric, such as 12.13.52 or 3:30pm
MEDIUM is longer, such as Jan 12, 1952
LONG is longer, such as January 12, 1952 or 3:30:32pm
FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.
*/
DateFormat dtf= null;
Timestamp strd = rs.getTimestamp(getCampo());
if(((String)pageContext.getAttribute("localeT")).c ompareTo("it_IT") == 0) {
dtf = DateFormat.getDateInstance(format, Locale.ITALY);
} else if (((String)pageContext.getAttribute("localeT")).com pareTo("us_US") == 0) {
dtf = DateFormat.getDateInstance(format, Locale.US);
} else if (((String)pageContext.getAttribute("localeT")).com pareTo("de_DE") == 0) {
dtf = DateFormat.getDateInstance(format, Locale.GERMANY);
} else if (((String)pageContext.getAttribute("localeT")).com pareTo("fr_FR") == 0) {
dtf = DateFormat.getDateInstance(format, Locale.FRANCE);
}
else {
dtf = DateFormat.getDateInstance(format, Locale.ITALY);
}
str = dtf.format(strd);
// new 22 novembre 2002
//String strdata;
//String datamy;
//strdata = "01/01/1900";
//datamy = dtf.format(strdata);
//System.out.println(strd.toString().trim());
//System.out.println(strdata);
if(!(strd.toString().trim().equals(strdata)))
{
}
// end new
}
catch (SQLException e) {
throw new JspException(e.toString());
}
try {
if(str != null) {
// new 22 novembre 2002
String strdata;
//String datamy;
strdata = "01/01/1900";
//if(str.toString().trim().compareTo("01/01/1900") != 0)
if(!(strd.toString().trim().equals(strdata))){
{
// end new
pageContext.getOut().write("<SPAN CLASS=\"data\">");
if(getHref() != null) {
pageContext.getOut().write("<A " + getA_attributi() + " href=\"" + getHref() + "?recn=" + URLEncoder.encode(rs.getString("recn")) + "\">");
}
int len = str.length();
for (int indx = 0; indx < len; indx++) {
if( str.charAt(indx) == '\n') {
pageContext.getOut().write("
");
} else {
pageContext.getOut().write(str.charAt(indx));
}
}
if(getHref() != null) {
pageContext.getOut().write("</A>");
}
pageContext.getOut().write("</SPAN>");
// new 22 novembre 2002
}
else
{
pageContext.getOut().write("");
}
// end new
}
}
catch (IOException e) {
throw new JspException(e.toString());
}
catch (SQLException e) {
throw new JspException(e.toString());
}
}
} // fine otherdostarttag()
Non ho capito l'errore che ti da, cmq prova a definire la variabile String strdata fuori dal corpo del try catch
ultima cosa cosa ti viene in output dalle due Sistem.out.println?