Saturday, November 15, 2014

Konversi java.util.Date Ke java.sql.Date

tengah malam ketemu pemecahan masalah konversi nilai tanggal dari java.util.Date ke java.sql.Date . hasil merangkai dari berbagai tulisan disini:


http://stackoverflow.com/questions/530012/how-to-convert-java-util-date-to-java-sql-date

http://www.java2s.com/Tutorial/Java/0040__Data-Type/ConvertfromajavautilDateObjecttoajavasqlDateObject.htm

http://stackoverflow.com/questions/23564363/getting-value-from-jdatechooser-and-saving-to-ms-sql-db

http://www.java2s.com/Code/PostgreSQL/Insert-Delete-Update/Insertdatedatatotable.htm


http://stackoverflow.com/questions/23447694/problems-using-jdatechooser-with-a-database


http://www.java2s.com/Code/PostgreSQL/Insert-Delete-Update/Insertdatedatatotable.htm

hasil akhir formulasi codingnya:


 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package latihan6;

import com.toedter.calendar.JDateChooser;

import javax.swing.*;

import java.awt.*;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.sql.*;

/**
 *
 * @author steven
 */
public class Frame5 extends javax.swing.JFrame {

    /**
     * Creates new form Frame5
     */
    public Frame5() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jDateChooser1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N

        jButton1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jButton1.setText("Print java.util.Date");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jButton2.setText("Print java.sql.Date");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2))
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jDateChooser1, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>                      

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
      
        Date tanggal = jDateChooser1.getDate();
      
        System.out.print(tanggal);
      
      
    }                                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
      
      
        // Date tanggal = jDateChooser1.getDate;
      
        java.sql.Date tanggal2 = new java.sql.Date(jDateChooser1.getDate().getTime());
      
        System.out.print(tanggal2);
      
      
      
      
      
      
    }                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Frame5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Frame5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Frame5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Frame5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Frame5().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                   
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    // End of variables declaration                 
}


Penampakan Program Kecilnya Ketika Dijalankan:











kalau menjalankan tombol Print java.util.Date maka format tanggal yg keluar adalah :

Mon Nov 03 00:00:00 WITA 2014

kalau menjalankan tombol Print java.sql.Date maka format tanggal yg keluar adalah :

2014-11-03

 
Format tanggal 2014-11-03 inilah yg katanya bisa dimasukan ke kolom tabel PostgreSQL yg memiliki type data Date.


Bolak balik nyari cara supaya outputnya JDateChooser adalah 2014-11-03 . ternyata format defaultnya java.sql.Date udah seperti itu. yah udah cocok lah. dan ternyata output standardnya JDateChooser adalah java.util.Date , jadi kalau outputnya mau dimasukan ke database harus di konversi dulu ke java.sql.Date




No comments:

Post a Comment

please, write your comment about this article