I am trying to convert a string with milliseconds (20180510-10:50:58.106) to date in java. However, when I convert it I get the millisecond part but again in the string. I want the same part in date format.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SimpleDateFormatExample {
public static void main(String[] args) {
String curDate = "20180510-10:50:58.106";
Date SysDateVar = null ;
//SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");
//String Todate = format.format(curDate);
//System.out.println("format.parse(curDate)="+Todate);
String abc = null;
try
{
abc = format.format(format.parse(curDate));
System.out.println("SysDateVar ="+abc);
SysDateVar = new Date ((long) (format.parse(curDate)).getTime());
System.out.println("format.parse(curDate)="+ SysDateVar);
}
catch (ParseException e) {
e.printStackTrace();
}
}
}
Output Is :
$javac SimpleDateFormatExample.java
$java -Xmx128M -Xms16M SimpleDateFormatExample
SysDateVar =20180510-10:50:58.106
format.parse(curDate)=Thu May 10 10:50:58 UTC 2018
Variable abc is a string. I want the same output in a Date variable.