Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read and wrire date in RMS

 
remish singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just pass ByteArrayInputStream as argument to DataInputStream and you can use readUTF(), readInt() and more. Same applies for DataOutputStream and writing methods
 
remish singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to add date..how can i do that
 
Peter Miklosko
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Convert Date/Calendar (not sure which you prefer) to long type ( getTimeInMillis()/ getTime() )and store it that way
 
remish singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
i have convert it in long..
like
dout.writeLong(getDOB().getTime());
DOB = new Date(din.readLong());

and in vector class

import java.io.*;
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() );
}
}

dout.flush();

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 );
System.out.println("Object:"+o);
System.out.println("Class:"+cl);
}
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic