0

Basically I'm attempting to parse date/time to Java, but having issues when trying to parse the milliseconds.

Example of data to be parsed: a[0] = 16/03/2015, a[1] = 10:00:18.120

I read in the two values and concatenate them. Getting: dateTime = (java.lang.String) "16/03/2015 10:00:18.120" As you can see the string has the milliseconds when i debug it. From here I parse it to SimpleDateFormat. It works- however the milliseconds are not displayed

DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
String dateTime;
dateTime= a[0]+" "+a[1];
Date d = df.parse(dateTime);

Current output: d = (java.util.Date) Mon Mar 16 10:00:18 GMT 2015

Thanks for your help.

Max
  • 41
  • 1
  • 9
  • not exactly a fix to your problem, but you could try parsing into Joda Time instead, see http://stackoverflow.com/questions/6252678/converting-a-date-string-to-a-datetime-object-using-joda-time-library – halafi Jul 09 '15 at 11:06
  • 5
    The `toString()` method of java.util.Date does not output milliseconds. This does not necessarily means that it does not contain them. Use the same SimpleDateFormat to format/convert to String it and you will see... – Robert Jul 09 '15 at 11:07
  • Everything looks fine. Try printing it with `System.out.println(df.format(d));`. – Keppil Jul 09 '15 at 11:08
  • How to format to string: http://stackoverflow.com/questions/1459656/how-to-get-the-current-time-in-yyyy-mm-dd-hhmisec-millisecond-format-in-java – Joni Turunen Jul 09 '15 at 11:09
  • 1
    Thanks guys- I was printing it out wrong. Much appreciated. – Max Jul 09 '15 at 11:15
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 10 '18 at 06:16

4 Answers4

1

Your code is fine, but not your interpretation of the result. As correctly mentioned in one comment, the method toString() of class java.util.Date does not output the millisecond part. But the millisecond part is still part of the state of your result object. Proof:

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
String dateTime = "16/03/2015 10:00:18.120";
Date d = df.parse(dateTime);
System.out.println(d); // Mon Mar 16 10:00:18 CET 2015
System.out.println(d.getTime()); // 1426496418120
System.out.println("millisecond-part=" + (d.getTime() % 1000)); // millisecond-part=120

So all is fine. You can even format your result back to a string using the same (or another instance of SimpleDateFormat - maybe with different pattern, locale and timezone).

If java.util.Date was correctly implemented as value-type then the inventors of that class would have taken care of making the output of toString() representing the whole exact state of the object but it has not happened - another example why this class is broken.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
0

Using DateFormat.format(Date date) function might meet your requirement

 Date date = new Date();
 DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS",Locale.ENGLISH);
 String dateTime;
 dateTime=df.format(date);
 String[] a=dateTime.split(" ");
 System.out.println(a[0]+" "+a[1]);
0
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS",Locale.ENGLISH);
    String dateTime;
    dateTime= "03/16/2015"+" "+"10:00:18.120";
    Date d = df.parse(dateTime);
    System.out.println(df.format(d));
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
0

Try this:

    String[] a = new String[]{"16/03/2015", "10:00:18.120"};

    DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
    String dateTime = a[0] + " " + a[1];

    try {
        Date d = df.parse(dateTime);
        System.out.println(d.getTime());//Returns milliseconds
    } catch (ParseException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }

The result: 1426492818120

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117