Jump to content
MarcioSalgado

Formatar data

Recommended Posts

MarcioSalgado

Alguém sabe como consigo mudar o DataFormatter() aparece mm/dd/yyyy e eu queria dd/mm/yyyy

O meu código está assim.

<%-- 
    Document   : index
    Created on : Oct 9, 2017, 11:11:52 AM
    Author     : Marcio Salgado
--%> 

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.poi.ss.usermodel.DataFormatter"%>
<%@page import="org.apache.poi.openxml4j.opc.OPCPackage"%>
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8"  
   pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <%@ page import ="java.util.Date" %>
    <%@ page import ="java.io.*" %>
    <%@ page import ="java.io.FileNotFoundException" %>
    <%@ page import ="java.io.IOException" %>
    <%@ page import ="java.util.Iterator" %>
    <%@ page import ="java.util.ArrayList" %>
    <%@ page import ="java.sql.Statement" %>
    <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
    <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
    <%@ page import="org.apache.commons.fileupload.*"%>
    <%@ page import="java.util.*, java.io.*" %>
    <%@ page import="java.util.Iterator"%>
    <%@ page import="java.util.List"%>
    <%@ page import="java.io.File"%>

    <%@ page import ="org.apache.poi.hssf.usermodel.HSSFCell" %>
    <%@ page import ="org.apache.poi.hssf.usermodel.HSSFRow" %>
    <%@ page import ="org.apache.poi.hssf.usermodel.HSSFSheet" %>
    <%@ page import ="org.apache.poi.hssf.usermodel.HSSFWorkbook" %>
    <%@ page import ="org.apache.poi.poifs.filesystem.POIFSFileSystem" %>
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Insert title here</title>  
 </head>  
 <body>  
 <jsp:useBean id="connection" class="DB.DB_Connection" scope="page">  
   <jsp:setProperty name="connection" property="*"/>  
 </jsp:useBean>       
     

     
 <%!    
   
 Connection con;  
 PreparedStatement ps=null;  
 ResultSet rs=null;  
 public static ArrayList readExcelFile(String fileName)  
 {  
   /** --Define a ArrayList  
     --Holds ArrayList Of Cells  
    */  
   ArrayList cellArrayLisstHolder = new ArrayList();  
   try{  
   /** Creating Input Stream**/  
      FileInputStream myInput = new FileInputStream(fileName);
   /** Create a POIFSFileSystem object**/  
   POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
   /** Create a workbook using the File System**/  
    HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
    /** Get the first sheet from workbook**/  
   HSSFSheet mySheet = myWorkBook.getSheetAt(0);  
   /** We now need something to iterate through the cells.**/  
    Iterator rowIter = mySheet.rowIterator();  
    while(rowIter.hasNext()){  
      HSSFRow myRow = (HSSFRow) rowIter.next();  
      Iterator cellIter = myRow.cellIterator();  
      ArrayList cellStoreArrayList=new ArrayList();  
      while(cellIter.hasNext()){  
        HSSFCell myCell = (HSSFCell) cellIter.next();  
        cellStoreArrayList.add(myCell);  
      }  
      cellArrayLisstHolder.add(cellStoreArrayList);  
    }  
   }catch (Exception e){e.printStackTrace(); }  
   return cellArrayLisstHolder;  

 }%>  
 

 
 <%  

String ImageFile="";
String itemName = "";
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart)
{
}
else
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try
{
items = upload.parseRequest(request);
}
catch (FileUploadException e)
{
e.getMessage();
}
 
