Application Techniques



Converting Data Types

How to convert variables from one data type to another.

About this technique

Details

Category

Core Programming Techniques> Java

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on coding Java for SilverStream applications in the Programmer's Guide

Converting a String to an int   Top of page

The following code illustrates how to convert a String into an int value. A default String appears in an entry field, which you may change. When you clicks the appropriate conversion button, the result is then displayed in another field on the form. As the entry field and the display field receive the focus, the field's name and class are displayed for informational purposes.

  // Excerpt from the formActivate event of the form frmConversion: 
  // Display the text '12345' in field fldFrom1 
  fldFrom1.setText("12345"); 

This is the code for the actionPerformed event on button btnConvert1:

  // Get the string from fldFrom1 
  // Use the Integer class to convert it to an int, 
  // Then set fldTo1 to the new int value 
  String sValue = fldFrom1.getText(); 
  int iValue = Integer.parseInt(sValue); 
  fldTo1.setValue(iValue);     
  // Set the focus to fldTo1 so that the Control and Class 
  // labels are updated 
  fldTo1.requestFocus(); 

Notes about the code

Converting an int to a String   Top of page

The following code illustrates how to convert an int value into a String. A default number appears in an entry field, which you may change. When you clicks the appropriate conversion button, the result is then displayed in another field on the form. As the entry field and the display field receive the focus, the field's name and class are displayed for informational purposes.

  // Excerpt from the formActivate event of the form frmConversion: 
  // Display the integer 67890 in field fldFrom2 
  fldFrom2.setValue(67890); 

This is the code for the actionPerformed event on button btnConvert2:

  // Get the int value from fldFrom2 
  // Use the Integer class to convert it to a String, 
  // Then set fldTo2 to the new String value 
  int iValue = fldFrom2.getValue(); 
  String sValue = Integer.toString(iValue); 
  fldTo2.setText(sValue); 
   
  // Set the focus to fldTo2 so that the Control and  
  // Class labels are updated 
  fldTo2.requestFocus(); 

Note about the code

Converting a String to a Date   Top of page

The following code illustrates how to convert a String into a Date. A default String appears in an entry field, which you may change. When you clicks the appropriate conversion button, the result is then displayed in another field on the form. As the entry field and the display field receive the focus, the field's name and class are displayed for informational purposes.

  // Excerpt from the Loaded event of the form frmConversion 
  // Get today's date and create a java.sql.Date from it 
  java.util.Date dtNow = new java.util.Date(); 
  java.sql.Date dtToday = new java.sql.Date(dtNow.getYear(), dtNow.getMonth(), dtNow.getDate()); 
   
  // Display today's date as a string in field fldFrom3 
  fldFrom3.setText(1900 + dtNow.getYear() + "-" + (dtNow.getMonth() 
     + 1) + "-" + dtNow.getDate()); 

Note about the code

This is the code for the actionPerformed event on button btnConvert3:

  // Get the String value from fldFrom3 
  // Use the java.sql.Date class to convert it to a java.sql.Date, 
  // then set fldTo3 to the new java.sql.Date value 
  String sValue = fldFrom3.getText(); 
  java.sql.Date dtValue = java.sql.Date.valueOf(sValue); 
  fldTo3.setValue(dtValue); 
   
  // Set the focus to fldTo3 so that the Control and Class labels are updated 
  fldTo3.requestFocus(); 

Notes about the code

Converting a Date to a String   Top of page

The following code illustrates how to convert a Date into a String. A default Date appears in an entry field, which you may change. When you clicks the appropriate conversion button, the result is then displayed in another field on the form. As the entry field and the display field receive the focus, the field's name and class are displayed for informational purposes.

The class java.sql.Date is used rather than java.util.Date since databases do not generally understand Java.

  // Excerpt from the Loaded event of the form frmConversion 
  // Get today's date and create a java.sql.Date from it 
  java.util.Date dtNow = new java.util.Date(); 
  java.sql.Date dtToday = new java.sql.Date(dtNow.getYear(), dtNow.getMonth(), dtNow.getDate()); 
   
  // Display today's date in field fldFrom4 
  fldFrom4.setValue(dtToday);  

Note about the code

This is the code for the actionPerformed event on button btnConvert4

  // Get the java.sql.Date from fldFrom4 
  // Use the toString() method to convert it to a String, 
  // then set fldTo4 to the new String value 
  java.sql.Date dtValue = (java.sql.Date) fldFrom4.getValue(); 
  String sDate = dtValue.toString(); 
  fldTo4.setText(sDate); 
   
  // Set the focus to fldTo4 so that the Control and Class labels are updated 
  fldTo4.requestFocus(); 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.