|
Technical Interview Questions
Javascript Interview Questions
Oracle Interview Questions
J2EE Interview Questions
C++
Interview Questions
XML
Interview Questions
EJB
Interview Questions
JSP
Interview Questions
.........More
Programming Source Codes
Java Source Codes
Html Source Codes
CSS Source Codes
C Source Codes
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
JDBC Interview Questions and Answers
How do I display and parse a date?
The Java I18N way is to use a DateFormat. While
SimpleDateFormat, which is generally returned, creates a
large number of objects, it is locale aware and will
handle most of your needs. The following sample code
initially creates a java.sql.Date object and formats it
for the default locale. An initial actionPerformed call
additionally formats/displays it for a German locale and
also displays the resulting java.sql.Date in standard
escape format. Other dates can be entered and parsed
after the initial display.
// JDFDP.java - Display and Parse java.sql.Date
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class JDFDP extends JFrame
implements ActionListener,
WindowListener
{
// create a java.sql.Date
java.sql.Date jsqlDate = new java.sql.Date(
System.currentTimeMillis() );
DateFormat dfLocal = DateFormat.getDateInstance(
DateFormat.SHORT );
DateFormat dfGermany = DateFormat.getDateInstance(
DateFormat.SHORT, Locale.GERMANY );
JButton jb = new JButton( "Go" );
JLabel jlI = new JLabel("Input a Date:"),
jlD = new JLabel("Display German:"),
jlP = new JLabel("Parsed:");
JPanel jp = new JPanel();
JTextField jtI = new JTextField( 10 ),
jtD = new JTextField( 10 ),
jtP = new JTextField( 10 );
public JDFDP()
{
super( "JDFDP" );
addWindowListener( this );
jb.addActionListener( this );
jp.add(jlI);
jp.add(jtI);
jp.add(jb);
jp.add(jlD);
jp.add(jtD);
jp.add(jlP);
jp.add(jtP);
getContentPane().add( jp, BorderLayout.CENTER );
pack();
// set text by sending dummy event
jtI.setText( dfLocal.format( jsqlDate ) );
actionPerformed(
new ActionEvent( this, 12, "12" ) );
show();
} // end constructor
// ActionListener Implementation
public void actionPerformed(ActionEvent e)
{
jtD.setText( "" );
jtP.setText( "" );
try
{
java.util.Date d = dfLocal.parse(
jtI.getText() );
jtI.setText( dfLocal.format( d ) );
jtD.setText( dfGermany.format( d ) );
d = dfGermany.parse( jtD.getText() );
// get new java.sql.Date
jsqlDate = new java.sql.Date( d.getTime() );
jtP.setText( jsqlDate.toString() );
}
catch( ParseException pe ) { jtI.setText( "" ); }
} // End actionPerformed
// Window Listener Implementation
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
// End Window Listener Implementation
public static void main(String[] args)
{
new JDFDP();
}
} // end class JDFDP
How can I retrieve string data from a database in
Unicode format?
The data is already in Unicode when it arrives
in your program. Conversion from and to the
encoding/charset/CCSID in the database from/to
Unicode in the program is part of the JDBC driver's job.
If, for some reason, you want to see the data in
'\uHHHH' format ( where 'H' is the hex value ),
the following code, while not very efficient,
should give you some ideas:
public class UniFormat
{
public static void main( String[] args )
{
char[] ac = args[0].toCharArray();
int iValue;
String s = null;
StringBuffer sb = new StringBuffer();
for( int ndx = 0; ndx < ac.length; ndx++ )
{
iValue = ac[ndx];
if( iValue < 0x10 )
{
s = "\\u000";
}
else
if( iValue < 0x100 )
{
s = "\\u00";
}
else
if( iValue < 0x1000 )
{
s = "\\u0";
}
sb.append( s + Integer.toHexString( iValue ) );
} // end for
System.out.println("The Unicode format of " +
args[0] + " is " + sb );
} // end main
} // end class UniFormat
Page Numbers :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Java Interview
Questions for more Java Interview Questions with answers
Check
Structs Interview
Questions for more Structs Interview Questions with answers
Check
Servlet Interview
Questions for more Servlet Interview Questions with answers
|