I have the following date
2017-08-23-11.19.02.234850
it has the following date format
yyyy-MM-dd-HH.mm.ss.SSSSSS
What I want to do is to convert the date to format yyyy-MM-dd'T'HH:mm:ss.SSSSSS
I have the following code
    public static void main(String[] args) {
        String strDate = "2017-08-23-11.19.02.234850";
        String dateFmt = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
        System.out.println("converted Date: " + convertDate(strDate, dateFmt));
    }
    public static String convertDate(String strDate, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
        sdf.setLenient(true);
        try {
            Date dateIn = sdf.parse(strDate);
            return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS").format(dateIn);
        }catch(ParseException e) {
            e.printStackTrace();
        }
        return "";
    }
the result is
converted Date: 2017-08-23T11:22:56.000850
input date 2017-08-23-11.19.02.234850 converted date 2017-08-23T11:22:56.000850 doesn't look the same, it seems java is rounding the milliseconds besides if I turn lenient off for date validation
sdf.setLenient(false);
I get the following
java.text.ParseException: Unparseable date: "2017-08-23-11.19.02.234850"
    at java.text.DateFormat.parse(Unknown Source)
    at mx.santander.canonical.datamodel.enums.Main.convertDate(Main.java:74)
    at mx.santander.canonical.datamodel.enums.Main.main(Main.java:66)
converted Date:
How to build a function which validates and converts date strings like this in a proper way?
EDIT:
I added a new function to obtain results
/**
      * Gets the ISO 8601 date str from string.
      *
      * @param strDate the str date
      * @return the ISO 8601 date str from string
      */
     private String getISO8601DateStrFromString (String strDate)  {
        String responseISO8601Date = "";
        if(strDate == null || "".equals(strDate.trim())) {
            return responseISO8601Date;
        }
         try {
             String strDtWithoutNanoSec = strDate.substring(0, strDate.lastIndexOf("."));
             String strDtNanoSec        = strDate.substring(strDate.lastIndexOf(".") + 1, strDate.length());
             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
             formatter.setLenient(false);
             Date   date = formatter.parse(strDtWithoutNanoSec);
             Timestamp t = new Timestamp(date.getTime());
             t.setNanos(Integer.parseInt(strDtNanoSec));
             DateFormat   df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'");
             NumberFormat nf = new DecimalFormat("000000");
             responseISO8601Date = df.format(t.getTime()) + nf.format(t.getNanos());
         } catch (ParseException | StringIndexOutOfBoundsException | NumberFormatException e) {
             String errorMsg = String.format("The date provided for conversion to ISO 8601 format [%s] is not correct", strDate);
             System.out.println(errorMsg);
         } 
         return responseISO8601Date;
     }
What I get:
Uptadet date 2017-12-20T11:19:02.234850
 
     
     
     
    