Iterator itr = items.iterator();
while (itr.hasNext())
{
FileItem item = (FileItem) itr.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
if(name.equals("ImageFile"))
{
ImageFile=value;
}
 
}
else
{
try
{
itemName = item.getName();

File savedFile = new File(config.getServletContext().getRealPath("/")+"Example\\image-folder\\"+itemName);
item.write(savedFile);

}
catch (Exception e)
{
out.println("Error"+e.getMessage());
}
}
}
try
{ 
 //con=connection.getConn();  
 String query1="insert into test(image) values ('"+itemName+"')";  
 //ps=con.prepareStatement(query1);  
 
//ps.executeUpdate("insert into test(image) values ('"+itemName+"')");
 
}
catch(Exception el)
{
out.println("Inserting error"+el.getMessage());
}
}

 //request.setAttribute("file", request.getParameter("file"));
 //String fileName = itemName;
 String fileName = config.getServletContext().getRealPath("/")+"Example\\image-folder\\"+itemName;
 
 //String fileName="C:/teste2.xls";
 //"C:/teste2.xls"; // Excel File name.xls
 //Read an Excel File and Store in a ArrayList  

 ArrayList dataHolder=readExcelFile(fileName);  
 //Print the data read  
 //printCellDataToConsole(dataHolder);  
 con=connection.getConn();  
 String query="insert into teste1 values(?,?,?,?,?,?,?,?,?)";  
 ps=con.prepareStatement(query);  
 int count=0;
 ArrayList cellStoreArrayList=null;  
 //For inserting into database  
 String cmdSelect="";
 for (int i=1;i < dataHolder.size(); i++) {  
   cellStoreArrayList=(ArrayList)dataHolder.get(i);  
   
     String bank = cellStoreArrayList.get(7).toString();
     
     cmdSelect = "SELECT codigo FROM bancos WHERE n_abrev like '%"+bank+"%' ";
     Statement stmt = connection.getConn().createStatement();
     System.out.println(cmdSelect);
     ResultSet rs = stmt.executeQuery(cmdSelect);
     String idBank = "";
     
     while (rs.next()) {
        idBank = rs.getString("codigo");
			}
     SimpleDateFormat DtFormat = new SimpleDateFormat("dd/MM/yyyy");
     ps.setString(1,(new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(0))).toString());  
     ps.setString(2,(new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(1))).toString());  
     ps.setString(3,(new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(2))).toString()); 
     ps.setString(4,(new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(3))).toString()); 
     ps.setString(5,(new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(4))).toString());  
     ps.setString(6,(new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(5))).toString());  
     ps.setString(7,(new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(6))).toString()); 
     ps.setString(8,idBank); 
     ps.setString(9,(new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(8))).toString());  
     count= ps.executeUpdate();  
     System.out.print(((HSSFCell)cellStoreArrayList.get(8)).toString() + "t");  
     }  
 
    
 
 //For checking data is inserted or not?  
   if(count>0)  
     { %>  
     
     

     Foram inseridos na base de dados os seguintes dados.  
           <table>  
             <tr>  
               <th>paciente</th>  
               <th>nome</th>  
               <th>motivo</th>  
               <th>pag</th>  
               <th>cheque</th>  
               <th>conta</th>  
               <th>data</th>  
               <th>banco</th>  
               <th>data Entrega</th>  
             </tr>  
     <% for (int i=1;i < dataHolder.size(); i++) {  
   cellStoreArrayList=(ArrayList)dataHolder.get(i);%>  
   <tr>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(0))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(1))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(2))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(3))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(4))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(5))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(6))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(7))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(8))).toString()) %></td>  
   </tr>  
     <%}  
    }  
   else  
   {%>  
   <center> Details have not been inserted!!!!!!!!!</center>  
   <%  }  %>  
     </table>  
 </body>  
 </html>  

Alguém  me pode ajudar?

Obrigado

Share this post


Link to post
Share on other sites
FacaNaCaveira

Fala ai MarcioSalgado tudo blz?

vc ja fez uma parte do processo para formatar o campo só ficou faltando aplica-lo

Aqui voce Instanciou o SimpleDateFormat:

SimpleDateFormat DtFormat = new SimpleDateFormat("dd/MM/yyyy");

ficou faltando aplica-lo nos campos desejados, acredito ser na coluna 7 e 9. Então ficaria assim:

 //For checking data is inserted or not?  
   if(count>0)  
     { %>  
     
     

     Foram inseridos na base de dados os seguintes dados.  
           <table>  
             <tr>  
               <th>paciente</th>  
               <th>nome</th>  
               <th>motivo</th>  
               <th>pag</th>  
               <th>cheque</th>  
               <th>conta</th>  
               <th>data</th>  
               <th>banco</th>  
               <th>data Entrega</th>  
             </tr>  
     <% for (int i=1;i < dataHolder.size(); i++) {  
   cellStoreArrayList=(ArrayList)dataHolder.get(i);%>  
   <tr>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(0))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(1))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(2))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(3))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(4))).toString()) %></td>  
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(5))).toString()) %></td> 
          <% Date data = new date((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(6))).toString()); %>
        <td><%=DtFormat.format(data) %></td>
        <td><%=((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(7))).toString()) %></td>
          <% Date dataEntrega = new Date((new DataFormatter().formatCellValue((HSSFCell)cellStoreArrayList.get(8))).toString());%>
        <td><%=DtFormat.format(dataEntrega) %></td>  
   </tr>  
     <%}  
    }  
   else  
   {%>  
   <center> Details have not been inserted!!!!!!!!!</center>  
   <%  }  %>  
     </table>  

Espero ter ajudado

Abraços

Max

Edited by FacaNaCaveira

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.