How do you convert date taken from a bash script to milliseconds in a Java program?

Posted by Matt Pascoe on Stack Overflow See other posts from Stack Overflow or by Matt Pascoe
Published on 2010-03-29T21:49:46Z Indexed on 2010/03/29 22:03 UTC
Read the original article Hit count: 393

Filed under:
|
|

I am writing a piece of code in Java that needs to take a time sent from a bash script and parse the time to milliseconds. When I check the millisecond conversion on the date everything is correct except for the month I have sent which is January instead of March.

Here is the variable I create in the bash script, which later in the script I pass to the Java program:

TIME=`date +%m%d%Y_%H:%M:%S`

Here is the Java code which parses the time to milliseconds:

String dt = "${scriptstart}";
java.text.SimpleDateFormat scriptStart = new java.text.SimpleDateFormat("MMDDyyyy_HH:mm:ss");
long start = scriptStart.parse(dt).getTime();

The goal of this statement is to find the elapsed time between the start of the script and the current system time.

To troubleshoot this I printed out the two:

 System Time = 1269898069496 (converted = Mon Mar 29 2010 16:27:49 GMT-0500 (Central Daylight Time))
 Script Start = 03292010_16:27:45
 Script Start in Milli = 1264804065000 (Converted = Fri Jan 29 2010 16:27:45 GMT-0600 (Central Standard Time))

© Stack Overflow or respective owner

Related posts about java

Related posts about bash