Visualizzazione dei risultati da 1 a 8 su 8

Discussione: [JSP] Data e ora

  1. #1
    Utente di HTML.it L'avatar di Necsus
    Registrato dal
    Oct 2006
    Messaggi
    87

    [JSP] Data e ora

    Salve, mi chiedevo come potrei fare ad ottenere la data (gg/mm/aaa) e l'ora (hh:mm:ss) in una pagina JSP.
    Ho cercato in rete ma non ho trovato nulla che facesse al caso mio o che funzionasse qualvolta questi venissero inseriti in un DB.
    Grazie per le eventuali risposte!

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Niente di più:

    codice:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@page import="java.util.*,java.text.*"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page - Date Test</title>
        </head>
        <body>
            <h2>Un esempio con Date e SimpleDateFormat</h2>
            <%
            Date d = new Date();
            out.println(d+"
    ");
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy  HH:mm:ss");
            out.println(sdf.format(d));
            %>
        </body>
    </html
    Poi per l'altra richiesta, ovvero l'interazione con database, dovresti essere un po' più specifico: che cosa vuoi fare? Hai letto i tutorial vari (anche quelli che trovi qui sul forum)?
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it L'avatar di Necsus
    Registrato dal
    Oct 2006
    Messaggi
    87
    Innanzi tutto ti ringrazio per la tua risposta.
    Successivamente volevo chiederi una cosa:
    Come potrei "acchiappare" separatamente queste due informazioni (data ed ora) in due variaili stringa?
    Ovvero vorrei ottenere:
    String data="dd/MM/yyyy";
    String time="HH:mm:ss";
    Mi servirebbero di tipo stringa per un motivo ben preciso.

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Puoi inizializzare due SimpleDateFormat con il pattern che ti interessa su ciascuno, ovvero dd/MM/yyyy in uno e HH:mm:ss nell'altro. Con format tiri fuori uno String.

    Oppure con quel SimpleDateFormat che ho usato nell'esempio, e spezzi lo String (con split ad esempio) sullo spazio.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    Utente di HTML.it L'avatar di Necsus
    Registrato dal
    Oct 2006
    Messaggi
    87
    Ok, ti ringrazio per il suggerimento.
    Ho riscontrato però il seguente problema:
    Questa è la pagina in JSP:

    codice:
    <%@page import="java.util.*,java.text.*"%>
    
    <html>
        <head>
            <title>JSP Page - Date Test</title>
        </head>
        <body>
            <h2>Un esempio con Date e SimpleDateFormat</h2>
            <%
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            out.println(sdf.format(d));
    	String[] splitArray = d.split(" ");
    	String data= splitArray[0];
    	String time= splitArray[1];
    	out.println(data + " " + time);
            %>
        </body>
    </html>
    Ed ho ricevuto il seguente errore:

    An error occurred at line: 13 in the jsp file: /jsp/pages/prova.jsp
    The method split(String) is undefined for the type Date
    10: Date d = new Date();
    11: SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    12: out.println(sdf.format(d));
    13: String[] splitArray = d.split(" ");
    14: String data= splitArray[0];
    15: String time= splitArray[1];
    16: out.println(data + " " + time);
    Come risolvere?

  6. #6
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    The method split(String) is undefined for the type Date
    Il compilatore ti sta dicendo:
    Caro ragazzo,
    non mi rompere il ca...
    Se vuoi fare una splittata,
    non puoi farla su una data.
    sdf.format(d) sai cos'è?
    Se te lo chiedo c'è un perché!
    Splitta quello, amico mio,
    che a compilar poi ci penso io.

    Ok, vado a uccidermi.
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  7. #7
    Utente di HTML.it L'avatar di Necsus
    Registrato dal
    Oct 2006
    Messaggi
    87
    Ok, finchè uso questo codice nella mia "paginetta" di prova, fila tutto liscio.
    Quando invece lo inserisco nella pagina che mi interessa, mi da problemi:

    codice:
    <%@ page import="java.io.*,java.util.*,java.text.*,java.sql.*"%>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
    <html>
       <head>
          <title>J-Chat</title>
          <style>
    	@import url("../css/style.css");
          </style>
    	<script type="text/javascript" src="../js/effects.js"></script>
       </head>
       <body onload="attiva()">
       <form name="chat" method="POST" onsubmit="document.location='chat.jsp'">
    <input type="hidden" name="ora">
    <input type="hidden" name="data" >
    <table width="100%" height="90%" align="center">
    <tr>
    <td>
    		<table align="center" cellspacing="0" cellpadding="0">
    		<tr>
    		<td>[img]../img/logo.png[/img]</td>
    		<td id="small">Powered by Planeta Nicolò Stefano - Source: [img]../img/php.png[/img]</td>
    		</tr>
    		<tr>
    		<td colspan="2" id="main">
       <table id="table" align="center">
          <tr>
    <%
    String nickname = (String) session.getAttribute("nickname");
    %>         <td id="top" colspan="2">Benvenuto <a style="color:#7aa02f;"><%=nickname%></a> ! <input type="hidden" name="nickname" value="<%=nickname%>" /></td>
          </tr>
          <tr>
             <td colspan="2" align="center">
    	<iframe id="box" name="conversazione" src="conversazione.jsp"></iframe>
             </td>
          </tr>
          <tr>
             <td id="option">
    		<input type="button" class="bold" onClick='bold()' value="B"> 
    		<input type="button" class="italic" onClick='italic()' value="I"> 
    		<input type="button" class="underline" onClick='underline()' value="U"> 
    		<input type="button" class="mistake" onClick='mistake()' value="S">
    		
    
    
    		<input type="text" name="message" id="textfield"></td>
    		<td id="send">
    		<input src="../img/up.png" type="image" value="Invia" />
    	 </td>
          </tr>
          <tr>
             <td id="bottom" colspan="2"></td>
          </tr>
       </table>
    		</td>
    		</tr>
    		</table>
    </td>
    </tr>
    </table>
       </form>
    <%
    String msg=request.getParameter("message");
    Date d = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String[] splitArray = sdf.format(d).split(" ");
    String date= splitArray[0];
    String time= splitArray[1];
    if(msg!=null){
    Class.forName("org.gjt.mm.mysql.Driver");
    
    Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/jchat?user=root&password=");
    
    Statement statement = con.createStatement();
    String sql="insert into conversazione(nick,messaggio,time,date) values ('"+nickname+"','"+msg+"','"+time+"','"+date+"')";
    int rows = statement.executeUpdate(sql);
    statement.close();
    con.close();
    }
    %>
       </body>
    </html>
    Risposta:

    The type Date is ambiguous
    58: </form>
    59: <%
    60: String msg=request.getParameter("message");
    61: Date d = new Date();
    62: SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    63: String[] splitArray = sdf.format(d).split(" ");
    64: String date= splitArray[0];

  8. #8

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.