You can use SimpleDateFormat
to parse your dates:
SimpleDateFormat formatter = new SimpleDateFormat( "MM/dd-HH:mm:ss.S" );
Calendar c = Calendar.getInstance();
c.setTime( formatter.parse( "07/21-04:00:14.766098" ) );
c.set( Calendar.YEAR, 2012 );
System.out.println( new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss.S" ).format( c.getTime() ) );
The line
c.set( Calendar.YEAR, 2012 );
sets correct year because formatter.parse( "07/21-04:00:14.766098" )
returns 1970 year
UPDATE:
Class Timestamp
allow you to add nanoseconds. To get your dates in Timestamp
object you can split your date string into date & time part and fractionsl part. Parse date & time part into timestamp and then add nanoseconds.
there is example code:
String dateString = "07/21-04:00:14.766098";
String[] parts = dateString.split( "\\." );
Calendar c = Calendar.getInstance();
c.setTime( ( new SimpleDateFormat( "M/d-H:m:s" ) ).parse( parts[0] ) );
c.set( Calendar.YEAR, 2014 );
java.sql.Timestamp timestamp = new java.sql.Timestamp( c.getTimeInMillis() );
if( parts.length == 2 ) {
// Your fractional part must be 9 numbers length with tailing zeros
// I am not sure how to pad string with zeros at right
int nanos = Integer.parseInt( String.format( "%-9s", parts[1] ).replace( ' ', '0' ) );
timestamp.setNanos( nanos );
}
System.out.println( "Date string: " + dateString );
System.out.println( "Timestamp : " + timestamp.toString() );
The advantage of Timestamp
is that it implements compassion and other useful methods.