File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java Micro Edition and the fly likes How to read and wrire date in RMS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "How to read and wrire date in RMS" Watch "How to read and wrire date in RMS" New topic

How to read and wrire date in RMS

remish singh

Joined: Oct 17, 2008
Posts: 3
Hello guys,
I want to ask that can we read and write Date through RMS.
Because in ByteArrayInputStream there is no method like
writeUTF(), writeInt() and writeBoolean().

So can anyone help me how to store and retrieve date from RMS

Thanks in advance.
Peter Miklosko
Ranch Hand

Joined: May 26, 2009
Posts: 76
Just pass ByteArrayInputStream as argument to DataInputStream and you can use readUTF(), readInt() and more. Same applies for DataOutputStream and writing methods
remish singh

Joined: Oct 17, 2008
Posts: 3
i want to add can i do that
Peter Miklosko
Ranch Hand

Joined: May 26, 2009
Posts: 76
Convert Date/Calendar (not sure which you prefer) to long type ( getTimeInMillis()/ getTime() )and store it that way
remish singh

Joined: Oct 17, 2008
Posts: 3
i have convert it in long..
DOB = new Date(din.readLong());

and in vector class

import java.util.*;

public class VectorHelper {

private static final int NULL = 0;
private static final int INTEGER = 1;
private static final int STRING = 2;
private static final int BOOLEAN= 3;
private static final int DATE= 4;
private static final int PERSISTENT = 5;

public static byte[] persist( Vector v )
throws IOException {
ByteArrayOutputStream bout =
new ByteArrayOutputStream();
DataOutputStream dout =
new DataOutputStream( bout );

int n = v.size();
dout.writeInt( n );

for( int i = 0; i < n; ++i ){
Object o = v.elementAt( i );
if( o instanceof String ){
dout.writeByte( STRING );
dout.writeUTF( (String) o );
} else if( o instanceof Integer ){
dout.writeByte( INTEGER );
dout.writeInt(((Integer) o).intValue() );
} else if( o instanceof Boolean ){
dout.writeByte( BOOLEAN );
dout.writeBoolean(((Boolean) o).booleanValue() );
} else if( o instanceof Date ){
dout.writeByte( DATE );
dout.writeLong(((Date) o).getTime());
}else if( o instanceof Persistent ){
dout.writeByte( PERSISTENT );
dout.writeUTF(o.getClass().getName() );
byte[] data =
((Persistent) o).persist();
dout.writeInt( data.length );
if( data.length > 0 ){
dout.write( data );
} else if( o == null ){
dout.writeByte( NULL );
} else {
throw new IOException(
"Cannot persist " +"object of type " +
o.getClass().getName() );


return bout.toByteArray();

public static Vector resurrect( byte[] persisted )
throws IOException {
ByteArrayInputStream bin =
new ByteArrayInputStream(
persisted );
DataInputStream din =
new DataInputStream( bin );

Vector v = new Vector();
int n = din.readInt();

for( int i = 0; i < n; ++i ){
int type = din.readByte();
if( type == NULL ){
v.addElement( null );
} else if( type == INTEGER ){
v.addElement( new Integer( din.readInt() ) );
} else if( type == STRING ){
v.addElement( din.readUTF() );
} else if( type == BOOLEAN){
v.addElement( new Boolean(din.readBoolean()) );
}else if( type == DATE){
v.addElement( new Date(din.readLong()) );
}else if( type == PERSISTENT ){
String cname = din.readUTF();
System.out.println("cname:" + cname);
int len = din.readInt();
byte[] tmp = new byte[ len ];

din.readFully( tmp );

try {
Class cl = Class.forName( cname );
Object o = cl.newInstance();
((Persistent) o).resurrect( tmp );
v.addElement( o );
catch( IOException e ){
throw e;
catch( Exception e ){
throw new IOException(
"Exception " +e.toString() );
} else {
throw new IOException( "Unknown " +
"type " + type );

return v;


and while i am setting it in main class i m getting error in this line
emp.setDOB(new Long(Long.parseLong("12-11-1986")));

how i can resolve this.
please help me
Peter Miklosko
Ranch Hand

Joined: May 26, 2009
Posts: 76
You cannot be serious that you will be reading user input from general TextBox??? (I presume you first read data from some form filled in by user, you store these data and later re-display them) What about validation of date, to make sure that user did not give February 30th day, or made new 13th month?
You are forfeiting all back-stage functionality which you can have through DateField
I agree. Here's the link:
subject: How to read and wrire date in RMS