String-Date conversion with nanoseconds

    private String convertDate(String cdate)
{
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
    SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd");
    Date convertedDate;
    try
    {
        convertedDate = dateFormat.parse(cdate);
        cdate = postFormater.format(convertedDate);
    }
    catch (ParseException e)
    {
        Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
    }
    return cdate;
}

Try this.


To drop the nanoseconds, use:

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.")

The result you are getting is absolutely right.

Let's analyze this:

17.08.2012 05:35:19:7600000
  • 17: Day of month (17th)
  • 08: Month of year (August)
  • 2012: Year (2012)
  • 05: Hour of day (5am)
  • 35: Minute of hour (:35)
  • 19: Second of minute (:19)
  • 7600000: Milliseconds of second (7,600,000)

Now, the way the VM sees this is that you are declaring the time of day as 5:35:19am, then adding 7,600,000 milliseconds to it. 7,600,000 milliseconds = 7,600 seconds = 2 hours, 6 minutes, 40 seconds. 5:35:19am + 02:06:40 = 7:41:59am (and 0 milliseconds). This is the result you are getting. (It also appears that you are not setting the timezone properly, so the GMT string is 3 hours behind your result.)

If you want to retain the :7600000, to my knowledge this is not possible. As this can be simplified into seconds, the VM will automatically reduce it into the other time increments. The milliseconds (the SSSS) should be for storing values <1000.

I'd suggest you create a new SimpleDateFormat for your output; but remember that the milliseconds will be absorbed into the other times (since they are all stored as a single long in the Date object).


This is what you need (but it will loose millisecond information):

"dd.MM.yyyy HH:mm:ss.'000000'"

If you used "dd.MM.yyyy HH:mm:ss.SSSSSS", then would get three leading zeros for your milliseconds.

If you used "dd.MM.yyyy HH:mm:ss.SSS'000'", then you could format a date, but not parse any date.

Try it out:

public static void main(String[] args) throws ParseException {
    printDate("dd.MM.yyyy HH:mm:ss.SSS");//02.05.2010 21:45:58.073
    printDate("dd.MM.yyyy HH:mm:ss.SSSSSS");//02.05.2010 21:45:58.000073
    printDate("dd.MM.yyyy HH:mm:ss.SSS'000'");//02.05.2010 21:45:58.073000
    printDate("dd.MM.yyyy HH:mm:ss.'000000'");//02.05.2010 21:45:58.000000

    tryToParseDate("dd.MM.yyyy HH:mm:ss.SSS");//good
    tryToParseDate("dd.MM.yyyy HH:mm:ss.SSSSSS");//good
    tryToParseDate("dd.MM.yyyy HH:mm:ss.SSS'000'");//bad
    tryToParseDate("dd.MM.yyyy HH:mm:ss.'000000'");//good
}

private static void printDate(String formatString) {
    Date now = new Date();
    SimpleDateFormat format = new SimpleDateFormat(formatString);
    String formattedDate = format.format(now);

    // print that date
    System.out.println(formattedDate);
}

private static void tryToParseDate(String formatString) {
    Date now = new Date();
    SimpleDateFormat format = new SimpleDateFormat(formatString);
    String formattedDate = format.format(now);

    // try to parse it again
    try {
        format.parse(formattedDate);
        System.out.println("good");
    } catch (ParseException e) {
        System.out.println("bad");
    }
}